Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upgrade to Enterprise Library 6.0 giving issues with 'EnterpriseLibraryContainer'

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

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

Cannot resolve EnterpriseLibraryContainer

I found another post that speaks about the EnterpriseLibraryCOntainer on stackoverflow

In the upgrade notes of Enterprise Library it states:

“The name ‘EnterpriseLibraryContainer’ does not exist in the current context

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();

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

IUnityContainer container = new UnityContainer?

Thanks for your help

like image 689
Jonathan V Avatar asked Mar 24 '23 09:03

Jonathan V


1 Answers

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();

and register the LogWriter with the UnityContainer:

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

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.

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()).

like image 72
Randy supports Monica Avatar answered Apr 26 '23 20:04

Randy supports Monica