Search code examples
valueinjecter

using a factory to create new objects in ValueInjecter


When using the format...

var customerInput = Mapper.Map<CustomerInput>(customer); 

A CustomerInput is created using Activator.CreateInstance. I would think there should be a way to use a factory to create these objects. So we would like to...

var customerInput = Mapper.Map<ICustomerInput>(customer); 

...where we could use a factory to map ICustomerInput to a "new" CustomerInput.

Is there a way to do this with ValueInjecter?


Solution

  • you can use the "additional parameters" feature for this:

    var customer = Mapper.Map<Customer>(foo, new Customer { ... });
    

    you can use this parameter in AddMap like this:

    Mapper.AddMap<Foo, Customer>((src, tag) =>
        {
            var res = (Customer)tag;
            res.InjectFrom(src);
            res.A = src.B + src.C; 
    
            ...
            return res;
        });