Extending the generated mapping files

The generated mapping files can be extended using two ways: Hbm.xml files can be extended using user code regions and FluentNHibernate mapping files can be extended by using a partial class and implement a partial method. This section describes both methods.

Extending Hbm.xml files using user code regions

User code regions are regions within the generated file which are marked with a begin marker and an end marker and which contents are preserved across code generation cycles. This in short means that whatever you place between these begin / end markers will be copied to the new version of the hbm.xml file if you re-generate the code in LLBLGen Pro.

These regions are ideal for extending the generated XML inside the generated hbm.xml files, for example to add additional mapping information or custom mappings you couldn't add within the LLBLGen Pro designer. User code regions have a unique name within the file they're placed in and are created by a command in the template used to produce the hbm.xml file.

This means that you can't change the name of the region nor can you add your own in the generated code: if you want to add a new user code region to the hbm.xml file, you have to add the statement to produce the region to the hbm.xml template. See the LLBLGen Pro SDK documentation, the Lpt templates engine, for details about user code region production in templates.

A user code region in an hbm.xml file looks this:

<!-- __LLBLGENPRO_USER_CODE_REGION_START UserCodeRegionName -->
<!-- __LLBLGENPRO_USER_CODE_REGION_END --> 

The UserCodeRegionName is the name of the user code region, which is unique within the file. The start marker is the entire line the __LLBLGENPRO_USER_CODE_REGION_START token is placed on. The end marker is the entire line the __LLBLGENPRO_USER_CODE_REGION_END marker is placed on. To add code, in this case XML, to the region, open the hbm.xml file in a text editor, e.g. drag it onto the LLBLGen Pro designer or open it in the XML editor in VS.NET, and simply place the custom XML you want to add between the two marker lines, like in the following example:

<!-- __LLBLGENPRO_USER_CODE_REGION_START EntityMappingCustomCode -->
    <many-to-one name="Shipper" access="field.camelcase-underscore" fetch="select" cascade="all">
        <column name="ShipVia"/>
    </many-to-one>
<!-- __LLBLGENPRO_USER_CODE_REGION_END -->

Available user code regions for hbm.xml files

The following user code regions are available and where they're located.

  • EntityMappingCustomCode. Located at the bottom of the hbm.xml file for an entity.
  • TypedViewMappingCustomCode. Located at the bottom of the hbm.xml file for a typed view.

Extending FluentNHibernate mapping files using partial methods

Extending the mapping files generated for FluentNHibernate can be done by implementing the partial method void AdditionalMappingInfo(void) which is called at the end of a mapping declaration in the constructor of the FluentNHibernate mapping file.

To extend a generated mapping file for FluentNHibernate, create a partial class for the mapping class and implement the partial method with the additional mapping code. If you don't implement the partial method, it's compiled away by the C# or VB.NET compiler.