1

I am a newbie to .Net MVC and my question today is regarding the MVC pattern.

In our application we have a Service Layer which talks with the DB.

The Controller is Currently talking with the Service layer to get the values from the DB.

Our new Manager requires this service layer interaction from the Models and not from the Controller.

He does say that this architecture is to achieve a thin Controller. We are now starting to port the service layer interaction from controller to models.

And here comes my question. Apart from having a thin Controller, is there any other benefits from enforcing this pattern.

I would like to know the advantages and disadvantages of both pattern.

Some links would also be helpful

tereško
  • 58,060
  • 25
  • 98
  • 150
Shankar
  • 327
  • 2
  • 6
  • 16
  • To clarify, he is asking you to enforce service calls within the 'view' models? – ChrisBint May 19 '14 at 13:33
  • Thin controllers are a great idea; call services from your controllers and have very little other logic in them. You do not want to be calling services from your model. – Sam Leach May 19 '14 at 13:36
  • @ ChirsBint. Is that MVC pattern that requires service calls to be made from Models. Or Is it just an option to use either Model or Controller for Service Calls. Can you please elaborate your comment. – Shankar May 19 '14 at 13:37
  • Your controller should call the service. The models represent your data. – Edwin van Vliet May 19 '14 at 13:38
  • 2
    There are really 4 things; your `Controllers`, your `Views`, your `Model` and your `ViewModels`. Your `Model` is everything behind your `Controllers` (services, helpers, repository, database). Your `ViewModels` are what you pass from your `Controllers` to your `Views` and back. In a sense, your services are inherently your `Model`. Call services from your `Controllers`. – Sam Leach May 19 '14 at 13:39
  • @ Sam Leach. I'm talking about the ViewModels. We are now shifting all the service calls from Controller to View Models. Does that have any advantage. If not so,Can you describe the disadvantages of the same. – Shankar May 19 '14 at 13:43
  • 2
    @Shanky, NEVER call Services from ViewModels! – Sam Leach May 19 '14 at 13:43
  • @SamLeach, Thanks but why. Please put me some links to prove this to my manager. – Shankar May 19 '14 at 13:45
  • @Shanky, see my answer. Google 'MVC pattern' for evidence. I think there is some confusion between you, your manager and the mvc pattern. :) – Sam Leach May 19 '14 at 13:48
  • @SamLeach, OfCourse i agree. :). I think he might not mean the View models. My wild guess is he needs an extra layer between the Controller and Service. – Shankar May 19 '14 at 13:52

2 Answers2

4

Why you shouldn't call services from your ViewModels:

ViewModels are supposed to be classes that contain some data that is interchanged between the View and the Controller. They should not perform any action or retrieve further data. They are dumb models, they don't do anything expect transport data.

What is a View Model

If you are having trouble understanding what a View Model is and what it isn't, think of it like a subset of your model. It only contains data that you need to display on a given view at a given time.

Community
  • 1
  • 1
Sam Leach
  • 12,746
  • 9
  • 45
  • 73
2

There are 3 types of Models - View Model, Domain Model and Data Model. Check here.

If you are talking about View Models then its a bad idea. There are ways to achieve a thin controller, but ViewModel never should interect with services. If its possible a Controller Action should only invoke a service and throw the result to View. Like this:

[HttpGet]
public ActionResult GetAnimals(int id) 
{
    var viewModel = new AnimalsService(id).GetViewModel();
    return View(viewModel);
}

But in reality many times you can't do that for some obvious reasons. There are few things you can do though. Like don't validate models in controller, you can do that in service layer. Don't hesitate to create more services for different jobs, like pagination, context related logic or some third party api invocation. Create helper or utility classes for repititive codes. Also I think its ok to write fat services.

Md Nazmoon Noor
  • 3,187
  • 1
  • 24
  • 30
  • Also service models often operate on domain model, that means you need to at least map domain model to view model in controller. I tried to move mapping into service, but after some thinking I realized its bad idea, as services speak in their own language(domain model) thus should not use VM. To remediate your issues you can create facade, as additional layer just before controller, if someone still has this problem in 2019 :) Is this worth the effort? If controller is very big, I think yes. – Kmaczek Aug 13 '19 at 06:45