需要帮助将 PRISM Unity Module Init 转换为 PRISM MEF Module Init

发布于 2024-12-06 05:54:26 字数 2242 浏览 2 评论 0原文

我需要帮助转换以下类以在我正在开发的程序中使用。最初是来自 IdeaBlade 的一个演示程序,名为 “PRISM EXPLORER”,基于 Unity。我需要帮助将一个零件从 UNITY 转换为 MEF。我处理了其他一切。就卡在这个上了。我已经用 MEF“[EXPORT(typeof(XXX))]”标记了我的类,我认为我需要以某种方式使用“ComposeExportedValue”。令人困惑的部分是找到这句话的对应词:

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

谢谢!

以下是我需要转换的整个类。您可以在这里找到原件:Ideablade PRISM 页面

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");
    //}

  }
}

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");
    //}

  }
}

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

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

发布评论

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

评论(1

丿*梦醉红颜 2024-12-13 05:54:26

CompositionContainer 上对应的两个方法是 ComposeExportedValue(...),它允许您将特定实例添加到容器,以及 GetExportedValue(...),它允许您将特定实例添加到容器中。 T>(...) 从容器中获取 T 的实例。

如果您可以以减少服务位置使用的方式设计您的类型,并尝试并更喜欢构造函数注入,那么您的代码将更容易维护和测试。例如,您的代码可以转换为:

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

    }
}

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)
    {

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