John Livermore
John Livermore

Reputation: 31313

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?

Upvotes: 1

Views: 89

Answers (1)

Omu
Omu

Reputation: 71198

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;
    });

Upvotes: 1

Related Questions