-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Mapping System.Uri
PostPosted: Sun Mar 16, 2008 9:37 pm 
Newbie

Joined: Thu Mar 09, 2006 12:31 am
Posts: 8
Hi-

What's the easiest way to map an instance of System.Uri to a varchar column in my DB? I notice it isn't on the list of types supported right out of the box by NHibernate. Do I need to write my own mapping type?

Regards-
Eric


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 17, 2008 9:25 am 
Regular
Regular

Joined: Wed Jan 25, 2006 1:11 am
Posts: 118
Location: Copenhagen, Denmark
Implement IUserType, this is some code i made but never used so i can't be sure its working but give it a shot:

Code:
public class UriType : IUserType
    {
        public object NullSafeGet(IDataReader rs, string[] names, object owner)
        {
            //TODO: Handle badly formatted URI's
            Uri result = new Uri((string)NHibernateUtil.String.NullSafeGet(rs, names[0]));
            return result;
        }

        public void NullSafeSet(IDbCommand cmd, object value, int index)
        {
            if (value == null)
            {
                NHibernateUtil.String.NullSafeSet(cmd, null, index);
                return;
            }

            value = ((Uri)value).ToString();

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

        public object DeepCopy(object value)
        {
            if (value == null) return null;
            return new Uri(value.ToString());
        }

        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 SqlType[] SqlTypes
        {
            get
            {
                SqlType[] types = new SqlType[1];
                types[0] = new SqlType(DbType.String);
                return types;
            }
        }

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

        public bool IsMutable
        {
            get { return false; }
        }

        public bool Equals(object x, object y)
        {
            if (x == null || y == null) return false;
            return x.ToString().Equals(y.ToString());
        }

        public int GetHashCode(object x)
        {
            return x.GetHashCode();
        }
    }


You can now map:

Code:
public Uri MyUri{ get; set; }


with:

Code:
<property name="MyUri" type="Your.Namespace.UriType , Your.Assembly" />


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.