不理解的地方,以在系统架构创建IOC容器 [英] Not understanding where to create IoC Containers in system architecture
问题描述
说我有以下4 .NET程序集:
Say I have the following 4 .net assemblies:
- 的WinForms UI
- 商业逻辑
- SQL Server数据访问(实施一个IRepository)
- 通用接口(IRepository等的定义。)
我的商业逻辑(2)向使用构造函数依赖注入通过IRepository数据访问层(3)(4)中定义的呼叫。然而,当我ceate的业务对象,我需要在实际存储库来传递。我通过在我的商业逻辑层的单件类返回当前在实施IRepository使用的具体对象做到这一点。我来的结论是,这是一件坏事,因为我的商业逻辑层现在有参考3以及4。
My business logic (2) makes calls to the data access layer (3) through IRepository (defined in 4) using constructor dependency injection. However when I ceate a business object I need to pass in an actual repository. I do this by having a singleton class in my business logic layer return the currently in use concrete object implementing IRepository. I am coming to the conclusion that this is a bad thing, as my business logic layer now has to reference 3 as well as 4.
我想我需要一个IOC容器,但问题是我创建/把它当作好像无论我创建这个(1 - UI)?还需要保持一个参考3(SQL Server的数据访问)。难道我不只是移动问题,而不是实现实际脱钩?
I think I need a IoC Container but the question is where I create/put it as it seems that wherever I create this (1 - UI)? will also need to hold a reference to 3 (SQL Server Data Access). Am I not just moving the problem rather than achieving actual decoupling?
我在UI创建IOC容器。或通过其他新的组装揭露它。
Do I create the IoC Container in the UI. Or expose it through another new assembly.
(我使用C#,.NET 3.5和AutoFac)
(I'm using C#, .net 3.5 and AutoFac)
感谢。
推荐答案
IoC容器,一般应在主体项目(应用程序入口点)创建。对于Windows.Forms的应用程序是这样的EXE项目。
IoC container generally should be created in the host project (application entry point). For the Windows.Forms application that's the exe project.
一般来说,在简单的解决方案(在10个项目),只有一台主机的项目应该有国际奥委会库的引用。
Generally in simple solutions (under 10 projects), only a host project should have a reference to IoC library.
PS:<一href=\"http://abdullin.com/journal/2009/2/12/structuring-net-applications-with-autofac-ioc.html\">Structuring .NET应用程序使用Autofac IoC容器
这篇关于不理解的地方,以在系统架构创建IOC容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!