代码之家  ›  专栏  ›  技术社区  ›  Josh Close

NHibernate映射到系统。绘图。颜色

  •  3
  • Josh Close  · 技术社区  · 16 年前

    是否有可能只进行某种类型转换并直接映射到系统。绘图。颜色?我将颜色存储为html/css值。即#fffff。我不想创建一个实现IUserType的自定义类型,它只是System的包装器。绘图。颜色。

    4 回复  |  直到 16 年前
        1
  •  6
  •   David M    16 年前

    public class ColorUserType : IUserType
    {
        public bool Equals(object x, object y)
        {
            if (ReferenceEquals(x, y)) return true;
            if (x == null || y == null) return false;
            return x.Equals(y);
        }
    
        public int GetHashCode(object x)
        {
            return x == null ? typeof(Color).GetHashCode() + 473 : x.GetHashCode();
        }
    
        public object NullSafeGet(IDataReader rs, string[] names, object owner)
        {
            var obj = NHibernateUtil.String.NullSafeGet(rs, names[0]);
            if (obj == null) return null;
            var colorString = (string)obj;
            return ColorTranslator.FromHtml(colorString);
        }
    
        public void NullSafeSet(IDbCommand cmd, object value, int index)
        {
            if (value == null)
            {
                ((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
            }
            else
            {
                ((IDataParameter)cmd.Parameters[index]).Value = ColorTranslator.ToHtml((Color)value);
            }
    
        }
    
        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[] {new SqlType(DbType.StringFixedLength)}; }
        }
    
        public Type ReturnedType
        {
            get { return typeof(Color); }
        }
    
        public bool IsMutable
        {
            get { return true; }
        }
    }
    

    <property
        name="Color"
        column="hex_color"
        type="YourNamespace.ColorUserType, YourAssembly" />
    

    Map(m => m.Color).CustomTypeIs<ColorUserType>();
    
        2
  •  2
  •   Andrew Burns    16 年前

    我会花15分钟写一个IUserType实现来直接转换颜色属性,这样你就不会有任何神奇的属性了。

    http://www.lostechies.com/blogs/rhouston/archive/2008/03/23/mapping-strings-to-booleans-using-nhibernate-s-iusertype.aspx

    这也有一个好处,即您可以在HQL或Linq中使用您的颜色属性,而使用魔术属性则无法做到这一点,尽管使用颜色可能不是问题。

        3
  •  0
  •   Adam Lear    16 年前

    我将采用Frederik的实施方法,并按如下方式进行转换:

    将十六进制转换为RGB——每对十六进制值都是RGB分量之一——#23FF00表示R=23,G=FF,B=00。

    int.Parse("FF", System.Globalization.NumberStyles.AllowHexSpecifier);
    

    之后,只需调用Color即可。从Argb()静态,你将得到你的颜色。

        4
  •  0
  •   Frederik Gheysels    16 年前

    然后,我会在类中创建一个公共属性,返回一个Color,在该属性的getter中,我会将存储在私有字段/属性中的字符串转换为Color,在setter中,我将字符串字段/属性设置为与给定的Color值对应的值。

    public class MyEntity
    {
    
        private string htmlColorString;
    
        public Color TheColor
        {
             get { return System.Drawing.ColorTranslator.FromHtml (htmlColorString); }
             set 
             {
                  htmlColorString = System.Drawing.ColorTranslator.ToHtml(value);
             }
        }
    
    }