配置AutoMapper映射到具体的类型,但允许接口在我班上的定义 [英] Configure AutoMapper to map to concrete types but allow Interfaces in the definition of my class

查看:174
本文介绍了配置AutoMapper映射到具体的类型,但允许接口在我班上的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些code,它类似于下面是什么。基本上,它重新presents获取数据从一个Web服务,将其转换成客户端对象。

I have some code that is similar to what is below. Basically it represents getting data from a web service and converting it into client side objects.

void Main()
{
    Mapper.CreateMap<SomethingFromWebService, Something>();    
    Mapper.CreateMap<HasSomethingFromWebService, HasSomething>(); 
    // Service side
    var hasSomethingFromWeb = new HasSomethingFromWebService();
    hasSomethingFromWeb.Something = new SomethingFromWebService
            { Name = "Whilly B. Goode" };
    // Client Side                
    HasSomething hasSomething=Mapper.Map<HasSomething>(hasSomethingFromWeb);  
}    
// Client side objects
public interface ISomething
{
    string Name {get; set;}
}    
public class Something : ISomething
{
    public string Name {get; set;}
}    
public class HasSomething
{
    public ISomething Something {get; set;}
}    
// Server side objects
public class SomethingFromWebService
{
    public string Name {get; set;}
}    
public class HasSomethingFromWebService
{
    public SomethingFromWebService Something {get; set;}
}

我的问题是,我想使用的接口在我的课(HasSomething.ISomething在这种情况下),但我需要有AutoMapper映射到具体类型。 (如果我没有映射到具体的类型,然后AutoMapper将创建代理,对我来说,这会导致我的应用程序的其他问题。)

The problem I have is that I want to use Interfaces in my classes (HasSomething.ISomething in this case), but I need to have AutoMapper map to concrete types. (If I don't map to concrete types then AutoMapper will create proxies for me. That causes other problems in my app.)

以上code给我这个错误:

The above code gives me this error:

缺少类型映射配置或不支持的映射。

Missing type map configuration or unsupported mapping.

映射类型:   SomethingFromWebService - > ISomething
  UserQuery + SomethingFromWebService - > UserQuery + ISomething

Mapping types: SomethingFromWebService -> ISomething
UserQuery+SomethingFromWebService -> UserQuery+ISomething

所以我的问题是,我怎么能映射到具体类型,并仍在使用的接口在我的课?

注意:我尝试添加这样的映射:

NOTE: I tried adding this mapping:

Mapper.CreateMap<SomethingFromWebService, ISomething>();

但随后返回的对象的类型是不是的东西返回使用ISomething为模板生成的代理服务器。

But then the object returned is not of type Something it returns a generated Proxy using ISomething as the template.

推荐答案

所以我想的东西,似乎工作。

So I figured something that seems to work.

如果我添加这两个映射:

If I add these two mappings:

Mapper.CreateMap<SomethingFromWebService, Something>();
Mapper.CreateMap<SomethingFromWebService, ISomething>().As<Something>(); 

然后它的作品,因为我想要的。

then it works as I want.

我一直没能找到对'作为'方法的任何文件(尝试使用Google的!:),但它似乎是一个映射重定向。

I have not been able to find any documentation on the 'As' method (try googling for that! :), but it seems to be a mapping redirection.

例如:对于这种映射(<$ C C $> ISomething )解决它 A 的东西

For example: For this Mapping (ISomething) resolve it As a Something.

这篇关于配置AutoMapper映射到具体的类型,但允许接口在我班上的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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