Here are my classes:
public class A
{
public int archive {get; set;}
}
public class B : A
{
public new bool archive {get; set;}
}
public class aDTO
{
public int archive {get; set;}
}
public class bDTO : aDTO
{
public new bool archive {get; set;}
}
Now, when I try to use AutoMapper to map class bDTO to class B it throws an exception. Here is my mapper code I am using.
IEnumerable<bDTO> myBDTOCollection = getCollection;
Mapper.CreateMap<bDTO, B>();
IEnumerable<B> BList = Mapper.Map<IEnumerable<bDTO>, IEnumerable<B>>(myBDTOCollection);
I dont know if I am missing something simple or if there is a better way to do this or what. Any input would be appreciated.
So after much deliberation and battling with AutoMapper I discovered the source of almost every issue I have run into so far.
Once your objects start to gain in complexity it becomes prudent that when mapping them you dont use the interface for the source and/or destination.
In some cases (like with my DTO’s) I was still able to use the Interface, but for the business logic objects AutoMapper prefered the concrete type to be used.
In reality, when I was creating maps, I was mapping the interfaces for my objects:
Mapper.CreateMap<IbDTO, IB>()
What fixed my issue is when I mapped the concrete type for the business object:
Mapper.CreateMap<IbDTO, B>()
This fixed my problem and every other problem I have been having with Auto Mapper so far.