5

I keep reading that the biggest layer in the MVC pattern should be the model. I've also heard that we should avoid putting logic on the controller layer. However, as my ASP.Net MVC 5 application is getting larger, I see that I'm getting heavy views, heavy controllers, and... extremely tiny models (they're not more than references to my SQL tables).

Yes, I admit, I could never manage to put any logic on my model.

I like the MVC pattern, and my website is working good, but I keep on thinking that I'm surely not doing things right...

Can you show me some useful links about how to write MVC code properly? Rick Anderson's (Microsoft) MVC 5 tutorial is fine, but once again, his models are indeed very tiny...

3
  • how are your views/controllers heavy? Are you doing a lot of operations? Accessing data in the controllers? Sorting/Filtering on View side??? Commented Mar 10, 2014 at 13:14
  • stackoverflow.com/questions/4565681/… stackoverflow.com/questions/18563229/… Commented Mar 10, 2014 at 13:14
  • @user1010863 I attempted to basiacally say what that answer said but , I couldn't have said that answer any better myself Commented Mar 10, 2014 at 13:17

5 Answers 5

3

In my applications I put as much logic as possible in the domain models. On top of that there is an application layer which interacts with the database and domain models to perform application specific operations. The controller actions have as little code as possible and just call methods in the application layer.

In addition I usually have a view model for each view. Any logic that you have making your views "heavy" would go there.

One of the main reasons I try to put as much logic as possible in the domain models is to make unit testing easier. Logic in the application layer usually involves the database, which you will need to mock in order to test. Moving logic to the domain models makes testing easier and makes you code more reusable.

This is a pretty complex issue. I have an in depth blog post on the question if you're interested.

This answer is also pretty close to what I would suggest.

Sign up to request clarification or add additional context in comments.

7 Comments

I am definitely in favor of using view models for many reasons. Generally a view will need more than provided by a single domain model. A view model allows you to provide all of the necessary data for the view without throwing things in the ViewBag. The view model also gives you a place to put logic and expressions that would otherwise have been done in the controller action or directly in the view.
Hi jdehlin, thanks for you answer and post blog. I would like to hear your opinion about Sebin's post. Do you agree with him? Isn't it a bad practice to have both controllers and third party classes sending Models/ViewModels to the View? My controllers are sending the respective Model to the view, but I hesitate on creating third party classes that will send ViewModels to the view as well. I would appreciate your expertise on the MVC problematic. I've read that some people are treating MVC like if it was MVVC in fact, and I think that might be the case.
It's not that you send both a model and a view model. Your model, if needed, can be added to the view model. Or you can make your view model have similar properties to your model and map one to the other using something like AutoMapper. Or you can have your view model inherit from domain model and override/expand on it. This flexibility is the benefit of a view model. You design it for the view on a case by case basis, rather than trying to mold your domain models to fit your views. I think this is what Sebin means by emit view models.
I agree with most of what Chris Pratt has to say in the linked response. I would disagree that persistence belongs in the domain model, though. I would argue that persistence belongs in the application layer. View models aren't just to represent domain models in different state (although than can be for that). For example imagine we have a view which displays data from three different domain models. We can create a view model which has three properties, one for each of the domain models we need in our view.
There isn't really one "right" answer to this question. The best thing you can do is learn and practice the underlying principles behind why people do these things. The SOLID principles, etc. etc.
|
2

You're missing a service/business layer which should be injected in your controllers though "Dependency Injection". These services do all the heavy lifting.

Having Models without any methods or operations in them is a good thing. You're only storing this info anyway. They basically just get; set; data.

Comments

1

Use extra layer between models and controllers (for example repositories as data access layer). I strongly recommend using ViewModels-they make code much more organized.

Comments

1

You should Create Some Classes that purely doing business logic and emit ViewModels for MVC view. Controller should respond to actions and the action method delegate the responsibility of getting the model to this business classes.

Comments

1

After some research on this issue, and taking into account some of these answers and comments, I realized that a medium sized MVC project can't rely exclusively on the 3 layered model. As the controller actions become bigger, the developer starts feeling the need of creating a 4th layer: the service layer. Like Gunnar Peipman correctly suggests in the following blog post, "Controller communicates with service layer and gets information about how access code claiming succeeded": http://weblogs.asp.net/gunnarpeipman/archive/2011/06/20/asp-net-mvc-moving-code-from-controller-action-to-service-layer.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.