gpt4 book ai didi

c# - 使用 Unity/Prism/MVVM 的 ObjectContext 构造函数注入(inject)

转载 作者:行者123 更新时间:2023-11-30 15:09:59 25 4
gpt4 key购买 nike

我使用带有 MVVM 模式和 Prism 的 WPF 开发了一个应用程序。 View 被添加到 ModuleCatalog 中, View 模型被注册到一个统一容器中。为此,我使用了一个 Bootstrapper,它负责创建 shell、配置统一容器和模块目录。
现在的问题是,如何将我的 EntityContext 注入(inject)到多个 View 模型中。
首先是 Bootstrap :

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

<pre><code> protected override void ConfigureContainer()
{
base.ConfigureContainer();
Container.RegisterType<EntityContext >("Context");
Container.RegisterType<PersonViewModel>(new InjectionConstructor(
new ResolvedParameter<EntityContext >("Context")));
}

protected override IModuleCatalog GetModuleCatalog()
{
ModuleCatalog catalog = new ModuleCatalog();
catalog.AddModule(typeof(PersonModule));
return catalog;
}
</code></pre>

<p></p>

View 模型看起来像那样(摘录)


public class PersonViewModel : ViewModelBase, IDataErrorInfo
{
private Person _person;
private PersonRepository _repository;
readonly EntityContext _context;<p></p>

<pre><code> public PersonViewModel(EntityContext context)
{
_context = context;
_person = new Person();
_repository = new PersonRepository(context);
}
</code></pre>

<p></p>

模块:


public class PersonModule : IModule
{
private readonly IRegionManager regionManager;<p></p>

<pre><code> public PersonModule(IRegionManager regionManager)
{
this.regionManager = regionManager;
}

public void Initialize()
{
regionManager.RegisterViewWithRegion("PersonData", typeof(PersonView));
}

}
</code></pre>

<p></p>

View 代码隐藏:


public partial class PersonView : UserControl
{
private PersonViewModel _vm;<p></p>

<pre><code> public PersonView()
{
InitializeComponent();
}

[Dependency]
public PersonViewModel VM
{
get
{
return this.DataContext as PersonViewModel;
}
set
{
_vm = value;
this.DataContext = _vm;
}
}
}
</code></pre>

<p></p>
我不确定我的方法原则上是否有效,但为了保存对数据库的更改,我需要了解对它所做的更改的上下文。现在它显然无法正常工作,因为发生了 ModuleInitializeException。堆栈跟踪:
初始化模块“PersonModule”时发生异常。
- 异常消息是:尝试将 View 添加到区域“PersonData”时发生异常。
- 最可能导致异常的是:“System.InvalidOperationException:EntityContext 类型具有多个长度为 1 的构造函数。无法消除歧义。
bei Microsoft.Practices.ObjectBuilder2.ConstructorSelectorPolicyBase 1.FindLongestConstructor(Type typeToConstruct)<br/>
bei Microsoft.Practices.ObjectBuilder2.ConstructorSelectorPolicyBase
1.SelectConstructor(IBuilderContext上下文)
在 Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.PreBuildUp(IBuilderContext 上下文)
bei Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)'.
但还要检查 InnerExceptions 以获取更多详细信息或调用 .GetRootException()。 - 试图从中加载模块的程序集是:App,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null
检查异常的 InnerException 属性以获取更多信息。如果在 DI 容器中创建对象时发生异常,您可以使用 exception.GetRootException() 来帮助定位问题的根本原因。

如果该问题还有其他解决方案,我对此持开放态度,但我或多或少想使用所提供的基本结构。
提前致谢。

最佳答案

您必须配置容器以消除 EntityContext 构造的歧义:

Container.RegisterType<EntityContext >("Context", new InjectionConstructor(...))

关于c# - 使用 Unity/Prism/MVVM 的 ObjectContext 构造函数注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3669213/

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