keeping asp.net mvc controller size down

廉价感情. 提交于 2019-12-09 18:22:25

问题


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?


回答1:


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




回答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.




回答3:


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



来源:https://stackoverflow.com/questions/5193368/keeping-asp-net-mvc-controller-size-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!