Skip to content
This repository has been archived by the owner on Dec 29, 2020. It is now read-only.

Convention Based Mapping

Trevor Pilley edited this page Jan 12, 2019 · 5 revisions

The default Mapping Convention that MicroLite offers is Convention Based Mapping. Convention Based Mapping offers a quick and easy way to define the mapping relationship between a class and a table and is the recommended approach. The default conventions are as follows:

  • The class name is the singular of the table name (e.g. public class Customer -> TABLE [Customers])
  • The property mapped as the identifier must be called either Id or {ClassName}Id
  • The identifier values are generated by the database (Identity or AutoIncrement)
  • The property is mapped if it has a public get and set
  • The column name and property name are the same, unless the property type is an enum in which case the column name should be {EnumTypeName}Id
  • The column value can be inserted
  • The column value can be updated

Configuration

The Convention Based Mapping is included in the main MicroLite assembly so you don't need to install an additional package. You only need to explicitly specify it in your application if you want to alter the conventions used:

// Do this before calling Configure.Fluently()...
Configure.Extensions()
    .WithConventionBasedMapping(
        new ConventionMappingSettings
        {
           ...
        });

Customizing Conventions

The ConventionMappingSettings class has a number of properties defined:

public Func<PropertyInfo, bool> AllowInsert { get; set; }
public Func<PropertyInfo, bool> AllowUpdate { get; set; }
public Func<PropertyInfo, bool> Ignore { get; set; }
public Func<PropertyInfo, bool> IsIdentifier { get; set; }
public Func<PropertyInfo, string> ResolveColumnName { get; set; }
public Func<PropertyInfo, DbType> ResolveDbType { get; set; }
public Func<PropertyInfo, string> ResolveIdentifierColumnName { get; set; }
public Func<Type, IdentifierStrategy> ResolveIdentifierStrategy { get; set; }
public Func<PropertyInfo, string> ResolveSequenceName { get; set; }
public Func<Type, string> ResolveTableName { get; set; }
public Func<Type, string> ResolveTableSchema { get; set; }

If you want to replace the default function, simply specify a replacement when setting the ConventionMappingSettings. For example if we have some auditing and don't want to update the value of any property which contains a created date we could add our own AllowUpdate method:

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

You do not have to specify a replacement for all functions, just the ones which define the convention you want to change. You can see the default implementations Here.

Inflection

There are 2 further properties on ConventionMappingSettings which control the the change between the singular and plural of words.

public IInflectionService InflectionService { get; set; }
public bool UsePluralClassNameForTableName { get; set; }

By default, the convention is that tables are plural, classes are singular. If you want public class Customer and TABLE [Customer] instead then simply set UsePluralClassNameForTableName to false:

Configure.Extensions()
    .WithConventionBasedMapping(
        new ConventionMappingSettings
        {
            ...
            UsePluralClassNameForTableName = false
        });

An IInflectionService is supplied in the MicroLite assembly for the English language to turn singular words to plural. If you are not a native English language developer, you can specify your own IInflectionService implementation to handle the conversion if you wish.

e.g. The English InflectionService which ships with MicroLite will turn Client into Clients. If you are an Italian speaker for example, you would perhaps want to use public class Cliente however in Italian the plural is Clienti.

See the posts tagged Mapping on the blog for further details.