Simple scenario

If you just want to create a mapper that uses it all (convention, configuration, attributes, and also allows the addition of more mapping instructions through code), you can just use the standard repository behavior:

SourceClass source = new SourceClass();
source.Title = "Hello World!";
TargetClass target = new TargetClass();
var mapper = MappingRepository.Default.ResolveMapper<SourceClass, TargetClass>();
mapper.Map(source, target);


Using the repository offers better performance upon mapper reutilization. Most mappers require some processing (to discover mappings) when they are created and the repository keeps them in cache.

Using a specific mapper

Any of the following classes can be used directly, without the need for a repository or any additional complications:
  • ConfigurationMapper
  • AttributeMapper
  • ConventionMapper

// skipping creation and setup of the source and target objects
// ...

// using a convention mapper in this example. Any of the other classes listed above could be used in the same way.
ConventionMapper<SourceClass, TargetClass> mapper = new ConventionMapper<SourceClass, TargetClass>();
mapper.Map(source, target);

Using a multimapper

A composite mapper (AKA multimapper) is a special mapper that contains one or more inner mappers.

The code below shows a CompositeMapper object being created for a given source/target object combination, with two inner mappers, one based on convention and the other based on configuration.

The order in which the instances are provided will be the exact order in which the mappers are executed. This means that in this case the ConfigurationMapper may supersede mappings by the ConventionMapper.

CompositeMapper<SourceClass, TargetClass> mapper =
    new CompositeMapper<SourceClass, TargetClass>(
        new Core.IMapperBase<SourceClass, TargetClass>[] 
        {
            new ConventionMapper<SourceClass, TargetClass>(),
            new ConfigurationMapper<SourceClass, TargetClass>()
        }
        );
mapper.Map(source, target);

Adding manual mappings

In case the automated mappings do not do the trick, a ManualMapper can be used:
SourceClass source = new SourceClass();
source.Title = "Hello World!";
TargetClass target = new TargetClass();
ManualMapper<SourceClass, TargetClass> mapper = new ManualMapper<SourceClass, TargetClass>();
mapper.AddMapping("Title", "Title", (s, t) => t.Title = "From: " + s.Title);
mapper.Map(source, target);


In case the manual mapper is contained within a composite mapper (all mappers created with the Default repository are), manual mapping can be used to just cover the scenarios the other mappers would leave untouched.


SourceClass source = new SourceClass();
source.Title = "Hello World!";
TargetClass target = new TargetClass();
var mapper = MappingRepository.Default.ResolveMapper<SourceClass, TargetClass>();
mapper.AddMapping("CustomerFirstName, CustomerLastName", "Customer.FullName", (s, t) => t.Customer = new Customer() { FullName = s.CustomerFirstName + s.CustomerLastName } );
mapper.Map(source, target);

Last edited Apr 26, 2012 at 5:37 PM by dlucas, version 8

Comments

No comments yet.