Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keeping asp.net mvc controller size down

I have a Controller. "OrderController". Currently it's 1800 lines. I like to reduce the size. I'm using static helper methods which is fine but i'm using ninject to call my repositories so don't have access to the repositories in the static methods without passing the properties in.

What are some good approaches for reducing controller noise?

like image 785
frosty Avatar asked Mar 04 '11 11:03

frosty


1 Answers

How to get a Thin Controller

  1. Refactor reusable functionalities that can apply to multiple types of output to ActionFilters. Consequence: Less repetitive code, thinner Controller actions, quicker future development

  2. Refactor reusable functionalities that apply to a specific type of output to a custom ActionResult. Consequence: Less repetitive code, thinner Controller actions, quicker future development

  3. Leverage ModelBinders to bind your input values to complex objects that are injected into your Controller action. Consequence: You don't need to handle the actual HTTP input (RouteData, Form values, querystring parameters) at all in your controller. You can also handle data validation in your model binder.

  4. Implement Dependency Injection via a custom ControllerFactory. Consequence: You don't need to construct services in your Controller.

  5. Refactor single Controllers with an excessive amount of Controller actions into multiple Controllers. Consequences: Your code becomes more maintainable.

  6. Move your static helper methods to static classes. Consequence: Your methods become reusable by multiple controllers and you have less bloated code in the Controller, so it is easier to maintain and make changes to your app.

Other Notes

Plenty of open source resources exist to help accomplish these tasks. I definitely suggest looking into the MvcContrib project. They have a FluentController base class that was designed with building thin Controllers in mind. Also, I upvoted Darin because the video he recommended is helpful, so check it out

like image 190
smartcaveman Avatar answered Sep 22 '22 06:09

smartcaveman