编程语言
首页 > 编程语言> > c#-Ninject模块或组织接线依赖

c#-Ninject模块或组织接线依赖

作者:互联网

我已经开始使用Ninject了,从一个截屏中可以看到,以下是设置绑定的方法:

class MyModule : StandardModule {

    public override void Load() {
        Bind<IInterface>().To<ConcreteType>();
        // More bindings here...
    }
}

这一切都很好.

但是,假设您在一个应用程序中使用了一百个对象.那意味着这将有一百个绑定.这个对吗?

其次,我假设给定这样一个应用程序,可以将其划分为诸如GUI,数据库,服务等子系统.

然后,您将为每个子系统创建一个自定义模块吗?

> GUI模块
>数据库模块
>服务模块
> …

对于每个模块,您将拥有所需的正确绑定.我在右边的页面上吗?

最后,此绑定是否全部发生在Main或应用程序的入口点?

解决方法:

However suppose you have one hundred
objects used in an application. That
would mean this would have one hundred
bindings. Is this correct?

是的,是一百个已注册的组件,但不一定是一个一个地注册.有一个Convention extension for Ninject,可让您根据一些已定义的规则扫描程序集并注册类型.以this test为例.

Would you then create a custom module
for each subsystem

同样,不一定.您可能只想在一次约定注册中注册所有存储库(仅用于命名).

For each module you’d have the correct
bindings that they required.

与任何“模块”(无论是程序集,类,应用程序)一样,coupling and cohesion的概念也适用于此.最佳做法是保持低耦合(不要过多地依赖于其他模块)和高内聚力(模块内的所有组件都必须朝着一个共同的目标努力)

Finally would this binding all occur
in Main or the entry point for your
application?

是的,请参见this related question.

标签:ninject,c,ioc-container
来源: https://codeday.me/bug/20191106/2001171.html