保持 asp.net mvc 控制器大小较小

发布于 2024-10-20 07:14:44 字数 129 浏览 4 评论 0原文

我有一个控制器。 “订单控制器”。目前有1800行。我喜欢缩小尺寸。我正在使用静态辅助方法,这很好,但我使用 ninject 来调用我的存储库,因此在不传递属性的情况下无法访问静态方法中的存储库。

有哪些减少控制器噪音的好方法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我还不会笑 2024-10-27 07:14:44

如何获得瘦控制器

  1. 重构可重用的功能,这些功能可以应用于ActionFilters的多种类型的输出。 结果:重复代码更少、控制器操作更精简、未来开发更快

  2. 将适用于特定类型输出的可重用功能重构为自定义 ActionResult结果:重复代码更少,控制器操作更精简,未来开发更快

  3. 利用 ModelBinders 将输入值绑定到注入到 Controller< 中的复杂对象/代码> 行动。 结果:您根本不需要在控制器中处理实际的 HTTP 输入(RouteData、表单值、查询字符串参数)。您还可以在模型绑定程序中处理数据验证。

  4. 通过自定义ControllerFactory实现依赖注入。 结果:您不需要在控制器中构建服务。

  5. 将具有过多控制器操作的单个控制器重构为多个控制器。 后果:您的代码变得更易于维护。

  6. 将静态辅助方法移至静态类。 结果:您的方法可以被多个控制器重用,并且控制器中的臃肿代码更少,因此更容易维护和更改您的应用程序。

其他注释

有大量开源资源可以帮助完成这些任务。我绝对建议研究 MvcContrib 项目。他们有一个 FluentController 基类,该基类是为构建瘦控制器而设计的。另外,我给 Darin 投了赞成票,因为他推荐的视频很有帮助,所以请观看

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

冷默言语 2024-10-27 07:14:44

您的控制器中不应该有那么多代码。我怀疑你还没有分开你的担忧。

我会看一下并思考这个问题的答案:

ASP.NET MVC模式

简而言之:

  1. 将复杂性放入执行明确目的的服务类中,即交付控制器所需的内容。

  2. 控制器应该只具有应用程序逻辑,即,它应该只是充当一种空中交通,嗯,控制器,基于应用程序逻辑以这种方式发送请求。简而言之,这就是它的功能。其他东西不属于控制器。

我的控制器如下所示:

[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);
        }
    }

注意控制器构造函数中的服务实例化以及操作中对服务的调用。

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.

偏爱你一生 2024-10-27 07:14:44

1800行!!!!!!!!!神圣的上帝之母。我建议您观看以下视频,了解如何控制控制器节食。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文