views:

173

answers:

2

I come from the WPF side of the world and I am used to using the MVVM pattern quite a bit. I am attempting to learn MVC and am having a little bit of dificulty trying to understand where my boundries are in MVC. Here is my scenario:

I have 3 objects, Parent, Child and GrandChild. These are custom objects, not using the built in model stuff from MVC. I have a good handle on validation stuff. I have a good handle on how to get and populate my objects. But, I am struggling to find best practices on what to do with Controllers. What should my controller be responsible for? For example, should I have one controller that understands how to CRUD Parent, Child, and GrandChild? Or should those be separated? If they should be separated, how should I do that if, when I am looking at Parent, I want to see a list of Children.

Any guidance on this subject or links to other blogs with information would be greatly appreciated. Thanks.

+3  A: 

Controller is used only for controlling the flow of the request-response. So, in your example, controller should never know how to CRUD them. CRUD logic should be wrapped in a Repository class of the model.

Take a look at the Official Nerd Dinner example and I personally love this part the most.

xandy
I have to apologize for my lack of clarity, but what I actually meant was my model is doing the CRUD, and the Controller is talking to my model. Sorry for the confusion.
poindexter12
It depends on how complicated is your business model. For simple application, it is OK for controller to instruct the repository class to CRUD. As you can see from nerd dinner, even validation on input logics are better to implement outside controller. I think for more complicated business models, it might better the controller only deal with "passing" data to business-tier classes.
xandy
A: 

The Nerd Dinner app is a clean cut example. I agree with pushing CRUD to a repository, and in general, using the controller only for control flow.

However, in my experience with ASP.NET MVC (right or wrong), the controller ends up doing a lot of rearranging of the data before handing off to the view, and vice versa when accepting an object model as data from a form post. But again, it is just making a translation between what the View needs and what the Model needs.

Greg Ogle
Right, controller acting as a *bridge* between view and model. Be sure that view and model are not dependent on each other, and one interesting point I like from Nerdinner is that controller classes should not dependent on the actual view too.
xandy