Question

I have an ASP.NET MVC3 in C# and Razor. The architecture of the application is divided in Data Access Layer (EF classes + Repository), Service Layer, Controller, ViewModels and View.

My ViewModel exposes a method Fill which accepts as parameter the data collection to display in the View. In order not to have coupling between the components, the Fill method has to be called from the Service Layer or from the Controller?

Was it helpful?

Solution

Definition: Model (as in MVC) = Service layer (in this case)

The controller should be conspired as a glue between the view and the model. The view should not be aware of the model and vice versa.

The answer to your question is therefore that the Controller should contain all logic used to move information from the model to the view model. It's perfectly fine to use any class from the Model within the view model.

Update in answer to comments

Like I said. I have no idea how your classes looks like or what you have to do to generate the navigation.

  1. The controller should be used to assemble the information required by the view model
  2. The controller should not have any business logic

This means that you might have to create new classes in your service layer to be able to achieve those two goals.

OTHER TIPS

It would be better to remove the Fill function from your ViewModel then the ViewModel wont depended on your data layer.

I would then use something like Automapper in the controller to map the DataObject To the ViewModel before returning the view.

ActionMethod:

public ActionResult Edit(int id)
{
    var data = DataRepo.Get(id);
    var viewMode = Mapper.Map<ViewModel>(data);
    return View(viewModel);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top