简体   繁体   English

AutoMapper映射共享接口的两个具体类

[英]AutoMapper mapping two concrete classes that share an interface

I have an interface that describes two objects. 我有一个描述两个对象的接口。 One is a DAO, the other is a BO. 一个是DAO,另一个是BO。 So, here's what it looks like: 所以,这是它的样子:

public interface IOrder ...

public class OrderDAO : IOrder ...

public class OrderBO : IOrder ...

And I would like to map OrderDAO to OrderBO. 我想将OrderDAO映射到OrderBO。 My original line of thought was that I could simply map interface to interface and supply both concrete objects, like so: 我最初的想法是,我可以简单地将接口映射到接口并提供两个具体对象,如下所示:

AutoMapper.Mapper.Map<IOrder, IOrder>(myOrderDAO, myOrderBO);

However, this did not work out-of-the-box. 但是,这不是开箱即用的。 I still had to create a map in order to get the data from myOrderDAO to myOrderBO. 我仍然必须创建一个地图才能将数据从myOrderDAO获取到myOrderBO。

AutoMapper.Mapper.CreateMap<IOrder, IOrder>();

I'm not sure that makes a lot of sense to me and I feel like I'm "doing it wrong", as it were. 我不确定这对我有多大意义,而且我觉得自己“做错了”。 So my question is two-fold. 所以我的问题有两个方面。

  1. What is the best way to map in the above scenario? 在上述情况下映射的最佳方法是什么?
  2. Why would I need to create a map for the same interface? 为什么我需要为相同的界面创建地图?

Thanks in advanced. 提前致谢。

  1. this is a good way to map. 这是映射的好方法。 However if you want to reduce number of CreateMap calls, create a method that would create all maps and then call it once per application domain. 但是,如果要减少CreateMap调用的次数,请创建一个将创建所有地图的方法,然后在每个应用程序域中调用一次。 for example from some static constructor or another app start event. 例如来自某个静态构造函数或另一个应用程序启动事件。 see how they recommend it in their Get Started 看看他们如何在入门中推荐它

  2. even it is for the same interface there still should be a map, I guess. 我猜,即使是对于同一接口,也应该有一张地图。 on the bright side it makes your life easier, you do not have to create maps like and a lot more. 从好的方面来说,它使您的生活更加轻松,您不必创建诸如此类的地图,并且无需做很多其他事情。 It could be an optimization though. 虽然这可能是一种优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM