Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 3 - Controllers and ViewModels - Which should contain most of the business logic?

Currently in my application and using the unit of work pattern and generic repository, all my controllers contain all of the business logic. I'm in the process of putting everything over to use ViewModels instead of the straight Model.

While this is a good idea, now comes a question that can significantly separate my business logic in the controllers. For controllers and ViewModels, which should contain most of the business logic?

I've tried a few ways to get my ViewModels to practically contain all the business logic. However, I do have to have an argument in my ViewModel's constructor that takes a Unit of Work. Is this a good idea?

My code smell tells me it is. However, I am just a little worried how this will be in consistency with controllers that perform actions that do not need ViewModels. Simply put, actions that do not require to pass a Model/ViewModel to a View; this case happens on actions that do redirects to other actions. Which means, my business logic can either stay in that action or I could separate that business logic into a function.

What is the best practice here?

like image 573
TIHan Avatar asked Sep 12 '11 16:09

TIHan


People also ask

Should Viewmodels contain logic?

Short answer, Yes. The main aims of the Model-View-ViewModel (MVVM) pattern are: Permit unit testing of your view logic. These are unit tests applied to the ViewModel layer which is executed without a View associated with it.

Does ViewModel contain business logic?

It represents the data and the business logic of the Android Application. It consists of business logic, local and remote data sources, and model classes.

Where do you put your business logic in a MVC?

The business logic should be placed in the model, and we should be aiming for fat models and skinny controllers. As a start point, we should start from the controller logic. For example: on update, your controller should direct your code to the method/service that delivers your changes to the model.

What is MVC business logic?

MVC enables the application to be extensible and modular by separating the application into three parts: the business logic part, which implements data retrieval and manipulation. the user interface part, which is what the application users see. the controller part, which routes requests to the proper objects.


1 Answers

I can't say that my approach is a best practice, but I prefer to put any business logic in a separate "service" layer.

I tend to use the ViewModel to only store the properties required for a specific view. If there are any methods on the ViewModel, they are likely just to retrieve collections related to that view.

I keep my controllers lightweight by trying to limit them to validation and redirection/displaying views as much as possible.

So if I have any complex logic, I'll have a controller action call out to a separate service just to handle that logic. This way the logic is isolated which makes it easier to test since there's no longer a need to create a controller or ViewModel to test it. It's also easier to reuse a service than to pick apart a ViewModel.

Hopefully this helps. Good luck.

like image 148
Jonathan S. Avatar answered Sep 17 '22 14:09

Jonathan S.