gpt4 book ai didi

c# - 如何从 Prism 应用程序的各个模块中的 Unity 容器中获取相同的对象实例?

转载 作者:太空宇宙 更新时间:2023-11-03 23:22:43 33 4
gpt4 key购买 nike

我有一个 Prism 应用程序,其中包含三个模块:

  • SharedServiceModule(我正在使用 SharedServices 在模块之间进行通信)
  • 模块A
  • 模块B

SharedServiceModule 只是有接口(interface)和它的实现 CommonService:

public interface ICommonService
{
string SomeStorage { get; set; }
}

public class CommonService : ICommonService
{
string fooStorage;
public string FooStorage
{
get
{
return fooStorage;
}
set
{
fooStorage = value;
OnPropertyChanged("FooStorage");
}
}
}

我想要的是使用共享服务 在模块之间创建通信。所以我在 ModuleAViewModelA 分配了 «ModuleAValue»,然后我想在 ModuleBViewModelB 中读取这个值.让我们看看细节。

我在 ModuleAViewModelA 中创建了一个 ICommonService 实例,并将值“ModuleAValue”分配给 FooStorage:

ViewModelA的方法:

unityContainer = new UnityContainer();
unityContainer.RegisterType<ICommonService, CommonService>(new ContainerControlledLifetimeManager());
IMyService someFoo = unityContainer.Resolve<ICommonService>();
someFoo.FooStorage = "ModuleAValue";//value is "ModuleAValue" in FooStorage

然后我想在 ModuleBviewModelB 中读取这些数据。但是 FooStorage 的值不是“模块 A”,而是空值:

ViewModelB的方法:

IUnityContainer unityContainer=new UnityContainer//creation of UnityContainer in ModuleB
ICommonService someFoo = unityContainer.Resolve<CommonService>();
string str=someFoo.FooStorage;//value is empty in
FooStorage, but it should be "ModuleAValue"

我的 Bootstrap 是:

public class Bootstrapper:UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.Resolve<Shell>();
}

protected override void InitializeShell()
{
base.InitializeShell();
App.Current.MainWindow = (Window)Shell;
App.Current.MainWindow.Show();
}

protected override void ConfigureContainer()
{
base.ConfigureContainer();
Container.RegisterType<IShellViewModel, ShellViewModel>();
RegisterTypeIfMissing(typeof(IMyService), typeof(MyService), true);
}

protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
{
RegionAdapterMappings mappings = base.ConfigureRegionAdapterMappings();
mappings.RegisterMapping(typeof(StackPanel), Container.Resolve<StackPanelRegionAdapter>());
return mappings;
}

protected override IModuleCatalog CreateModuleCatalog()
{
ModuleCatalog catalog = new ModuleCatalog();
catalog.AddModule(typeof(ModuleAModule));
catalog.AddModule(typeof(ModuleBModule));
return catalog;
}
}

我做错了什么?在我看来,Unity 总是创建 CommonService 的新实例。从 Unity 容器获取同一个实例时我做错了什么?

任何帮助将不胜感激!

最佳答案

您应用的 Bootstrap 会为您创建一个 UnityContainer,请参阅 UnityBootstrapper.cs:

protected virtual IUnityContainer CreateContainer()
{
return new UnityContainer();
}

Bootstrap 还应该将容器注册为 View 模型工厂:

protected override void ConfigureContainer()
{
base.ConfigureContainer();

ViewModelLocationProvider.SetDefaultViewModelFactory( type => Container.Resolve( type ) );
}

在您的模块定义类中,您可以将这个“全局”容器作为依赖注入(inject):

public class ClientModule : IModule
{
private readonly IUnityContainer _unityContainer;

public ClientModule( IUnityContainer unityContainer )
{
_unityContainer = unityContainer;
}
}

在你的模块的初始化中,你向这个容器注册类型或实例:

public void Initialize()
{
// Register services
_unityContainer.RegisterType<IGameClock, LocalGameClock>( new ContainerControlledLifetimeManager() );
_unityContainer.RegisterType<IWorldStateService, LocalWorldStateService>( new ContainerControlledLifetimeManager() );
}

在您的 View 中(在 xaml 中),您现在可以使用 ViewModelLocator.AutoWireViewModel="True" 为您的 View 自动创建 View 模型。 ViewModelLocationProvider 将使用“全局”容器来解析 View 模型(如上定义),因此所有 View 模型都将收到我们的相同实例,例如,IGameClock

辅助建议:您不想自己直接调用 Resolve。以这种方式使用它首先会破坏使用统一性的全部目的。最好将依赖项作为构造函数参数注入(inject),并且只在最高级别使用容器,即在模块初始化中。而且您永远不需要创建多个容器,以免您确切地知道自己在做什么。

关于c# - 如何从 Prism 应用程序的各个模块中的 Unity 容器中获取相同的对象实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34857532/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com