巩固ASP.NET MVC控制器相关性(StructureMap) [英] Consolidating ASP.NET MVC Controller Dependencies (StructureMap)

查看:149
本文介绍了巩固ASP.NET MVC控制器相关性(StructureMap)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待在我的网站上的控制器,以及大部分的构造函数是这样的:

I'm looking at the controllers in my website, and most of their constructors look like this:

public SomeController(
   IServiceOne serviceOne, 
   IServiceTwo serviceTwo, 
   ILoggingService loggingService, 
   IGeospatialService geoSpatialService)
{
    // copy to class variables.
}

在换句话说,它是非常有毛,使重构困难。有些控制器具有8点左右的依赖关系。

In other words, it's very hairy and makes refactoring difficult. Some controllers have around 8 dependencies.

有什么方法可以让我莫名其妙地组,这些依赖关系更加桶之一?

Is there any way i can somehow "group" these dependencies into one of more buckets?

例如, ILoggingService 在每个控制器是必需的,是由控制器谁做空间的东西需要 IGeospatialService ,和 IServiceOne IServiceTwo 只需要在某些情况下。

For example, ILoggingService is required in every controller, IGeospatialService is required by controllers who do spatial stuff, and IServiceOne and IServiceTwo is only required in certain cases.

我希望看到这样的事情:

I would like to see something like this:

public SomeController(
       ICoreServicesGroup coreGroup,
       ISomeNameForServicesGroup serviceGroup)
    {
        // copy to class variables.
    }

我想这将是很好的介绍一些面向对象的技术,比如有一个基地depedency类,它需要一个 ILoggingService 在它的构造函数的保护。那么你可能有另一个孩子抚养它继承等。

I'm thinking it would be good to introduce some OO techniques, such as having a "base" depedency class, which takes a ILoggingService in it's protected ctor. Then you might have another child dependency which inherits, etc.

有没有人这样做呢?这是不是StructureMap能为我做的,还是它只是我滚动我自己的基本code?

Has anyone done this before? Is this something StructureMap can do for me, or is it simply me rolling my own basic code?

推荐答案

登录

当需要在依赖的每一个的控制器这是一个pretty一定的指标,它不是一个正常的依赖,而是一个横切关注点。日志记录是一个横切关注点的原型例子,所以 ILoggingService 应该像任何其他横切关注点处理。

When a dependency is required in each and every Controller it's a pretty certain indicator that it's not a 'normal' dependency, but rather a Cross-cutting Concern. Logging is the archetypical example of a Cross-cutting Concern, so ILoggingService should be dealt with like any other Cross-cutting Concern.

SOLID OO适当的方式来解决横切关注点是采用一个装饰(其中的可以对AOP 一概而论)。然而,ASP.NET MVC控制器操作方法不是任何接口的一部分,所以这是一个不太理想的解决方案。

In SOLID OO the appropriate way to address a Cross-cutting concern would be to employ a Decorator (which can be generalized towards AOP). However, ASP.NET MVC Controller action methods aren't part of any interface, so that's a less ideal solution.

相反,MVC框架提供行动过滤器截取目的。如果你想实现一个松耦合的过滤器,做自己href=\"http://stackoverflow.com/a/7194467/126014\">实现它作为一个全球性,而不是过滤器的属性一个忙,

Instead, the MVC framework provides Action Filters for interception purposes. If you want to implement a loosely coupled filter, do yourself a favor and implement it as a global filter instead of an attribute.

其他依赖

有关其他依赖是有意义的<一个href=\"http://stackoverflow.com/questions/6025482/strategy-to-refactor-when-too-many-dependencies-injected-into-service-or-control/6026515#6026515\">refactor他们的门面服务。这涉及到确定相关的服务自然集群,所以究竟是如何做到这一点是具体到每个code基地。

For other dependencies it makes sense to refactor them to Facade Services. This involves identifying natural clusters of related services, so exactly how this is done is specific to each code base.

这篇关于巩固ASP.NET MVC控制器相关性(StructureMap)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆