NHibernate mapping for System.Drawing.Color

Is it possible to just do some type conversion and map it directly to System.Drawing.Color? I save colors as html / css values. those. #ffffff. I don't want to create a custom type that implements IUserType, it's just a wrapper for System.Drawing.Color.

+3
source share
4 answers

Try this for size. The custom NHibernate type does not replace the type you want to open, it simply provides a mechanism for automatically matching from a stored database type to a .NET type (here, from row to color and vice versa).

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; }
    }
}

Then the following mapping should work:

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

, FluentNHibernate, :

Map(m => m.Color).CustomTypeIs<ColorUserType>();
+6

:

/ , .

, , getter , /, , string/ , .

public class MyEntity
{

    private string htmlColorString;

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

}
0

:

hex RGB - RGB - # 23FF00 R = 23, G = FF, B = 00.

This will give you an int value for each of the RGB components after you parse the string by the hex value:

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

After that, just call Color.FromArgb () static, and you will have your own color.

0
source

Source: https://habr.com/ru/post/1711587/


All Articles