我有一个遗留数据库,我正在使用Nhibernate进行映射.在几个位置,列表och strigs或域对象被映射为数据库中的分隔字符串.值类型情况下的'string | string | string'和引用类型情况下的'domainID | domainID | domainID'.
我知道我可以在类上创建一个虚拟属性并映射到那些字段,但我想以更干净的方式进行,比如将Enums作为其字符串表示形式与EnumStringType类进行映射.
是IUserType的方式吗?
在此先感谢/约翰
我用这个:
public class DelimitedList : IUserType { private const string delimiter = "|"; public new bool Equals(object x, object y) { return object.Equals(x, y); } public int GetHashCode(object x) { return x.GetHashCode(); } public object NullSafeGet(IDataReader rs, string[] names, object owner) { var r = rs[names[0]]; return r == DBNull.Value ? new List() : ((string)r).SplitAndTrim(new [] { delimiter }); } public void NullSafeSet(IDbCommand cmd, object value, int index) { object paramVal = DBNull.Value; if (value != null) { paramVal = ((IEnumerable )value).Join(delimiter); } var parameter = (IDataParameter)cmd.Parameters[index]; parameter.Value = paramVal; } public object DeepCopy(object value) { return value; } public object Replace(object original, object target, object owner) { return original; } public object Assemble(object cached, object owner) { return cached; } public object Disassemble(object value) { return value; } public SqlType[] SqlTypes { get { return new SqlType[] { new StringSqlType() }; } } public Type ReturnedType { get { return typeof(IList ); } } public bool IsMutable { get { return false; } } }
SplitAndTrim是我自己的字符串扩展.然后在类中(使用ActiveRecord进行映射):
[Property(ColumnType = "My.Common.Repository.UserTypes.DelimitedList, My.Common.Repository")] public virtual IListFooBar { get; set; }