keeping asp.net mvc controller size down
Asked Answered
C

3

7

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?

Crinite answered 4/3, 2011 at 11:53 Comment(0)
P
12

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

Preciado answered 4/3, 2011 at 12:35 Comment(0)
E
2

No way should there be that much code in your controller. I suspect you haven't separated your concerns.

I would have a look and a think at the answer to this SO question:

ASP.NET MVC Patterns

In short:

  1. Put the complexity into Service classes that perform a clear cut purpose, ie, to deliver what the controller needs.

  2. The controller should just have the application logic, ie, it should just be acting as a kind of air traffic, uhmm, controller, sending requests this way and that based on app logic. That is pretty much its function in a nutshell. Other stuff doesn't belong in a controller.

My controllers look like:

[Authorize(Roles="Admin, Tutor, Pupil")]
public partial class CourseController : Controller
{
    ICourseDisplayService service;
    public CourseController(ICourseDisplayService service)
    {
        this.service = service;
    }

    public virtual ActionResult Browse(int CourseId, string PupilName, string TutorName)
    {
        service.Initialize(CourseId, 1, PupilName, TutorName, User);
        service.CurrentStepOrder = service.ActiveStepIndex;
        if (Request.IsAjaxRequest())
        {
            return PartialView(MVC.Courses.Course.Views._Display, service.ViewModel);
        }
        else
        {
            return View(MVC.Courses.Course.Views.Display, service.ViewModel);
        }
    }

note the service instantiation in the controller's constructor and the calls to service in the actions.

Earring answered 4/3, 2011 at 12:36 Comment(1)
Your services expose view models directly? Doesn't seem like a great example of good design. I'd rather have a shedload of mapping code in my controller than have a service that has not only application-specific but view-specific code in it.Ayacucho
P
0

1800 lines!!!!!!!!! Holy mother of God. I would recommend you watching the following video about putting your controllers on a diet.

Professed answered 4/3, 2011 at 11:56 Comment(3)
How about 4000. For anyone that has worked on a large/medium sized team of developers this won't be remarkable. Certain hot-spots can accumulate bloat very quickly when the developers don't understand the basic architecture.Strasbourg
@Darin: Hi, could you fix the link and/or provide the name of the session, please?Heterosexuality
@Darin: I down voted the answer as the link does not work and it does not help to provide an answer.Shrubby

© 2022 - 2024 — McMap. All rights reserved.