【问题标题】:PRISM: Using MVVM, how to resolve or inject in a constructor objects?PRISM:使用 MVVM,如何解析或注入构造函数对象?
【发布时间】:2012-04-23 04:20:54
【问题描述】:

我正在使用 MVVM 和 PRISM。在项目中,我有一个通用接口叫IFoo,其他模块应该实现这个接口并注册它。

// Common module
public interface IFoo { }

// Module1 module
public class Foo1 : IFoo { }

然后当我初始化 module1 时,我注册我的类型并导航。

_container.RegisterType<IFoo, Foo1>(new ContainerControlledLifetimeManager());
_container.RegisterType<Object, View1>("View1");

var module = new Uri("View1", UriKind.Relative);
_regionManager.RequestNavigate("MainRegion", module);

View1 构造函数包含 viewModel,这个视图模型在它的构造函数中有:

    public ViewModel1(IFoo foo, IEventAggregator eventAggregator, IRegionManager regionManager)
    {
        ...
    }

到此为止,没关系。但后来,我需要从外部模块中获取 Foo1 。所以,我将另一个注册表设置为 Foo1 的映射名称:

_container.RegisterType<IFoo, Foo1>(new ContainerControlledLifetimeManager());
_container.RegisterType<IFoo, Foo1>("foo1", new ContainerControlledLifetimeManager());

没错,它对我有用,但我不喜欢将两个实例分开的想法。我只需要一个,并访问同一个实例。

有没有办法解决这种情况? 提前致谢。

无论如何,我附上一个 Zip,其中包含一个代表我的问题的演示。 http://www.mediafire.com/?feod8x0b952457e

【问题讨论】:

    标签: c# mvvm prism unity-container ioc-container


    【解决方案1】:

    您可以在加载模块时在引导程序中注册所有类型。

    // register all modules
    protected override void ConfigureModuleCatalog()
    {
        // get all module types
        var types = new List<Type>();
        types.Add(typeof(ModuleA));
        types.Add(typeof(ModuleB));
        types.Add(typeof(ModuleC));
    
        // register all types
        foreach (var type in types)
        {
            ModuleCatalog.AddModule(new ModuleInfo()
            {
                ModuleName = type.Name,
                ModuleType = type.AssemblyQualifiedName
            });
        }
    }
    

    然后在ConfigureContainer 中映射您以后想要访问的所有类型和/或实例。配置的容器被传递到 Module1Module 的构造函数中。

    // register all types in all modules
    protected override void ConfigureContainer()
    {
        base.ConfigureContainer();
    
        // ModuleA
        Container.RegisterType<IInterface1, Type1>();
        Container.RegisterType<IInterface2, Type2>();
    
        // ModuleB
        Container.RegisterInstance<IInterface3>("name", new Type3());
        Container.RegisterType<IInterface4, Type4>();
    
        // ModuleC
        Container.RegisterType<IInterface5, Type5>();
        Container.RegisterType<IInterface6, Type6>();
    }
    

    【讨论】:

      【解决方案2】:

      我认为您不需要注册 Foo1 两次。您使用的是ContainerControlledLifetimeManager,因此每当您向 Unity 容器请求 IFoo 实例时,它都会为您提供 Foo1 - 您无需使用名称作为键。

      所以,你在module1注册了Foo1:

      _container.RegisterType<IFoo, Foo1>(new ContainerControlledLifetimeManager());
      System.Diagnostics.Debug.Print(Foo1.GetHashCode());
      

      在你的外部模块中:

      IFoo someFoo = _container.Resolve<IFoo>();
      
      // someFoo is the same object as Foo1, so the hashcodes will be equal.
      System.Diagnostics.Debug.Print(someFoo.GetHashCode());
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-28
        • 1970-01-01
        • 2011-05-11
        • 2016-11-19
        • 2017-04-27
        • 1970-01-01
        相关资源
        最近更新 更多