I have a domain object that has a couple of Uri properties that need to be persisted to a Urls table that has several parts of the Uri from which the properties will need to be reassembled after retrieval. I can store the properties well enough, but how do I tell NHibernate how to recreate the Uri property? You can't set most of the Uri object's properties directly. Rather, the url must be assembled and passed to a Uri constructor.
+2
A:
Personally I do not find the Uri type to be super useful but there are 2 options that I can think of:
- Create a new user type by implementing IUserType (see http://intellect.dk/post/Implementing-custom-types-in-nHibernate.aspx - search for the Uri implementation it's about half way down)
- Create a protected string property that is mapped in NHibernate and wrap a public Uri around it.
UPDATE
I haven't played with this but given that you are looking to map the properties of the Uri I think you want to look at implementing the ICompositeUserType instead of IUserType. See this post for an idea (not based on Uri but could be good for pointers) http://www.lostechies.com/blogs/rhouston/archive/2008/03/23/mapping-timestamp-data-using-nhibernate-s-icompositeusertype.aspx
Michael Gattuso
2009-12-08 14:28:16
Neither solution works, because I need the individual parts of the URL stored as columns in the database for later analysis.
Chris
2009-12-08 15:03:18
Ah, I missed that part, sorry. Is the intent then to retrieve or search based on a segment of the uri? I.e. from obj o where o.Uri.Host == 'foo'?
Michael Gattuso
2009-12-08 15:45:51