Search code examples
nhibernatenhibernate-mapping-by-code

NHibernate mapping by code: Mapping userTypes by convention


I would like to know how to configure NHibernate "mapping by code" so that when I map a property of a certain type, it uses a certain userType to perform that mapping. It figures this out by convention.

For example, if I have an Account class with a property Currency of type Currency, then the NHibernate configuration should figure out (by convention) that it needs to use the CurrencyUserType to perform the mapping.

I'm unable to find the relevant documentation for this, so if such documentation does exist, then a few links will be appreciated too.

Note: This is not a FluentNHibernate question.


Solution

  • var mapper = new ConventionModelMapper();
    mapper.IsProperty((info, b) => b || info.GetPropertyOrFieldType() == typeof(Currency));
    mapper.BeforeMapProperty +=
           (inspector, member, customizer) =>
           {
               if (member.LocalMember.GetPropertyOrFieldType() == typeof(Currency))
                   customizer.Type<CurrencyUserType>();
           };