Jammer
Jammer

Reputation: 10208

ASP.NET Mvc - AutoMapper Best Practice - Performance

I've not looked at the AutoMapper source code yet but was just about to make some changes to an API controller in my solution and had a thought.

The way I like to code is to keep my controller methods as concise as possible, for for instances I make use of a generic Exception attribute to handle try{}catch{} scenarios.

So only the code that is absolutely relevant to the controller action is actually in the action method.

So I just arrived at a situation where I need to create an AutoMapper map for a method. I was initially thinking that I would add this (as I have done previously) to the controller constructor so its available immediately.

However, as the controller grows following this pattern may introduce a lot unnecessary AutoMapper work depending on the controller action method which is invoked.

Considering controllers are created and destroyed per request this could get expensive.

What are some recommendations around this? Considering AutoMapper is accessed statically I was wondering if it's internals live beyond the request lifetime and it internally checks for an existing map before creating a new one each time CreateMap() is invoked?

Upvotes: 1

Views: 1090

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

You should create your maps (CreateMap) once per AppDomain, ideally when this domain starts (Application_Start).

Upvotes: 3

Related Questions