1
\$\begingroup\$

I'd like to check my understanding and usage of the AutoMapper in my ASP.Net MVC app.

I setup the maps in Application_Start:

 Mapper.CreateMap<Customer, CustomerViewModel>(); Mapper.CreateMap<CustomerViewModel, Customer>(); Mapper.AssertConfigurationIsValid(); 

To return a list of users associated with the logged in user, in my Customer Controller I have:

 // // GET: /Customer/ public ActionResult Index() { var customer = db.Customers.Where(x => x.UserName == User.Identity.Name).ToList(); IList<CustomerViewModel> customerVM = Mapper.Map<IList<Customer>, IList<CustomerViewModel>>(customer); return View(customerVM); } 

To Edit a specific customers details I am using:

 // // GET: /Customer/EditPartial public ActionResult EditPartial(int id) { var customer = db.Customers.Where(x => x.UserName == User.Identity.Name && x.CustomerId == id).FirstOrDefault(); CustomerViewModel customerVM = Mapper.Map<Customer, CustomerViewModel>(customer); return PartialView(customerVM); } 

Where I Post back updates after making changes in the EditPartial view:

 // // POST: /Customer/EditPartial [HttpPost] [ValidateAntiForgeryToken] public ActionResult EditPartial(CustomerViewModel customerVM) { if (ModelState.IsValid) { Customer customer = db.Customers.Where(x => x.UserName == User.Identity.Name && x.CustomerId == customerVM.CustomerId).FirstOrDefault(); if (customer == null) { return HttpNotFound(); } customer.CustomerName = customerVM.CustomerName; // How do I use AutoMapper here, instead of updating the domain model directly customer.Email = customerVM.Email; db.Entry(customer).State = EntityState.Modified; db.SaveChanges(); ///////////////// // Now I want to return a customer list again // Is this the best way to do it again var customer2 = db.Customers.Where(x => x.UserName == User.Identity.Name).ToList(); IList<CustomerViewModel> customerVM2 = Mapper.Map<IList<Customer>, IList<CustomerViewModel>>(customer2); return PartialView("CustomerListPartial", customerVM2); } this.Response.StatusCode = 400; return PartialView("EditPartial", customerVM); } 

The code above works, I'm just not sure about using AutoMapper in the Post, and if my way of returning a partial list of customers is the best method.

\$\endgroup\$

    1 Answer 1

    1
    \$\begingroup\$

    If you were not using AutoMapper, your POST method would either have a block of manual mapping, or it would call out to a custom mapper class. In the latter case, you would have a line (or two) calling that helper class instead of a line calling AutoMapper.

    Whether returning a PartialView is the best method depends on your UI; if that's what your View needs, then that's what you need to to. (I personally don't use them in my markup.)

    I wager the reason you think the mapping doesn't belong in your POST method is because your Controller is not just concerned with Control, but also Persistence and Construction. If the rest of your application is indeed this simple, then you might as well stick with it, but as complexity grows, you should consider hiding the persistence concerns behind an interface.

    \$\endgroup\$
    2
    • \$\begingroup\$Hi - thank you - the reason for the partial view return, is the post is made from a popup box via AJAX - so the partial view returns the updated customer list, which is then used to update a DIV on the UI - so no "postback" visible. If you're aware, do you know how I would use the AutoMapper in the Post controller, or am I using it correctly, by copying objects from the Post into my domain objects? Thanks, Mark\$\endgroup\$
      – Mark Tait
      CommentedMay 9, 2013 at 6:59
    • \$\begingroup\$You are absolutely using it as intended, taking a ViewModel as an argument to your ActionMethod and mapping it to your domain object(s).\$\endgroup\$CommentedMay 9, 2013 at 23:50

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.