Click here to Skip to main content
15,895,799 members
Articles / Programming Languages / C#

Enum, Alternate Values, and FluentNhibernate

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
29 May 2011CPOL6 min read 23.5K   303   10  
Pulling together various techniques to make enums, alternate values, and FluentNHibernate play well together
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using NHibernate.UserTypes;
using NHibernate.SqlTypes;

using Common;


namespace FluentNHibernateEnum.UnitTests.MapHelpers
{
    /// <summary>
    /// Mape EnumKey attribute to database
    /// </summary>
    /// <example>Map(x =&gt; x.ActivityType).CustomType&lt;EnumKeyMapping&lt;ActivityType&gt;&gt;().Column("ActivityType").Not.Nullable(); </example>
    /// <remarks> spinoff from http://stackoverflow.com/questions/1345426/fluent-nhibernate-table-per-hierarchy-mapping-problem by DaeMoon</remarks>
    /// <typeparam name="T"></typeparam>
    public class EnumKeyMapDescriptionHelper<T> : IUserType
    {

        private Int16 _length;

        public EnumKeyMapDescriptionHelper()
        {
            _length = 255;
        }

        public EnumKeyMapDescriptionHelper(Int16 length)
        {
            _length = length;
        }

        #region IUserType Members
        public object Assemble(object cached, object owner)
        { return cached; }
        public object DeepCopy(object value)
        { return value; }
        public object Disassemble(object value)
        { return value; }
        public int GetHashCode(object x)
        { return x.GetHashCode(); }
        public bool IsMutable
        {
            get { return false; }
        }
        public new bool Equals(object x, object y)
        { return object.Equals(x, y); }
        public object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner)
        {
            int index0 = rs.GetOrdinal(names[0]);
            if (rs.IsDBNull(index0))
            { return null; }
            string description = rs.GetString(index0);
            return EnumExtension.EnumParseDescription<T>(description, true);
        }
        /// <summary>
        /// Use the enum value to get the description
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="value"></param>
        /// <param name="index"></param>
        public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index)
        {
            if (value == null)
            {
                ((IDbDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
            }
            else
            {
                T enumValue = (T)Enum.Parse(typeof(T), value.ToString());
                //((IDbDataParameter)cmd.Parameters[index]).Value = enumValue.GetKey();; //can't use cause don't know kind of type
                ((IDbDataParameter)cmd.Parameters[index]).Value = enumValue.GetDescription<T>();
                
            }
        }
        public object Replace(object original, object target, object owner)
        { return original; }
        public Type ReturnedType
        { get { return typeof(T); } }
        public global::NHibernate.SqlTypes.SqlType[] SqlTypes
        {
            //get { return new SqlType[] { SqlTypeFactory.GetString(4096) }; }
            get { return new SqlType[] { SqlTypeFactory.GetAnsiString(_length) }; }
        }

        #endregion
    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
United States United States
A biography in this little spot...sure.
I've worked at GTE HawaiianTel. I've worked at Nuclear Plants. I've worked at Abbott Labs. I've consulted to Ameritech Cellular. I've consulted to Zurich North America. I've consulted to International Truck and Engine. Right now, I've consulted to Wachovia Securities to help with various projects. I've been to SHCDirect and now at Cision.

During this time, I've used all kinds of tools of the trade. Keeping it to the more familier tools, I've used VB3 to VB.NET, ASP to ASP/JAVASCRIPT/XML to ASP.NET. Currently, I'm developing with C# and ASP.NET. I built reports in Access, Excel, Crystal Reports, and Business Objects (including the Universes and ETLS). Been a DBA on SQL Server 4.2 to 2000 and a DBA for Oracle. I've built OLTP databases and DataMarts. Heck, I've even done Documentum. I've been lucky to have created software for the single user to thousands of concurrent users.

I consider myself fortunate to have met many different people and worked in many environments. It's through these experiences I've learned the most.

Comments and Discussions