如何在多项目解决方案中使用 Unity 进行解析

发布于 2024-09-05 01:14:25 字数 692 浏览 4 评论 0原文

在一个新的 WPF 项目(VS2010)中,我第一次使用 Unity 2。 在此项目中,我使用以下结构:

Solution

WPF Project

Class Library1

Class Library2

Class Library 3 ....

使用 Unity 注册不同类型是在 WPF 项目中使用以下代码片段完成的:

IUnityContainer container = new UnityContainer()
                            .RegisterType<IObjectContext, ObjectContextAdapter>()
                            .RegisterType<IConnectionStringProvider, ConnectionStringProvider>()
                            .RegisterType(typeof(IRepository<>), typeof(Repository<>));

现在假设我想获取存储库<订单>构造函数注入已在 Class Library1 中解决。 显然该容器在其他项目中是未知的!

我该怎么做?

In a new WPF project (VS2010) i 'm using Unity 2 for the first time.
In this project i use the following structure:

Solution

WPF Project

Class Library1

Class Library2

Class Library 3 ....

Registering the different types using Unity is done in WPF Project using the following snippet:

IUnityContainer container = new UnityContainer()
                            .RegisterType<IObjectContext, ObjectContextAdapter>()
                            .RegisterType<IConnectionStringProvider, ConnectionStringProvider>()
                            .RegisterType(typeof(IRepository<>), typeof(Repository<>));

Let's say now that i would like to get the Repository<Orders> constructor-injected resolved in Class Library1.
Apparently the container is not known in the other projects!

How would i do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸 2024-09-12 01:14:25

我基本上同意 Chris 的回答,但我认为配置文件很讨厌(尤其是对于 Unity),因此这里有一个解决方案,允许您使用运行时配置而不使用循环引用。我们将通过注册表来完成此操作。

创建一个将包含 IConfigureUnity 的基础设施项目。

public interface IConfigureUnity
{
    public void Configure(UnityContainer container);
}

您的每个类库项目将负责实现此接口以注册其自己的类。

public class RegistryForSomeClassLibrary : IConfigureUnity
{
    public void Configure(UnityContainer container)
    {
        container
            .RegisterType<IObjectContext, ObjectContextAdapter>()
            .RegisterType<IConnectionStringProvider, ConnectionStringProvider>()
            .RegisterType(typeof(IRepository<>), typeof(Repository<>));
    }
}

然后,在您的 WPF 项目中,您需要创建容器并应用这些注册表。

var container = new UnityContainer();
new RegistryForSomeClassLibrary().Configure(container);
new RegistryForAnotherClassLibrary().Configure(container);

现在您拥有一个完全配置的容器实例,无需任何配置文件。

I mostly agree with Chris' answer, but I think config files are icky (especially for Unity) so here's a solution that will allow you to use runtime config w/o circular references. We're going to do this with registries.

Create an Infrastructure project that will contain IConfigureUnity.

public interface IConfigureUnity
{
    public void Configure(UnityContainer container);
}

Each of your class library projects will be responsible for implementing this interface to register it's own classes.

public class RegistryForSomeClassLibrary : IConfigureUnity
{
    public void Configure(UnityContainer container)
    {
        container
            .RegisterType<IObjectContext, ObjectContextAdapter>()
            .RegisterType<IConnectionStringProvider, ConnectionStringProvider>()
            .RegisterType(typeof(IRepository<>), typeof(Repository<>));
    }
}

Then in your WPF project you'll need to create the container and apply these registries.

var container = new UnityContainer();
new RegistryForSomeClassLibrary().Configure(container);
new RegistryForAnotherClassLibrary().Configure(container);

Now you have a fully configured container instance w/o any config files.

七秒鱼° 2024-09-12 01:14:25

要在此场景中让多个项目使用同一个 UnityContainer,您需要一个“通用”项目,其中包含您的 UnityContainer 并公开它,以便所有其他项目都可以访问它。

WPF 项目

类库 1

类库 2

类库 3

公共库(UnityContainer 位于此处)

为了避免循环项目依赖项,我建议使用 通过配置文件进行 Unity 设计时配置,而不是运行时配置(如示例中所示)。否则,您的公共库将必须引用包含它解析的所有类型的项目,而这些项目反过来又将依赖于公共库(因为这可能是您公开 UnityContainer 实例的地方)。您也许可以使用运行时配置让它工作,但我还没有尝试过;我确实知道设计时配置是有效的,因为我已经使用与此完全相同的模型完成了一些项目。

To have multiple projects make use of the same UnityContainer in this scenario, you need a "common" project that contains your UnityContainer and exposes it such that all other projects can access it.

i.e.

WPF Project

Class Library 1

Class Library 2

Class Library 3

Common Library (UnityContainer lives here)

To avoid circular project dependencies, I'd recommend using Unity design-time configuration via a configuration file instead of run-time configuration (as you have in your example). Otherwise your Common Library will have to reference the projects that contain all of the types it resolves and those projects, in turn, would be dependent on the Common Library (since that is presumably where you would expose the UnityContainer instance). You may be able to get it to work using run-time configuration, but I have not tried that; I do know that the design-time configuration works as I have done a few projects using a model exactly like this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文