Question

I need help converting the following class for use in a program that I am developing. The original was a demo program from IdeaBlade called "PRISM EXPLORER" based on Unity. I need help converting one part from UNITY to MEF. I handled everything else. Just stuck on this one. I already marked my classes with the MEF "[EXPORT(typeof(XXX))]" and I think I need to use the "ComposeExportedValue" somehow. The confusing part is finding the equivelant for this line:

var provider = 
    (IEntityManagerProvider) _container.Resolve<IPersistenceGateway>();
  _container.RegisterInstance<IEntityManagerProvider>(provider);

THANKS!

The following is the entire class I need to convert. You can find the original here: Ideablade PRISM Page

using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using PrismExplorer.Infrastructure;
namespace ModelExplorer.Explorer {
  public class ExplorerModule : IModule {

    private readonly IUnityContainer _container;

    public ExplorerModule(IUnityContainer container) {
      _container = container;
    }

    public void Initialize() {
      InitializeContainer();
      SetViews();
    }

    // ToDo: Consider getting from configuration
    private void InitializeContainer() {
      RegisterGatewayAndEntityManagerProvider();

      _container.RegisterType<IQueryRepository, QueryRepository>(
        new ContainerControlledLifetimeManager()); // singleton
    }

    private void RegisterGatewayAndEntityManagerProvider() {
      _container.RegisterType<IPersistenceGateway, PrismExplorerPersistenceGateway>(
        new ContainerControlledLifetimeManager()); // singleton

      var provider = 
        (IEntityManagerProvider) _container.Resolve<IPersistenceGateway>();
      _container.RegisterInstance<IEntityManagerProvider>(provider);
    }

    private void SetViews() {
      var regionManager = _container.Resolve<IRegionManager>();

      var view = _container.Resolve<ExplorerView>();
      regionManager.AddToRegion(RegionNames.MainRegion, view);

      regionManager.RegisterViewWithRegion(RegionNames.MainRegion, typeof(ExplorerView));
    }


    // Destructor strictly to demonstrate when module is GC'd
    //~MevModule() {
    //  System.Console.WriteLine("Goodbye, MevModule");
    //}

  }
}
Was it helpful?

Solution

The two corresponding methods on a CompositionContainer are ComposeExportedValue<T>(...), which allows you to add a specific instance to the container, and GetExportedValue<T>(...) which gets an instance of T from the container.

If you can design your types in a way to reduce this use of service location and try and prefer constructor injection, it will make your code much easier to maintain and test. E.g., could your code be transformed into:

[Export(typeof(IModule))]
public class ExplorerModule : IModule
{
    [ImportingConstructor]
    public ExplorerModule(IPersistenceGateway gateway)
    {

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top