Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Can anyone name any usefull strategy/architectural pattern for allocating actions between different controllers when using MVC pattern for developing web application?

I am now developing web app using asp.net Mvc3 framework and still can't figure out how to manage actions and controllers. One approach is to create single action controller for each url, but it's not the best choice since to much controllers have to be created. Should I list all available urls that are supported by me web app, devide them into groups and create separate controller for each group or act in any different manner?

It seems like I will become face to face with some kind of mess with no consistent approach in managing actions and controllers.

share|improve this question

migrated from stackoverflow.com Aug 29 '12 at 14:51

This question came from our site for professional and enthusiast programmers.

    
A controller is responisbile for a 'domain' within your application. You can have multiple actions per controller. Example - you have a 'Car' domain object. You could have a controller for the 'Car' object that could contain actions for creating, deleting, displaying, editing, etc. –  Tommy Aug 29 '12 at 14:34
    
And what about different auxiliary stuff (e.g. account/home/about/pricing pages/views etc that are not parts of the application domain )? –  singleton Aug 29 '12 at 14:41
    
An account is still an single 'object' You can have an account controller. Views are associated with a controller and an action -> not sure what you are getting at here. Are you migrating an old application into a new one? Either way, group your items into meaningful groups. Each group would have an associated set of actions. –  Tommy Aug 29 '12 at 14:44
    
No migration. Brand new app from the scratch. Your approach sounds reasonable. I've just thought that there were some pattern or strategy that can help with consistent controller/action creation. –  singleton Aug 29 '12 at 14:49
    
There is no golden rule as each application is different. Basically, keep your logical items grouped by controller :) –  Tommy Aug 29 '12 at 14:50
show 1 more comment

1 Answer 1

up vote 4 down vote accepted

The best thing for you to do is just to start with the default MVC scheme - where controllers and actions are 'allocated' by URL convention, with the first part of the path determining the controller, and the rest of the URL (and the request type, e.g. GET or POST) determining the action that'll be called.

Whilst at first this seems like it must lead to a 'mess' the reality is that most websites don't actually require loads of controllers, and certainly don't require loads of actions.

Sometimes you might wish to do some clever stuff to split one controller into two, whilst keeping the URLs the same - e.g you might have:

~/Products going to ProductsController.GetProducts()

~/Products/{id} going to ProductsController.GetProduct(id)

But then, let's say you have reviews of products too, you might have

~/Products/{id}/Reviews going to ProductReviewsController.GetReviews(id)

This could be legitimate since reviews functionality might require quite a lot of code, which isn't directly related to the products themselves.

That said, the motivation for splitting controllers shouldn't be to keep a code-file small (you can do that with partial classes), it should be to keep to a single-responsibility principle; that is - a controller should have responsiblity for single section of your site. The Users controller should not also be used for your homepage.

Don't spawn a new controller for everything you do, consider it based on the entity that the controller will operate on or provide details of (e.g. products, users etc). Thinking URL-first and then using that as the basis for new controllers is a very good starting point, because ultimately you want your site to be easily navigable by search engines as well as users.

The beauty of MVC, also, is that with the separation of controllers from the route patterns that trigger their actions, you can completely change the URL structure of all your existing code just by changing the routing. For that reason it's particularly important to remember always to use the UrlHelper for generating URLs to other actions, and HtmlHelper's ActionLink and RouteLink operations in views for generaing cross-links; these honour routing, and so take away a lot of the pain associated with re-structuring a website at the URL level.

share|improve this answer
1  
+1, I believe the same thing, its SRP that should guide you here in creating the controllers and actions. And don't forget, SRP applies not only to classes, but to packages (of classes), modules, and other hierarchies as well. –  Shivan Dragon Aug 30 '12 at 8:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.