最佳位置流利IOC配置/模块(目前正在Ninject) [英] Best location for Fluent IOC configuration/Modules (Currently trying Ninject)

查看:169
本文介绍了最佳位置流利IOC配置/模块(目前正在Ninject)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在努力找找到我的Ninject配置模块的最佳地点(如指定类型绑定的地方)。我希望我只是缺少一些明显的把戏,因为这正开始变成一个交易断路器对我来说,使用流利的配置(并因此Ninject):

I am struggling to find the best place to locate my Ninject configuration "Modules" (the place where Type bindings are specified). I hope I am just missing some obvious trick, as this is starting to turn into a deal-breaker for me with using fluent configuration (and thus Ninject):

在包含三个单独的项目一个简单的Web栈:网络,BusinessLogic,数据访问。我的不希望Web层有直接引用数据访问层的,但我看不到解决的办法,因为:

In a simple Web stack containing three separate projects: Web, BusinessLogic, DataAccess. I do not want the Web tier to have to directly reference the DataAccess tier, but I can't see a way around this because:

  • 如果我把数据访问配置模块中的数据访问层,我必须引用数据访问层,这样我就可以在Web层实例化Ninject内核时访问配置模块

  • If I put the DataAccess configuration Module in the DataAccess layer, I have to reference DataAccess layer so I can access the configuration module when instantiating the Ninject Kernel in the Web tier

如果我把数据访问配置模块在Web层,我要引用数据访问层,以获得我想要绑定类型

If I put the DataAccess configuration Module in the Web tier, I have to reference the DataAccess layer to have access to the types I want to bind

如果我把数据访问配置模块在一个单独的配置项目,我结束了循环引用问题,试图指定绑定Web和数据访问层的时候。

If I put the DataAccess configuration Module in a separate configuration project, I end up with circular reference issues when trying to specify bindings for both web and DataAccess tiers.

国际奥委会的好处之一是允许松耦合,但就我所看到的,使用Ninject会要求我增加更多的直接的项目引用了我现在有。我在想什么?

Part of the benefit of IOC is to allow loose coupling, but as far as I can see, use of Ninject would require me to add more direct project references that I currently have. What am I missing?

推荐答案

Ninject不需要的组件引用!你可以告诉内核来加载所有的模块,从符合特定模式的组件 - 请参阅加载()重载!使用这种机制你映射了可以公开的特征模块,@Daniel马尔巴赫在每个功能被实现的地方建议。我不喜欢定义每个绑定程序集的这些巨大的模块。我宁愿每一个具体的小模块,用于特定功能。

Ninject does not require that the assemblies are referenced! You can tell the Kernel to load all modules from the assemblies that match a certain pattern - see the Load() overloads! Using this mechanism you map the can expose your features as Modules as @Daniel Marbach suggested in the place where each feature is implemented. I do not like these huge modules defining every binding for an assembly. I'd rather have each in a specific small module for a certain feature.

这也可以让一个启用/禁用/替换实现无其他组件重新编译(至少如果你有在不同的组件接口)。

This also allows one to enable/disable/replace implementations without recompilation of the other assemblies (at least in case you have the interfaces in separate assemblies).

所以基本上你有:

  • 在一个或多个Web层组件:包含控制器,视图和绑定Web层。每个组件的引用一些组件定义接口这取决于
  • 在一个或多个组件定义接口的web层的依赖关系。
  • 在一个或多个业务逻辑组件实现全部或部分由Web层所需要的接口。引用一些组件包含它们所依赖的对象的接口。包含定义的绑定他们提​​供的部件的模块。
  • 在一个或多个组件定义接口的业务逻辑层的依赖关系。
  • 实现业务逻辑层(即直接从业务逻辑提供了例如数据)的依赖,可能还有一些web层的一个或多个组件。含有它们提供的部件的模块。
  • 在一个使用引导程序加载这些程序集的模块 kernel.Load(*。DLL)或类似的。
  • One or more web tier assemblies: containing the controllers, views and bindings for the web tier. Each of the assembly references some assemblies that define the interfaces it depends on.
  • One or more assemblies that define the interfaces for the dependencies of the web tier.
  • One or more business logic assemblies implementing all or some of the interfaces required by the web tier. Referencing some assemblies that contain the interfaces of the objects they depend on. Containing the modules that define the bindings for the components they provide.
  • One or more assemblies that define the interfaces for the dependencies of the business logic tier.
  • One or more assemblies that implement the the dependencies of the business logic tier and possibly some of the web tier (e.g. data that is directly provided with out the business logic). Containing the modules of the components they provide.
  • One bootstrapper loading the modules of these assemblies using kernel.Load("*.dll") or similar.

这样做的优点是:

  • 从Web层没有提到商业逻辑层和数据层
  • 从业务逻辑层没有可参考的数据层
  • 在每一层更换不会对他人造成任何影响

这篇关于最佳位置流利IOC配置/模块(目前正在Ninject)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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