问题描述:

I am sucessfully using AutoMapper to convert my business objects to Dtos in all my services, and that works out prettly well.

however i would also like to use automapper, to do the reverse i.e to convert dto to business object, but there is one problem, i need to run some custom logic that has dependency on my EF entity context before mapping

for this i would like to pass my EF context to the map method somehow. I know i can use service locator type pattern to resolve dependency, but i hate introducing magic dependencies, and would like to make everything constructor dependent.

Is it possible to pass my dataContext to AutoMapper while mapping ?

网友答案:

A possible way could be something like:

using (Context db = new Context())
{
    Mapper.CreateMap<dtos, Ent>()
          .BeforeMap((a, b) => /* and here use db */ )
          ./* mapping rules */;

}

Declaring the mapping inside a using of your Countext, it should make the context visible in the BeforeMap(), where you can perform your logics.

相关阅读:
Top