Fab*_*ian 7 c# architecture autofac
我正在开发一个由许多模块组成的应用程序,其中一些模块依赖于其他模块.我现在决定使用Autofac来解决循环依赖关系并改进体系结构.
要配置autofac,我使用xml方法(http://code.google.com/p/autofac/wiki/XmlConfiguration).
现在我不确定如何实现Autofac.我是否需要在应用程序的每个模块中引用autofac?这意味着我每次想要解决依赖关系时都必须注册所有组件...
ContainerBuilder builder = new ContainerBuilder();
builder.RegisterModule(new ConfigurationSettingsReader("autofac", configPath));
IContainer container = builder.Build();
IWhatever w = container.Resolve<IWhatever>();
Run Code Online (Sandbox Code Playgroud)
这是这样做的吗?
或者在单独的模块中包装Autofac更好吗?使用这种方法,我只需要注册一次模块(当应用程序启动时),并且可以使用包装的Autofac来解决依赖关系......
IWhatever w = container.Resolve<IWhatever>();
Run Code Online (Sandbox Code Playgroud)
我希望有人能告诉我使用Autofac的最佳方法.
谢谢!
如果您想使用autofac模块,每个项目都需要依赖autofac核心包.
使用如下所述的autofac模块:http://autofac.readthedocs.io/en/latest/configuration/modules.html
更新
我在这里描述了为什么要使用模块:http://www.codeproject.com/Articles/386164/Get-injecteded-into-the-world-of-inverted-dependenci