I am using Automapper to convert between my EF4 Models and my ViewModels. Automapper needs map relationships declared and I find myself copy/pasting them inside every controller’s constructor.
Mapper.CreateMap<CoolObject, CoolObjectViewModel>();
Where can I place the mapping declarations so they will only be called once and not every time a controller is instantiated? Is this possible?
You can put it in the
application_start()of theglobal.asaxCurrently I have a static method that I call from the application_start that initializes all my mappings.
Library.AutoMapping.RegisterMaps();So my Controller looks something like this. You’ll notice that the HomeController constructor requires an IDataContext. I register IDataContext with Ninject on a RequestScope level and a DataContext is instantiated for me and injected into my controller. This is where my request level repository comes from.
I have a slightly more detailed explanation about Ninject here http://buildstarted.com/2010/08/24/dependency-injection-with-ninject-moq-and-unit-testing/