When writing you controller actions, keep them short and sweet. Don’t add a lot of actions and keep a close eye on the length of each action.
Whenever possible, I try to make my controller actions look something like this.
public ActionResult DoSomething() { MyModel model = MyService.GetModel(); return View("MyView",model); } //or public ActionResult DoSomethingWithAKey(int myKey) { MyModel model = MyService.GetModel(myKey); return View("MyView",model); }
(1)
There are two reasons to do this:
1) It makes your controllers less complicated and easier to maintain. – You don’t want to have to do a lot of debugging in your controllers. Chances are, if you have a lot of complicated logic in your controller actions, you are probably mixing concerns. Make sure that your controllers are only responsible for getting data from your models and figuring out what view to send to the client.
2) It makes testing easier. If all you are doing is getting a model and passing that model to a view, what do you really need to test? Maybe that the correct view is being returned.
(1) Of course I use more descriptive names than “myKey”. Image may be NSFW.
Clik here to view.