Thank you very mauch for your halp! We are very apprishiate this. We
prefere clean way with IUserType. We have found some difference
between YesNo type in shared example and IPAddress type because of
refference mutable nature of IPAddress type. We are sharing our
version of IPAddressType and ask audience to point out our errors.
Thanks!

class IPAddressType : NHibernate.UserTypes.IUserType
    {
        public bool IsMutable
        {
            get { return true; }
        }

        public Type ReturnedType
        {
            get { return typeof(System.Net.IPAddress); }
        }

        public NHibernate.SqlTypes.SqlType[] SqlTypes
        {
            get { return new[]
{ NHibernate.NHibernateUtil.String.SqlType }; }
        }

        public object NullSafeGet(System.Data.IDataReader rs, string[]
names, object owner)
        {
            var ADDRESS =
NHibernate.NHibernateUtil.String.NullSafeGet(rs, names[0]);
            if (ADDRESS == null)
                return null;
            try
            {
                return System.Net.IPAddress.Parse((string)ADDRESS);
            }
            catch (FormatException ex)
            {
                throw new
NHibernate.Exceptions.DataException(String.Format("Expected data to be
IP Address but was '{0}'.", ADDRESS), ex);
            }
        }

        public void NullSafeSet(System.Data.IDbCommand cmd, object
value, int index)
        {
            if (value == null)
            {
 
((System.Data.IDataParameter)cmd.Parameters[index]).Value =
DBNull.Value;
            }
            else
            {
 
((System.Data.IDataParameter)cmd.Parameters[index]).Value =
((System.Net.IPAddress)value).ToString();
            }
        }

        public object DeepCopy(object value)
        {
            if (value == null)
                return null;
            else
                return new
System.Net.IPAddress(((System.Net.IPAddress)value).GetAddressBytes());
        }

        public object Replace(object original, object target, object
owner)
        {
            return DeepCopy(original);
        }

        public object Assemble(object cached, object owner)
        {
            return DeepCopy(cached);
        }

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

        public new bool Equals(object x, object y)
        {
            if (ReferenceEquals(x, y)) return true;

            if (x == null && y == null) return true;

            if (x == null || y == null) return false;

            return x.ToString()== y.ToString();
        }

        public int GetHashCode(object x)
        {
            return x == null ?
typeof(System.Net.IPAddress).GetHashCode() + 473 : x.GetHashCode();
        }
    }

On Sep 13, 5:44 pm, kor <[email protected]> wrote:
> if you don't use it in queries (for example to do a query to load all
> the classes with an ip address that begin wtih 172) use
> type="serializable" (it will be saved in a blob column).
>
> otherwise save as string/varchar, the iusertpe implementation is
> simple, for example see 
> thishttp://www.lostechies.com/blogs/rhouston/archive/2008/03/23/mapping-s...
>
> for the convertions ipaddress <---> string
> uses ipaddress .ToString()  and IPAddress.Parse

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to