简体   繁体   中英

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. 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. 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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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