Hosting ASPAP MVC 2 Automapper

I use Automapper to convert between my EF4 models and ViewModels. Automapper requires that relationships with cards be declared, and I find that they copy / paste them inside each controller constructor.

Mapper.CreateMap<CoolObject, CoolObjectViewModel>();

Where can I place matching declarations, so they will only be called once, and not every time a controller instance is created? Is it possible?

+3
source share
1 answer

You can put it in application_start()inglobal.asax

I currently have a static method that I call from application_start, which initializes all my mappings. Library.AutoMapping.RegisterMaps();

AutoMapper.Mapper.CreateMap(typeof(CoolObject), typeof(CoolObjectViewModel));

AutoMapper.Mapper.CreateMap<CoolObject, CoolObjectViewModel>()
    .ForMember(d => d.Property1, f => f.MapFrom(s => s.Property1))
    .ForMember(d => d.Property2, f => f.MapFrom(s => s.Property2))
    .ForMember(d => d.Property3, f => f.MapFrom(s => s.Property3));

, . , HomeController IDataContext. IDataContext Ninject RequestScope, DataContext . .

public class HomeController : Controller {

    IDataContext dataContext;

    public HomeController(IDataContext dataContext) {
        this.dataContext = dataContext;
    }
}

Ninject http://buildstarted.com/2010/08/24/dependency-injection-with-ninject-moq-and-unit-testing/

+5

Source: https://habr.com/ru/post/1767621/


All Articles