I don't think this is possible, but it's worth the question, I suppose.
I have the following types that share an interface (I promise, this isn't the same question I've asked before).
public interface ICustomer;
public class CustomerBO : ICustomer
public class CustomerSO : ICustomer // SO is Service Object in this case.
Then, I have the following mapping:
Mapper.Map<ICustomer, ICustomer>();
Now, here's where it gets interesting / confusing.
This works:
Mapper.Map<ICustomer, ICustomer>(customerSO, new CustomerBO);
This doesn't work:
Mapper.Map(customerSO, new CustomerBO());
Now, normally I wouldn't have a problem with just typing in the first Map statement with the two interface type defined, but my problem is when the Customer object is buried somewhere.
public class CustomerOrderDTO
{
ICustomer customer;
}
public class CustomerOrderSO
{
CustomerSO customer;
}
Mapper.Map<CustomerOrderDTO, CustomerOrderSO>();
This doesn't work, because there's no mapping from ICustomer to CustomerSO, so config assertion fails.
Currently, I'm going around the issue by doing this:
Mapper.CreateMap<CustomerOrderDTO, CustomerOrderSO>()
.ForMember(desc => dest.customer
, exp => exp.MapFrom(src => Mapper.Map<ICustomer, ICustomer>(src.customer
, new CustomerSO));
However, I would have to do this for every DTO-type object that we have, and then quite possibly have a cascading effect.
I understand that technically I could do the following to resolve the issue:
Mapper.Map<CustomerBO, CustomerSO>();
However, in CustomerBO there are a lot of other properties used in the business logic not in the interface. Similarly, there are a lot of properties in CustomerSO not in the interface. If I were to go with the above route, I would have a ton of Ignore() calls, and I'd have to map CustomerBO to CustomerSO, and then CustomerSO to CustomerBO, each with their own unique list of Ignore calls. Using the interfaces removes the need for the Ignore calls, as the data that I want to be visible from one to the other is defined in the interface.
So, in short, my question is this: is there some way I can tell AutoMapper to use the interface map when it encounters one of the implementing classes? Failing that, is there some other (read: better) way than a Map call in a MapFrom delegate to enforce my interface-to-interface mapping in a as-needed basis?
CustomerOrderDTO
should contain a dedicatedCustomerDTO
, not an interface. Well - my opinion :). – Mychaelclass MyDTO : Imydto { }
andinterface Imydto { Imyprop myprop {get;set;} }
, thenMyDTO
MUST containpublic Imyprop myprop {get;set;}
. – Leflore