MicroLite 4.0 – Convention Based Mapping Enhancements

In MicroLite 3.0, we introduced convention based mapping and with MicroLite 4.0 we are changing to make convention based mapping the default. Also, a number of enhancements have been made to allow your conventions to be specified.

The ConventionMappingSettings class now has a number of extra properties which can be configured to define your own conventions. The default values for these are backwards compatible with the conventions defined in MicroLite 3.0.

AllowInsert

The AllowInsert property is a Func<PropertyInfo, bool> which can be used to specify whether a property value can be inserted (the equivalent of setting AllowInsert = false on the ColumnAttribute if you were using attribute mapping). By default, this method will always return true, however if you had an audit property with updated date, you could configure the settings as follows:

Configure.Extensions().WithConventionBasedMapping(
    new ConventionMappingSettings
    {
        AllowInsert = (PropertyInfo propertyInfo) => 
        {
            return propertyInfo.Name != "Updated"; 
        }
    });

AllowUpdate

The AllowUpdate property defines a Func<PropertyInfo, bool> which can be used to specify whether a property value can be updated after it is first inserted (the equivalent of setting AllowUpdate = false on the ColumnAttribute if you were using attribute mapping). By default, this method will always return true, however if you had an audit property with created date which should not be changed, you could configure the settings as follows:

Configure.Extensions().WithConventionBasedMapping(
    new ConventionMappingSettings
    {
        AllowUpdate = (PropertyInfo propertyInfo) => 
        {
            return propertyInfo.Name != "Created"; 
        }
    });

Ignore

The Ignore property defines a Func<PropertyInfo, bool> which can be used to specify whether a property which would ordinarily be mapped should be ignored (the equivalent of not applying a ColumnAttribute if you were using attribute mapping). By default, this method will always return false.

IsIdentifier

The IsIdentifier property defines a Func<PropertyInfo, bool> which can be used to specify whether a property is the property mapped to the identifier column in the database table. By default, this method will return true if the property name is Id or {ClassName} + Id.

ResolveColumnName

The ResolveColumnName property defines a Func<PropertyInfo, string> which can be used to resolve the name of the column a property is mapped to. By default, the property name will be used unless the property is an enum in which case {EnumType} + Id will be used.

Example public string Forename would resolve to Forename whereas public CustomerStatus CustomerStatus would resolve to CustomerStatusId.

ResolveIdentifierColumnName

The ResolveIdentifierColumnName property defines a Func<PropertyInfo, string> which can be used to resolve the name of the column the identifier property is mapped to. This function will only be invoked if the IsIdentifier function returns true. By default, this will return the property name.

Example public int Id would resolve to Id so the expected column name in the database would be Id.

If your database schema defines the identifier column as TableName + Id (e.g. CustomerId) but in your code you want to use public int Id, you could implement the method as follows:

Configure.Extensions().WithConventionBasedMapping(
    new ConventionMappingSettings
    {
        ResolveIdentifierColumnName = (PropertyInfo propertyInfo) => 
        {
            return propertyInfo.DeclaringType.Name + "Id"; 
        }
    });
Advertisements

2 thoughts on “MicroLite 4.0 – Convention Based Mapping Enhancements

  1. Pingback: MicroLite Comparison Feedback | MicroLite ORM

  2. Pingback: MicroLite 5.0 – Changes to Convention Mapping | MicroLite ORM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s