本文介绍了如何使用/配置统一集装箱国际奥委会在我的处境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦,实现统一国际奥委会到我的项目从配置文件中读取。

I have some trouble implementing the Unity IOC into my project reading from config file.

下面是我

1)ClasslibraryA

1) ClasslibraryA

2)ClasslibraryB引用ClasslibraryA

2) ClasslibraryB that references ClasslibraryA

3)WinForms应用程序,它引用ClasslibraryB

3) Winforms App that references ClasslibraryB

注:SomeOther的应用程序将引用ClassLibraryA,例如。 Web服务。

Note: SomeOther app will reference ClassLibraryA, eg. a web service.

ClasslibraryA将不得不被配置为IOC取决于它被使用。例如。的IDataSource将是不同的,如果它是所谓的Web服务,当它从一个本地的应用程序调用。

ClasslibraryA will have to be configured for IOC depending on where it is used.eg. IDataSource will be different if it is called in the web service and when it is called from a local app.

ClasslibraryB将具有其自己的一组相关性,以及对由主应用程序被注入,在这种情况下,WinForms应用程序。 ClasslibraryB将实例在一个循环中许多ClasslibraryA对象。

ClasslibraryB will have its own set of dependencies as well to be injected by the main application, in this case, the winforms app. ClasslibraryB will instantiate many ClasslibraryA objects in a loop.

Winforms应用程序将包含具体落实的ClassLibraryB的依赖关系的实现和Container.Configure此处应该叫什么名字?

Winforms app will contain the concrete implementation of the ClassLibraryB's dependancies implementation and Container.Configure should be called here?

我的问题是,

在和我在哪里调用应用程序中的Container.Configure?
我需要的子容器的所有子库层/层?
如果classlibraryB或的WinForms实现具体类ClasslibraryA被注入到classlibraryA?我应该组中的每个层/层的IOC配置成不同的容器的名字在配置文件?

When and where do I call the Container.Configure in the application?
Do I need child container for all sub library tiers/layers?
Should classlibraryB or the winforms implement the concrete class for ClasslibraryA to be injected into classlibraryA?Should I group each layer/tier's IOC config into a different "Container" name in the config file?

推荐答案

每个应用程序拥有一个单独的容器并负责配置的容器实例。配置和调用解析应该发生尽可能接近到应用程序的入口点。这就是所谓的构成根

Each application owns a separate container and is responsible for configuring that container instance. Configuration and the call to resolve should happen as close as possible to the application's entry point. This is called the Composition Root.

您的Windows窗体应用程序应该拥有,配置和解决容器在引导code。这有可能是其他应用程序也使用ClassLibraryA是完全无关的。

Your Windows Forms application should own, configure and resolve the container in its bootstrapping code. That there might be other applications that also use ClassLibraryA is totally irrelevant to it.

Web服务应该拥有另一个容器中,并对其进行配置,以适应其需要。它知道任何有关Windows窗体应用程序。

The Web Service should own another container and configure it to suit its needs. It knows nothing about the Windows Forms application.

下面是关于组成根源的详细信息:

Here is more information about Composition Roots:

  • Where我应该做的依赖注入Ninject 2?
  • Design - 应该在哪里使用温莎
  • 在当前注册对象
  • Where should I do dependency injection with Ninject 2?
  • Design - Where should objects be registered when using Windsor

所有的库应该是完全的 DI容器无关这样就可以使用任何容器(及任何容器实例)来连接所有的依赖关系。

All the libraries should be completely DI Container-agnostic so that you can use any container (and any container instance) to wire up all the dependencies.

在这里看到更多的细节:

See here for more details:

  • Dependency注入(DI)"友好"库
  • Replace Spring.Net IoC和另一个容器(如Ninject)
  • Dependency Inject (DI) "friendly" library
  • Replace Spring.Net IoC with another Container (e.g. Ninject)

正如您可能会注意到,这些是一般原则和适用于DI整体 - 不只是统一

As you may notice, these are general principles and apply to DI overall - not just to Unity.

这篇关于如何使用/配置统一集装箱国际奥委会在我的处境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 20:03