AutoMapper:使用接口属性映射对象 [英] AutoMapper: Mapping objects with interface properties

查看:257
本文介绍了AutoMapper:使用接口属性映射对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的任务需要注意不同对象类型之间的映射,因此我认识到非常好的AutoMapper库.

my current task needs to pay attention on mapping between different object types and so I recognized the very nice AutoMapper library.

到目前为止很容易处理,但是这些不同的对象包含复杂的接口类型属性.让我向您展示一个代码段:

So far easy to handle but these different objects contains complex interface type properties. Let me show you a code snippet:

public inferface IInterface
{
    string TextProperty { get; set;}
}

public class A : IInterface
{
    string TextProperty { get; set; }
}

public class B : IInterface
{
    string TextProperty { get; set; }
}


public inferface IComplexInterface
{
    IInterface ComplexProperty { get; set; }
}

public class ComplexA : IComplexInterface
{
    IInterface ComplexProperty { get; set; }
}

public class ComplexB : IComplexInterface
{
    IInterface ComplexProperty { get; set; }
}

在我的情况下,类A可能映射到类B,反之亦然.
通过配置CreateMap<A, B>();
,从类型A映射到B没问题. 从类ComplexA映射到类ComplexB会引发异常:

In my case it is possible that class A is mapped to class B and vice versa.
Mapping from type A to B is no problem by configuring CreateMap<A, B>();
Mapping from class ComplexA to class ComplexB throws an exception:

错误映射类型.

Error mapping types.

映射类型:

  • ComplexA-> ComplexB
  • NamespaceOfComplexA.ComplexA-> NamespaceOfComplexB.ComplexB

类型映射配置:

  • ComplexA-> ComplexB
  • NamespaceOfComplexA.ComplexA-> NamespaceOfComplexB.ComplexB

属性:

  • ComplexProperty

我在stackoverflow上已经找到的一种可能的解决方案可能是如下配置:

A possible solution I already found here on stackoverflow could be a configuration as follows:

CreateMap<A, IInterface>().As<B>();
CreateMap<B, IInterface>().As<A>();

但是对于我来说,它不起作用.

But in my case it is not working.

有什么建议吗?

推荐答案

现在,我找到了适合我的解决方案.

Now, I found a solution that works for me.

我将AutoMapper与非通用方法结合使用,因此我通过以下方式进行配置:

I use AutoMapper with a non generic approach and so I configure via:

CreateMap(typeof(ComplexA), typeof(ComplexB))

要考虑具有复杂类型(如接口或抽象类)的属性,可以编写必须实现该接口的自己的ValueResolver:

To consider properties with complex types like interfaces or even abstract classes it is possible to write an own ValueResolver that has to implement the interface:

IValueResolver<object, object, object>

使用以下方法:

public object Resolve(object source, object destination, object destMember, ResolutionContext context)
{
//...
}

要解析接口/抽象类属性,可以通过使用ForMember(...)方法增强配置来配置类型,并为特定属性定义一个具体的ValueResolver,如下所示:

To resolve interface/abstract class properties you can configure your types by enhancing the configuration with the method ForMember(...) and define a conrete ValueResolver for the particular property as follows:

CreateMap(typeof(ComplexA), typeof(ComplexB)).ForMember("ComplexProperty", x => x.ResolveUsing(new ValueResolver(/*...*/)));

对于我来说,这是将接口属性映射到类定义的具体实现的解决方案.

In my case it was the solution to map the interface property to a concrete implementation of my class definitions.

希望它有用.

这篇关于AutoMapper:使用接口属性映射对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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