Skip to content

Customisation through the fluent api

Isaac Abraham edited this page Jan 26, 2016 · 14 revisions

###Overview The fluent API allows you to have all of your customisations placed in one (or many) classes that return a collection of customisations. This class must simply implement the IAutomapperConfigProvider interface; when you call the Automapper, as long as the provider is in the set of types / assemblies that you supply, the provider will be called.

class MyAutomapperConfigProvider : IAutomapperConfigProvider
{
    public AutomapperConfig CreateConfiguration() {
        return AutomapperConfig.Create()
                               .AndUseMultimappingFor(typeof(ICommand))
                               .AndUsePolicyInjectionFor(typeof(ICommand))
                               .AndUseNamedMappingFor(typeof(BackupCommand), "Backup")
                               .AndUseNamedMappingFor(typeof(CloseApplicationCommand), "Quit");
    }
}

The config above mirrors that of the attribute customisation sample, so you can compare and contrast the two.

Typically I would recommend that you create one mapping per assembly, although you can of course elect to make a single file for your entire solution, assuming that the assembly that this file resides in has a reference to all other assemblies.

###Benefits###

  • All mappings for e.g. an assembly are stored in a single class; easy to find them all.
  • Decoupled from your application types.

###Drawbacks

  • More code-heavy than using attributes
  • Possibly harder to read at-a-glance than attributes.