AutoMapper:地图接口抽象类 - 这可能吗? [英] AutoMapper: Map Interface to Abstract class - is this possible?

查看:163
本文介绍了AutoMapper:地图接口抽象类 - 这可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 AutoMapper 映射我的应用程序的不同层之间的对象。
在一边我有一个看起来像这样的接口:

I'm using AutoMapper to map objects between different layers of my application. On the one side I have an interface which looks like this:

public interface MyRepo 
{
    IEnumerable<IRootObject> GetItems(param);
}



IRootObject看起来是这样的:

IRootObject looks like this:

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



所以 GetItems()调用实际上只返回 IxxxSubObjectItems 的数组。
在另一侧的结构是这样的:

So the GetItems()call actually returns an array of only IxxxSubObjectItems. On the other side the structure looks like this:

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've set up the mapper like so

AutoMapper.Mapper.CreateMap<IRootObject, MyCustomRoot>
    .Include<IFirstSubObject, MyCustomFirstSub>
    .Include<ISecondSubObject, MyCustomSecondSub>();

AutoMapper.Mapper.CreateMap<IFirstSubObject, MyCustomFirstSub>();
AutoMapper.Mapper.CreateMap<ISecondSubObject, MyCustomSecondSub>();



不过,我不断收到MapperExceptions(不能构造抽象类,这是有道理的方式)。我还呼吁 AssertConfigurationIsValid ,而通过此代码。结果
。如果我不作 MyCustomRoot 类抽象,然后映射工作,但我得到 MyCustomRoot 对象的列表,而我其实想有 MyCustomxxxSub名单,因为有一个工厂以后,使用此类型生成正确的UI ...

But I keep getting MapperExceptions ("Cannot construct abstract class", which makes sense in a way). I also call AssertConfigurationIsValid, and that passes for this code.
If I don't make the MyCustomRoot class abstract, then the mapping works, but I get a list of MyCustomRoot objects, while I actually would like to have a list of MyCustomxxxSub, because there is a Factory later on which uses this type to generate the correct UI...

我希望有人能指出我正确的方向为这一个!结果
谢谢!

I hope someone can point me in the right direction for this one!
Thanks!

推荐答案

好吧,我发现了一种真正做到这一点!
因此,与上面所提供的接口和类,你可以做这样的事情结果
(记住: MyCustomRoot 是一个抽象类)

Ok I found a way to actually do this! So with the Interfaces and Classes provided above, you can do something like this
(Remember: the MyCustomRoot class is abstract)

AutoMapper.Mapper.CreateMap<IRootObject, MyCustomRoot>
    .Include<IFirstSubObject, MyCustomFirstSub>
    .Include<ISecondSubObject, MyCustomSecondSub>();



然后用followig地图调用对象的映射:

and then use the followig Map call to map the objects:

public List<MyCustomRoot> PerformMapping(List<IRootObject> rootObjects)
{
    var returnList = new List<CustomRoot>();
    foreach(var rootObject in rootObjects)
    {
        var abstractObject = (MyCustomRoot)Mapper.Map(rootObject, rootObject.GetType(), typeof(MyCustomRoot));
        returnList.Add(abstractObject);
    }
    return returnList;
}

现在我实际得到的数组 MyCustomRoot 的对象,这些都是具体的实现。

Now I actually get an array of MyCustomRoot objects, which are all the specific implementations.

这正是我一直在寻找!

这篇关于AutoMapper:地图接口抽象类 - 这可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆