In advance, I apologize if this question has been answered already somewhere else; But I've been finding a lot of mixed results on the matter.
I'm using:
- .net Framework 4.6.1
- Microsoft.AspNet.Mvc 5.2.6
- AutoMapper 6.2.2
- EntityFramework 6.2.0
I'm quite new to both ASP.net and C# and lately I've become a fan of the AutoMapper package. I've mostly been using it to convert my Entities I get from my ApplicationDbContext
to my DTO
's (Data Transfer Objects) or ViewModel
's.
I now use this setup in my applications to initialize and use the Mapper
in my Controller
's:
Global.asax.cs
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
Mapper.Initialize(AutoMapperConfiguration.Configure);
// Other configuration for MVC application...
}
}
AutoMapperConfiguration.cs
public static class AutoMapperConfiguration
{
public static void Configure(IMapperConfigurationExpression config)
{
config.CreateMap<Post, Post.DetailsViewModel>().ForMember(post => post.CanEdit, cfg => cfg.ResolveUsing((src, dst, arg3, context) => context.Options.Items["UserId"]?.ToString() == src.UserId));
}
}
PostsController.cs and Post.cs
public class PostsController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Post post = db.Posts.Find(id);
if (post == null)
{
return HttpNotFound();
}
return View(Mapper.Map<Post.DetailsViewModel>(post, options => options.Items["UserId"] = User.Identity?.GetUserId()));
}
}
// Post.cs
public class Post
{
public int Id { get; set; }
public string UserId { get; set; }
public virtual ApplicationUser User { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public DateTime PostedAt { get; set; } = DateTime.Now;
public class DetailsViewModel
{
public int Id { get; set; }
public string UserId { get; set; }
public ApplicationUser User { get; set; }
public string Title { get; set; }
public string Content { get; set; }
/// <summary>
/// A value indicating whether the current logged in user can edit the model
/// </summary>
public bool CanEdit { get; set; }
public DateTime PostedAt { get; set; }
}
}
Code summary
I am configuring my Mapper
in a static class (AutoMapperConfiguration
) that contains a Config
method which is invoked from the Global.asax.cs
file and maps the required classes.
Then, in my Controller
I use the static method Mapper.Map
to map my Post
to it's DetailsViewModel
.
The question
How does this usage of AutoMapper (via static method Mapper.Map
) impact performance, and is there a better way to do this?
Some clarifications:
For example: What if I get 100 requests per second on different controller actions; As far as I know every request will have a separate thread but will access the same memory for the Mapper.Map
method (if I am correct). Which - to my knowledge - means performance will be impacted severely.
A question I have already looked at but got mixed results from:
Non-static AutoMapper and ASP.NET MVC -> Where to place AutoMapper.CreateMaps?
Please correct me if I'm wrong on any of this.