Skip to content

Mapping Configurations

srkirkland edited this page Jan 26, 2011 · 2 revisions

Mapping Configurations

All mapping configurations require an XML configuration file (either in the web.config or an NHibernate.config file). This is required for any NHibernate project -- see NHibernate Session Configuration for more information.

Setting which mapping configuration is to be used should take place in the global.asax file, preferably in Application_Start (though you really just need to set the mapping configuration before the session factory is created)

Possible Mapping Configurations:

HBM Mapping (Default)

HBM Mapping (using XML files) is the default mapping strategy and requires no explicit configuration outside of the web.config (or NHibernate.config) file.

To explicitly set the mapping strategy to use Hbm mapping (again, not required currently), use the following code

NHibernateSessionConfiguration.Mappings.UseHbmMappings();

Fluent NHibernate Mapping

In order to use Fluent NHibernate Mappings, put the following in your application startup routine

NHibernateSessionConfiguration.Mappings.UseFluentMappings(assemblyWithFluentMappings);

See http://fluentnhibernate.org/ for details on using this mapping strategy

Auto Mapping

The fluent NHibernate project also has a technique called 'Auto Mapping', which "is a mechanism for automatically mapping all your entities based on a set of conventions." See http://wiki.fluentnhibernate.org/Auto_mapping for details.

Baked into UCD Architecture are a set of conventions used internally which include the following:

Note: These conventions can be easily overridden/modified using custom conventions

  • Has Many Convention: A foreign key is named with the entity + "ID". Ex: CustomerID
  • Primary Key Convention: All PKs are named "ID". Ex: Customers table PK would be ID
  • Reference Convention: The FK is named with with class name + "ID": Ex: CustomerID
  • Table Name Convention: All tables are the pluralization of your class name. Ex: Customer class maps to the Customers table
  • Many to Many Convention: Uses parentXChild as the table name. Ex: CustomersXOrders

In order to use Auto Mapping, put the following in your application startup routine.

//First generic is for a domain class, second for the a mapping class within the convention overrides assembly
NHibernateSessionConfiguration.Mappings.UseAutoMappings<Auto.Order, Auto.OrderMap>();

Custom Mapping and Creating your own conventions

Documentation Coming Soon

I'll write more soon, but the basic idea is to create your own custom class which inherits from AutoPersistenceModelGenerator and override whichever references you would like. Then you can use your custom class to create an IMappingConfiguration which would be passed in to the NHibernateSessionConfiguration.Mappings.UseCustomMapping() method. Some working sample code would be:

//Your custom persistence model generator which can override any conventions as desired
public class CustomPersistenceModelGenerator : AutoPersistenceModelGenerator
{
    public override void AddReferenceConvention(IConventionFinder conventionFinder)
    {
        conventionFinder.Add<MyReferenceConvention>();
    }
}

Now in your application startup set the NHibernateSessionConfiguration to use your custom mapping

var customModelMapper = new CustomPersistenceModelGenerator().GenerateFromAssembly<Conventions.Order, Conventions.OrderMap>();

NHibernateSessionConfiguration.Mappings.UseCustomMapping(AutoMappingConfiguration.CreateWithOverrides(customModelMapper));