保持 asp.net mvc 控制器大小较小
我有一个控制器。 “订单控制器”。目前有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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如何获得瘦控制器
重构可重用的功能,这些功能可以应用于
ActionFilters
的多种类型的输出。 结果:重复代码更少、控制器操作更精简、未来开发更快将适用于特定类型输出的可重用功能重构为自定义
ActionResult
。 结果:重复代码更少,控制器操作更精简,未来开发更快利用
ModelBinders
将输入值绑定到注入到Controller< 中的复杂对象/代码> 行动。 结果:您根本不需要在控制器中处理实际的 HTTP 输入(RouteData、表单值、查询字符串参数)。您还可以在模型绑定程序中处理数据验证。
通过自定义
ControllerFactory
实现依赖注入。 结果:您不需要在控制器中构建服务。将具有过多控制器操作的单个控制器重构为多个控制器。 后果:您的代码变得更易于维护。
将静态辅助方法移至静态类。 结果:您的方法可以被多个控制器重用,并且控制器中的臃肿代码更少,因此更容易维护和更改您的应用程序。
其他注释
有大量开源资源可以帮助完成这些任务。我绝对建议研究 MvcContrib 项目。他们有一个
FluentController
基类,该基类是为构建瘦控制器而设计的。另外,我给 Darin 投了赞成票,因为他推荐的视频很有帮助,所以请观看How to get a Thin Controller
Refactor reusable functionalities that can apply to multiple types of output to
ActionFilters
. Consequence: Less repetitive code, thinner Controller actions, quicker future developmentRefactor reusable functionalities that apply to a specific type of output to a custom
ActionResult
. Consequence: Less repetitive code, thinner Controller actions, quicker future developmentLeverage
ModelBinders
to bind your input values to complex objects that are injected into yourController
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.Implement Dependency Injection via a custom
ControllerFactory
. Consequence: You don't need to construct services in your Controller.Refactor single Controllers with an excessive amount of Controller actions into multiple Controllers. Consequences: Your code becomes more maintainable.
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您的控制器中不应该有那么多代码。我怀疑你还没有分开你的担忧。
我会看一下并思考这个问题的答案:
ASP.NET MVC模式
简而言之:
将复杂性放入执行明确目的的服务类中,即交付控制器所需的内容。
控制器应该只具有应用程序逻辑,即,它应该只是充当一种空中交通,嗯,控制器,基于应用程序逻辑以这种方式发送请求。简而言之,这就是它的功能。其他东西不属于控制器。
我的控制器如下所示:
注意控制器构造函数中的服务实例化以及操作中对服务的调用。
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:
Put the complexity into Service classes that perform a clear cut purpose, ie, to deliver what the controller needs.
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:
note the service instantiation in the controller's constructor and the calls to service in the actions.
1800行!!!!!!!!!神圣的上帝之母。我建议您观看以下视频,了解如何控制控制器节食。
1800 lines!!!!!!!!! Holy mother of God. I would recommend you watching the following video about putting your controllers on a diet.