Where is the best place to map from view model to

2019-03-08 21:25发布

问题:

Where is the best place to do mappings from view model to domain model? By mappings I mean from my EditGrantApplicationViewModel to a GrantApplication object.

Lets say that I have the following action method (partial code):

[HttpPost]
public ActionResult Create(EditGrantApplicationViewModel editGrantApplicationViewModel)
{
   if (!ModelState.IsValid)
   {
      return View("Create", editGrantApplicationViewModel);
   }

   return View("Index");
}

Do I need to pass editGrantApplicationViewModel to a service layer method and do the mappings in the method?

回答1:

You should not place any of your mapping logic inside the service layer since it simply dosent belong there. The mapping logic should go inside your controller and nowhere else.

Why you might ask? Simple enough, by placing the mapping logic in your service layer, it needs to know about the ViewModels which the service layer NEVER EVER should be aware of - also it reduces the flexibility of the app that you place the mapping logic in there since you cant reuse the service layer without a lot of hacks.

Instead you should do something like:

// Web layer (Controller)
public ActionResult Add(AddPersonViewModel viewModel)
{
    service.AddPerson(viewModel.FirstName, viewModel.LastName)
    // some other stuff...
}

// Service layer
public void AddPerson(string firstName, string lastName)
{
    var person = new Person { FirstName = firstName, LastName = lastName };
    // some other stuff...
}

By doing like above, you make your service layer more flexible since it's not bound to a particular class and it's not aware of the existence of your viewmodel.

UPDATE:

To map your Entities returned from the service layer to ViewModels, you might want to take a look at Automapper or Value Injecter.



回答2:

Use AutoMapper or similar framework directly in your controller.

Here is an introduction



回答3:

do it in your WebUI layer but don't do it the controller instead call your custom mapper/builder interface/class in there

sample: http://prodinner.codeplex.com



回答4:

I would personally never pass a view model to the service layer. If you go down that route your service ends up having direct knowledge about what is displayed on the view. This would cause changes in your viewmodel to cause changes in your service layer.

For example: Let's say you decided to add a SelectList to your view model for reason for grant edit.

public class EditGrantApplicationViewModel
{
   //...
   public SelectList ReasonForEdit {get;set;}
   //...
}

This could be a perfectly valid requirement, but ask yourself does having a SelectList pass to the Service Layer make sense? A select list is more of a UI domain and doesn't really mean anything to the service layer. The service layer only cares about the reason not the select list.

I would take your view model, digest the information needed and then pass that unit to your service layer.

[HttpPost]
public ActionResult Create(EditGrantApplicationViewModel editGrantApplicationViewModel)
{
   if (!ModelState.IsValid)
   {
      return View("Create", editGrantApplicationViewModel);
   }

   GrantApplication grantApplication = new GrantApplication();
   grantApplication. // other fields.
   grantApplication.Reason = editGrantApplicationViewModel.ReasonForEdit.SelectedValue;
   grantApplication. // other fields.
   _someService.EditApplication(grantApplication);

   return View("Index");
}

If you haven't looked yet check out AutoMapper as it can help in doing between view model's, dto's and other classes.