【发布时间】:2011-12-08 04:23:27
【问题描述】:
我正在查看我网站中的控制器,它们的大多数构造函数看起来像这样:
public SomeController(
IServiceOne serviceOne,
IServiceTwo serviceTwo,
ILoggingService loggingService,
IGeospatialService geoSpatialService)
{
// copy to class variables.
}
换句话说,它非常繁琐,使重构变得困难。一些控制器有大约 8 个依赖项。
有什么方法可以让我以某种方式将这些依赖项“分组”到多个存储桶之一中?
例如,每个控制器都需要ILoggingService,执行空间操作的控制器需要IGeospatialService,而IServiceOne 和IServiceTwo 仅在某些情况下需要。
我希望看到这样的东西:
public SomeController(
ICoreServicesGroup coreGroup,
ISomeNameForServicesGroup serviceGroup)
{
// copy to class variables.
}
我认为引入一些 OO 技术会很好,例如有一个“基础”依赖类,它在受保护的 ctor 中采用 ILoggingService。然后你可能有另一个继承的子依赖等等。
以前有人做过吗?这是 StructureMap 可以为我做的事情,还是只是我在滚动自己的基本代码?
【问题讨论】:
-
我本来打算回答的,但我最终会说这样的话:stackoverflow.com/a/6026515/373378
标签: c# asp.net-mvc dependency-injection dependency-management