I use AutoMapper to map objects between different layers of my application. On the one hand, I have an interface that looks like this:
public interface MyRepo { IEnumerable<IRootObject> GetItems(param); }
IRootObject is as follows:
public interface IRootObject { int Id { get; set; } DateTime? Date { get; set; } } public interface IFirstSubObject : IRootObject { string MyFirstProp { get; set; } } public interface ISecondSubObject : IRootObject { string MySecondProp { get; set; } }
So calling GetItems() actually returns an array of only IxxxSubObjectItems . On the other hand, the structure is as follows:
public abstract class MyCustomRoot { protected MyCustomRoot(){} public int Id { get; set; } public DateTime? Date { get; set; } } public class MyCustomFirstSub : MyCustomRoot { public MyCustomFirstSub() : base() {} public string MyFirstProp { get; set; } } public class MyCustomSecondSub : MyCustomRoot { public MyCustomSecondSub () : base() {} public string MySecondProp { get; set; } }
Now I configured mapper like this
AutoMapper.Mapper.CreateMap<IRootObject, MyCustomRoot> .Include<IFirstSubObject, MyCustomFirstSub> .Include<ISecondSubObject, MyCustomSecondSub>(); AutoMapper.Mapper.CreateMap<IFirstSubObject, MyCustomFirstSub>(); AutoMapper.Mapper.CreateMap<ISecondSubObject, MyCustomSecondSub>();
But I keep getting MapperExceptions ("Cannot construct an abstract class", which makes sense in some way). I also call AssertConfigurationIsValid and this passes this code.
If I do not do an abstract abstraction of MyCustomRoot , then the mapping works, but I get a list of MyCustomRoot objects, while I really would like to have a list of MyCustomxxxSub , because there is a Factory later on which this type is used to generate the correct user interface ...
Hope someone can point me in the right direction for this! Thank you