Unless you need to do something special, UriType
provided by NHibernate will work (I don't know what version it was introduced - I am using 3.1.4000). No need to write a custom user type.
ClassMap
You can specify UriType
in a ClassMap<>
:
public class ImportedWebImageMap : ClassMap<ImportedWebImage>
{
public ImportedWebImageMap()
{
Id(x => x.Id);
Map(x => x.SourceUri).CustomType<UriType>();
}
}
Property Convention
You can use a property convention to map all Uri
properties to use UriType
:
public class UriConvention : IPropertyConvention
{
public void Apply(IPropertyInstance instance)
{
if (typeof(Uri).IsAssignableFrom(instance.Property.PropertyType))
instance.CustomType<UriType>();
}
}
Storing as varchar
If you want to store the Uri
in the database as varchar
rather than the default nvarchar
you can create a custom type that derives from UriType
and specifies the AnsiString
SQL type:
public class UriAnsiStringType : UriType
{
public UriAnsiStringType()
: base(new AnsiStringSqlType())
{ }
public override string Name
{
get { return "UriAnsiStringType"; }
}
}