Question

In my web application a product is defined by N number of product options and a product object. To display a full product I need to create a view model that will hold the product object, a list of product options and for each product option a list of possible values. My question is what is best practice? Where should I construct this object?

In my mvc application I have a service layer, should I do it in the service layer and have something like productservice.getproduct(id) return the view model ready for the view...or in my controller do I use a combination of...

Productservice.getProduct(Id) and productoptionService.getProductoptions(productId)

And then inside the controller construct the view Model?

My guess is to go with the first option, any ideas?

Was it helpful?

Solution

Your question is a bit complicated because what one developer means by a service is something else to another.

So how do we continue?

Well we know it is the responsibility of the Controller to mediate between the View and the ViewModel.

  • Mapping from the request to the ViewModel
  • Choosing the appropriate View
  • And delivering the ViewModel to the View

Keeping in mind that one ViewModel to one View is generally a good practice.

So what about your Domain model?

Well it's best to keep that seperate don't allow it to leak into your controller, this is to prevent any sort of UI specific information from accidentily making its way onto your Domain model.

Personally I practise the following, I have a slim layer (call it a manager or service) that returns a ViewModel, and receives a ViewModel when talking to the controller. But when talking to my internals talks with my Domain model.

So Controller -> Manager -> Service

Manager maps between Domain model and ViewModel.

For example:

public class MemberManager 
{
  public MemberService MemberService {get;set;}

  public MemberViewModel GetMember(int id)
  {
      var domainModel = MemberService.GetMember(id);      
      return new MemberViewModel { FullName = domainModel.FullName };
  }

  public bool UpdateMember(MemberViewModel viewModel)
  {
      MemberService.Update(new MemberDomainModel { Id = viewModel.Id, FullName = viewModel.FullName });

     return true; // all good.
  }
}

And your controller.

public class MemberController 
{
   public MemberManager MemberManager {get;set;}

   public ActionResult View(int id)
   { 
     var viewModel = MemberManager.Get(id);
     return View(viewModel);
   } 
}

OTHER TIPS

From my understanding of DDD and MVC's view-model, the one you mentioned return from productservice.getproduct(id) is belong to domain model and the view model is just used for pass data to view for display purpose. So in my opinion, you should have a view-model which is a combination of Productservice.getProduct(Id), productoptionService.getProductoptions(productId) and you should construct your view-model in your controller by calling these 2 methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top