- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用带有 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。堆栈跟踪:
1.FindLongestConstructor(Type typeToConstruct)<br/>
bei Microsoft.Practices.ObjectBuilder2.ConstructorSelectorPolicyBase
1.SelectConstructor(IBuilderContext上下文)
如果该问题还有其他解决方案,我对此持开放态度,但我或多或少想使用所提供的基本结构。
提前致谢。
最佳答案
您必须配置容器以消除 EntityContext 构造的歧义:
Container.RegisterType<EntityContext >("Context", new InjectionConstructor(...))
关于c# - 使用 Unity/Prism/MVVM 的 ObjectContext 构造函数注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3669213/
首先,让我自己对必须维护 VB5 代码做出强制性的尖刻评论:是的,这很可怜,但我们可以切入正题吗?谢谢。 我不得不重新审视一些在 1998 年编写并且自 2003 年以来没有被触及的非常旧的代码。我遇
我想在 EntityFrameWork 6 中使用脚手架,但得到这个: Unable to retrieve metadata for '[myNameSpace].Models.prod'. Una
为什么我打不通DbContext.ObjectContext直接地?我可以做到((IObjectContextAdapter)context).ObjectContext 我认为内部机制是这样的接口(
我们在控制台应用程序中运行一个非常简单的函数,该函数循环遍历数据库并将表列到变量中。当然,最初它做的更多,但我们将其简化为仅列出 1 个表。 我们注意到,每次创建新的 ObjectContext 时,
我在我的应用程序中定义了三个数据模型。我的问题是,如果我需要三种不同的上下文?那么三个不同的 sqlite 文件? 目前,每次我将一条记录保存到我的数据库中时,我都会执行以下操作: managedOb
这里只是一个快速的完整性检查! 如果我在实例类中有一个静态方法,例如: public class myClass { public static void AMethod() {
我在使用 EntityFramework 4.0 创建关系时出错 对数据库的更改已成功提交,但更新对象上下文时出错。 ObjectContext 可能处于不一致状态。 内部异常消息:无法检索关系“Co
我按照此处描述的方式使用 Entity Framework : Entity framework uses a lot of memory 我意识到我需要使用“using”语句才能正常工作。当我在做的
我已经使用内存分析器对此进行了检查,没有真正的实体保留在内存中但是哈希集、字典和 EntityKey 对象——但我找不到如何断开这些引用的方法。 如此简单的问题:如何阻止上下文(或其 ObjectSt
我正在创建一个委托(delegate)来检索数据库中的所有专辑记录。我在另一个项目中使用了同样的方法,但由于某种原因,这次我遇到了错误。 我错过了一步吗?我不确定为什么会出现此错误。 代码
如何更新数据存储中的所有脏实体,并将其更改的值重置为原始存储值? 方法ObjectContext.Refresh需要将要刷新的实体作为参数。 最佳答案 以下通常有效: Context.Refresh(
有没有办法保存单个跟踪对象的更改,而不是 ObjectStateManager 中的所有对象,我的意思是: ObjectContext.SaveChanges(Contact) 最佳答案 也许您可以创
我在这里做错了什么? ... using (var ctx = ObjectContextManager.GetManager("MyDataContext")) { var idsToUpd
我有一个用 php 编写的宠物项目。我的代码所做的工作之一是加载一堆 csv 文件并将它们写入具有自动生成的主键和多部分唯一键的 MySQL 表。我无法控制和检查是否已处理某些文件的方法,因此唯一 k
我使用不同的命令和不同的参数多次调用 ObjectContext.ExecuteStoreCommand,尽管我对几个命令使用相同的参数列表(对象)。我收到以下异常: System.ArgumentE
我正在使用为每个请求创建一个 ObjectContext 的概念。从技术上讲,我将 ObjectContext 实例添加到 HttpContext.Current.Items。但我不知道如何正确地杀死
我已经看到这个问题被问了一百万次,但我遇到的每一个建议似乎都已经涵盖了。 我的 MVC 解决方案中有 Entity Framework ,我有一个“存储库”,它试图检索 MyObject 的集合: p
我不太确定 Dispose 我的 ObjectContext 的正确方法是什么。这是我的设置方式: public abstract class DataManagerBase where T:Obje
我正在尝试将大量行(>10,000,000)插入 MySQL使用 EF ObjectContext(数据库优先)的数据库。看完this question的回答后我编写了这段代码(批量保存)来插入大约
我尝试在 MVC4 中级联下拉列表。我有 2 个下拉列表 1 - 类别2 - 子类别。 当用户创建新产品时,他需要选择类别,然后选择与该类别相关的子类别。我将 ajax 与杰森一起使用。 publi
我是一名优秀的程序员,十分优秀!