Today Menu NHibernate User Type
Ok, today I faced a really simple problem, I have a domain class with a property of type uri, that seems to be not natively supported by NHibernate. Whenever you face a problem of this type the solution is IUserType, here is the full file, it is so simple to write a user type that I really does not check with google, after I write my class I found that other people had already created it, but it is worth to take a look to some particular piece of code.
|
|
The NullSafeGet is called to retrieve the value from database columns in a null safe way, first of all get the index of the data into resultset, then check if it is null, (if is null return null is ok) then if some data is contained into the resultset you can try to create the uri trapping the FormatException. If you prefer you can call Uri.TryCreate that probably is a better solution respect trapping the exception :D
|
|
The NullSafeSet has the duty to transform data from the original.net type (Uri) to something that can be stored in database, since I need to work only with AbsoluteUri, I store into the database the AbsoluteUri value or the Uri. Then we need to tell nhibernate the types involved
|
|
The ReturnedType tells nhibernate the.Net type handled by this UserType, the SqlTypes is used to tell nhibernate the structure of the data in the table, in this example a string is sufficient to store the AbsoluteUri and so I returned an array with only a string type.
alk.