Sorry if this is a dumb questin, but being moderately new to MVC, I'm a bit confused.
I have seen arguments put forward that strongly say that the model should be a dumb property bag that is used for passing data from the controller to the view. The controller is responsible for getting the data from the next layer down, doing whatever is
needed to that data, and populating the model.
I have seen equally strongly defended arguments that say the controller should do nothing more than decide which view to send back, and that the model is responsible for getting, validating and handling the data.
Any comments as to the relative merits of either approach?
Thanks
If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
I wasn't asking about the view, I try to keep those as lean and logic-free as possible.
I was asking about the model vs the controller, as I've seen people saying that the MVC-layer logic should go in one or the other. I was trying to find out which is best (if either).
If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
Hi Andrei, thanks for the reply and the interesting link. That's one vote for code in the model, although your controller isn't quite that dumb. Shouldn't the job of creating a new employee all go in the model?
If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
Sorry about that, between the Model and the Controller, I tend to make the Controller do as little work as possible, what that means is that I often put database actions in the Model generally as static members.
Please can you explain what you mean by that. Surely the creation of the new employee is nothign to do with the binding. I thought the binding happened between the view and the controller.
Thanks to both of you for the replies.
If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
the logic does not belong in either. the logic belongs in the BI layer. the controller should map BI data to the view model, and back. The view model should contain no data not required for the view. Exposing a full BI object to view is just asking for trouble.
Surely some logic can live in the MVC layer. What about validation logic? I would have thought you would valid the data at that stage, then pass the BLL a validated object, which it can then deal with as it sees fit.
Or would you put validation in the BLL as well?
If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
MrYossu
Member
134 Points
142 Posts
Where does the logic go, in the controller or the model?
Dec 27, 2012 01:27 PM|LINK
Hello,
Sorry if this is a dumb questin, but being moderately new to MVC, I'm a bit confused.
I have seen arguments put forward that strongly say that the model should be a dumb property bag that is used for passing data from the controller to the view. The controller is responsible for getting the data from the next layer down, doing whatever is needed to that data, and populating the model.
I have seen equally strongly defended arguments that say the controller should do nothing more than decide which view to send back, and that the model is responsible for getting, validating and handling the data.
Any comments as to the relative merits of either approach?
Thanks
eric2820
Contributor
2777 Points
1161 Posts
Re: Where does the logic go, in the controller or the model?
Dec 27, 2012 01:37 PM|LINK
Most of the logic goes in the controller with MVC, the only logic that goes into the views is generally a foreach loop to process a list of data.
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
ignatandrei
All-Star
134491 Points
21566 Posts
Moderator
MVP
Re: Where does the logic go, in the controller or the model?
Dec 27, 2012 02:02 PM|LINK
I am for a dumb controller, not for a dumb model.
Please see http://msprogrammer.serviciipeweb.ro/2010/03/29/asp-net-mvc-orm-and-viewmodels/ and continuation - using same ViewModel from DOS.
MrYossu
Member
134 Points
142 Posts
Re: Where does the logic go, in the controller or the model?
Dec 27, 2012 10:05 PM|LINK
Hi Eric, thanks for the reply.
I wasn't asking about the view, I try to keep those as lean and logic-free as possible.
I was asking about the model vs the controller, as I've seen people saying that the MVC-layer logic should go in one or the other. I was trying to find out which is best (if either).
MrYossu
Member
134 Points
142 Posts
Re: Where does the logic go, in the controller or the model?
Dec 27, 2012 10:07 PM|LINK
Hi Andrei, thanks for the reply and the interesting link. That's one vote for code in the model, although your controller isn't quite that dumb. Shouldn't the job of creating a new employee all go in the model?
eric2820
Contributor
2777 Points
1161 Posts
Re: Where does the logic go, in the controller or the model?
Dec 27, 2012 10:08 PM|LINK
Sorry about that, between the Model and the Controller, I tend to make the Controller do as little work as possible, what that means is that I often put database actions in the Model generally as static members.
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
ignatandrei
All-Star
134491 Points
21566 Posts
Moderator
MVP
Re: Where does the logic go, in the controller or the model?
Dec 28, 2012 04:14 AM|LINK
Maybe. But you lose the power of binding in MVC.
MrYossu
Member
134 Points
142 Posts
Re: Where does the logic go, in the controller or the model?
Dec 31, 2012 02:16 PM|LINK
Please can you explain what you mean by that. Surely the creation of the new employee is nothign to do with the binding. I thought the binding happened between the view and the controller.
Thanks to both of you for the replies.
bruce (sqlwo...
All-Star
36644 Points
5432 Posts
Re: Where does the logic go, in the controller or the model?
Dec 31, 2012 03:34 PM|LINK
the logic does not belong in either. the logic belongs in the BI layer. the controller should map BI data to the view model, and back. The view model should contain no data not required for the view. Exposing a full BI object to view is just asking for trouble.
MrYossu
Member
134 Points
142 Posts
Re: Where does the logic go, in the controller or the model?
Dec 31, 2012 05:24 PM|LINK
Hi Bruce, thanks for the reply.
Surely some logic can live in the MVC layer. What about validation logic? I would have thought you would valid the data at that stage, then pass the BLL a validated object, which it can then deal with as it sees fit.
Or would you put validation in the BLL as well?