EnterpriseLibraryContainer

EnterpriseLibraryContainer

本文介绍了升级到Enterprise Library 6.0,使“ EnterpriseLibraryContainer”出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级到Enterprise Library 6.0后,我遇到以下问题:

After upgrading to Enterprise Library 6.0 I'm having the following problem:

私有静态IUnityContainer容器= EnterpriseLibraryContainer.Current.GetInstance();

private static IUnityContainer container = EnterpriseLibraryContainer.Current.GetInstance();

我发现了另一篇关于

在企业库的升级说明中指出:

In the upgrade notes of Enterprise Library it states:

企业库的版本6
中所有块的引导代码已更改。这些块不再使用Unity来管理
的初始化和配置,并且每个块现在都包含自己的
引导代码。对
EnterpriseLibraryContainer.Current.GetInstance方法的任何调用以从其中一个企业库块中解析
类型的调用都应替换为特定于块的引导程序代码
。例如,要基于app.config文件中的配置创建LogWriter
实例,现在您可以
使用以下代码:LogWriterFactory logWriterFactory = new
LogWriterFactory(); var logWriter = logWriterFactory.Create();

The bootstrapping code for all of the blocks has changed in version 6 of Enterprise Library. The blocks no longer use Unity to manage the initialization and configuration, and each block now includes its own bootstrapping code. Any calls to the EnterpriseLibraryContainer.Current.GetInstance method to resolve a type from one of the Enterprise Library blocks should be replaced with the block specific bootstrap code. For example, to create a LogWriter instance based on configuration in the app.config file, you can now use the following code: LogWriterFactory logWriterFactory = new LogWriterFactory(); var logWriter = logWriterFactory.Create();

但是我不知道如何处理IUnityContainer。
我可以只使用

But I don't know how to handle this in the case of IUnityContainer.Could I just use

感谢您的帮助

推荐答案

典型的方法是引导块,注册

The typical approach would be to bootstrap the block, register the appropriate objects with Unity and have Unity inject the dependencies.

例如,如果您正在使用日志记录,则可以引导该块:

For example, if you are using logging then you would bootstrap the block:

LogWriterFactory logWriterFactory = new LogWriterFactory();
LogWriter logWriter = logWriterFactory.Create();

并向UnityContainer注册LogWriter:

and register the LogWriter with the UnityContainer:

IUnityContainer container = new UnityContainer();
// Register LogWriter as singleton
container.RegisterInstance<LogWriter>(logWriter);

如果您将EnterpriseLibraryContainer用作服务定位器,并希望继续使用相同的方法,则可以创建/包装服务定位器实现或创建静态帮助器方法。

If you were using the EnterpriseLibraryContainer as a service locator and wish to keep using that same approach then you could create/wrap a service locator implementation or create a static helper method. Unity comes with UnityServiceLocator which you could reuse.

如果不使用Unity,另一种方法是引导块,然后替换对<$ c $的调用。 c> EnterpriseLibraryContainer.Current.GetInstance<>()和静态外观方法(例如 Logger.Write())。

If you aren't using Unity, another approach would be to bootstrap the blocks and then replace the calls to EnterpriseLibraryContainer.Current.GetInstance<>() with the static facade methods (e.g. Logger.Write()).

这篇关于升级到Enterprise Library 6.0,使“ EnterpriseLibraryContainer”出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-19 03:38