Fluent NHibernate - convention to apply custom type
I'm using Fluent NHibernate (Auto Mapping) to map my domain objects to database tables. I've created my own 'Date' class which I map using 'DateTranslator' - an implementation of NHibernate's 'IUserType'.
public class MyDomainObject : DomainObject
{
public Date Date { get; set; }
}
public class MyDomainObjectMappingOverride : IAutoMappingOverride<MyDomainObject>
{
public void Override(AutoMapping<MyDomainObject> mapping)
{
mapping.Map(x => x.Date).CustomType(typeof(DateTranslator));
}
}
As you can see - I've created a mapping override for this domain object so that I can specify the custom type that should be used to map the 'Date' property.
Now - this means that I'll have to create a mapping override for 开发者_Python百科all domain objects that contain a property of type 'Date'.
I'd like to use a convention here so that 'DateTranslator' will be used to map all properties of type 'Date' but I've been unable to figure it out.
Any help with this would be greatly appreciated.
public class MyUsertypeConvention : IPropertyConvention
{
public void Apply(IPropertyInstance instance)
{
if (instance.Type.Name == "Date")
//or
//if (instance.Type.GetUnderlyingSystemType() == typeof(Date))
instance.CustomType<DateTranslator>();
}
}
and config for example
FluentMappings.Conventions.Add(new MyUsertypeConvention())
精彩评论