Mapping System.Version with NHibernate 1.2

I have an object that uses System.Version as a property. I want this object to appear in my table, saving the version as a string. What is the best way to do this with NHibernate v1.2?

public class MyClass
{
  public Version MyVersion {get; set;}
}

not sure what to do with the appropriate display

<property name = "MyVersion" column = "MyVersion" not-null = "true" />

this gives me errors saying "Invalid GetBytes attempt in column" MyVersion0_0_ ". The GetBytes function can only be used for columns of type Text, NText or Image." If I use type = "string" on the map, I get pronunciation errors.

offers?

+3
source share
4 answers

You will need to write an NHibernate user type that returns a value of a property of type Version but is saved as a string. There is an implementation of a custom skeleton type type here that takes care of some work for you.

+4
source

David M's suggestion and article link were great. Just in case, if someone has problems getting this site (I had to download it from the Google cache), here is my solution:


    public abstract class BaseImmutableUserType : IUserType
    {
        public abstract object NullSafeGet(IDataReader rs, string[] names, object owner);
        public abstract void NullSafeSet(IDbCommand cmd, object value, int index);
        public abstract SqlType[] SqlTypes { get; }

        public new 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.GetHashCode();
        }

        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 DeepCopy(cached);
        }

        public object Disassemble(object value)
        {
            return DeepCopy(value);
        }

        public Type ReturnedType
        {
            get { return typeof(T); }
        }

        public bool IsMutable
        {
            get { return false; }
        }
    }

    public class VersionUserType: BaseImmutableUserType
    {

        public override SqlType[] SqlTypes 
        { 
            get
            {
                return new SqlType[]{new StringSqlType()};
            } 
        }

        public override object NullSafeGet(IDataReader rs, string[] names, object owner)
        {
            Version version = null;
            var value = NHibernateUtil.String.NullSafeGet(rs, names) as string;
            if (value != null)
                version = new Version(value);
            return version;
        }

        public override void NullSafeSet(IDbCommand cmd, object value, int index)
        {
            object valueToSet;
            Version version = value as Version;
            if (version != null)
            {
                valueToSet = version.ToString();
            }
            else
            {
                valueToSet = DBNull.Value;
            }

            NHibernateUtil.String.NullSafeSet(cmd, valueToSet, index);
        }
    }

and the mapping file now has this property:

<property name = "MyVersion" column = "MyVersion" not-null = "true" type = "My.Namespace.VersionUserType, My.Assembly" />

+2
source

. , , System.Version , , ToString(), , NHibernate string, Facade, System.Version, :

public System.Version Version
{
    get { return new System.Version(_versionAsString); }
    set { _versionAsString = value.ToString(); }
}

, . , , , , .

+2

, , .

Alternatively, you can convert the property to a string and just use this. I'm not sure how great the variability is in your model.

0
source

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


All Articles