I develop an application using WPF with MVVM pattern and Prism. The views are added to the ModuleCatalog and the viewmodels are registered to a unity container. For that I'm using a Bootstrapper which is responsible creating the shell, configuring the unity container and the module catalog.
The question is now, how injecting my EntityContext to the several viewmodels.
First the Bootstrapper:
public class Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
Shell shell = Container.Resolve();
shell.Show();
return shell;
}
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;
}
视图模型长这样(摘录)
public class PersonViewModel : ViewModelBase, IDataErrorInfo
{
private Person _person;
private PersonRepository _repository;
readonly EntityContext _context;
public PersonViewModel(EntityContext context)
{
_context = context;
_person = new Person();
_repository = new PersonRepository(context);
}
The module:
public class PersonModule : IModule
{
private readonly IRegionManager regionManager;
public PersonModule(IRegionManager regionManager)
{
this.regionManager = regionManager;
}
public void Initialize()
{
regionManager.RegisterViewWithRegion("PersonData", typeof(PersonView));
}
}
The view code-behind:
public partial class PersonView : UserControl
{
private PersonViewModel _vm;
public PersonView()
{
InitializeComponent();
}
[Dependency]
public PersonViewModel VM
{
get
{
return this.DataContext as PersonViewModel;
}
set
{
_vm = value;
this.DataContext = _vm;
}
}
}
我不确定我的方法在原则上是否有效,但为了保存对数据库的更改,我需要了解对其所做更改的上下文。目前显然并未工作,因为出现了ModuleInitializeException。堆栈跟踪:
初始化模块'PersonModule'时发生异常。
- 异常消息是:尝试向区域'PersonData'添加视图时发生异常。
- 最有可能引起异常的是:'System.InvalidOperationException: 类型EntityContext具有多个长度为1的构造函数。无法消除歧义。
在Microsoft.Practices.ObjectBuilder2.ConstructorSelectorPolicyBase
1.FindLongestConstructor(Type typeToConstruct)
在Microsoft.Practices.ObjectBuilder2.ConstructorSelectorPolicyBase
1.SelectConstructor(IBuilderContext context)在Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.PreBuildUp(IBuilderContext context)
在Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)'中。
但也要检查InnerExceptions以获取更多详细信息或调用.GetRootException()。 - 模块尝试加载的程序集是:App,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null
若要获取更多信息,请检查异常的InnerException属性。如果异常发生在DI容器中创建对象时,您可以使用exception.GetRootException()来帮助定位问题的根本原因。
如果有其他解决该问题的方法,我很乐意尝试,但我想基本结构或多或少地使用它。
提前致谢。