I'm a newbie with WPF/MVVM/Entity Framework and it's a lot of concepts to handle simultaneously. I'm creating a WPF app with only one main View, which is splitted in 2 parts : 1 UserControl for a Master view of my data, another userControl for the detailed view. All data is stored in database generated via Entity Framework entity model.
我是WPF / MVVM / Entity Framework的新手,同时处理很多概念。我正在创建一个只有一个主视图的WPF应用程序,它分为两部分:1个UserControl用于我的数据的主视图,另一个用户控件用于详细视图。所有数据都存储在通过Entity Framework实体模型生成的数据库中。
So far I managed to do what I wanted (I use MVVM light) : databinding, commands, eventToCommand... I use following architecture in 1 VS project : 1 folder for Views, 1 for ViewModels, and 1 for Entities definition.
到目前为止,我设法做了我想要的(我使用MVVM灯):数据绑定,命令,eventToCommand ...我在1个VS项目中使用以下架构:1个用于Views的文件夹,1个用于ViewModels,1个用于实体定义。
I pass data from master to detail userControl using MVVM Light Messaging, and when I try to update one entity, I encountered exception telling me that I can't update because I try to update one object linked to an ObjectContext (declared in MasterViewModel) with one object from another one (declared in DetailedViewModel)
我使用MVVM Light Messaging将数据从master传递给详细userControl,当我尝试更新一个实体时,我遇到异常告诉我无法更新,因为我尝试更新链接到ObjectContext的一个对象(在MasterViewModel中声明)来自另一个对象的一个对象(在DetailedViewModel中声明)
How can I share EF ObjectContext between ViewModels? I read some stuff about repositories or UnitOfWork but I didn't really manage to see how I could use it in my case.
如何在ViewModels之间共享EF ObjectContext?我读了一些关于存储库或UnitOfWork的东西,但我并没有真正看到我如何在我的情况下使用它。
Subsidiary question : what's the best practice to access entities with EF and a n-tier app? Is the repository the answer? COnsidering the fact that classes already exists, should I have a "Model" folder in my solution architecture?
附属问题:使用EF和n层应用程序访问实体的最佳做法是什么?存储库是答案吗?考虑到类已存在的事实,我的解决方案体系结构中是否应该有“Model”文件夹?
1 个解决方案
#1
5
The answer is in the two design patterns you mention.
答案是你提到的两种设计模式。
A Repository is a design pattern that helps you to create a single access point to your data. For example a CustomerRepository which has functions like GetById(int customerId)
, Update(Customer customer)
, Delete(Customer customer)
and Add(Customer customer)
and, depending on your specific flavor of implementing the pattern, other more specific functions for handling data that involves a customer.
存储库是一种设计模式,可帮助您创建数据的单一访问点。例如,CustomerRepository具有GetById(int customerId),Update(客户客户),Delete(客户客户)和Add(客户客户)等功能,并且根据您实现模式的具体风格,还有其他更具体的处理数据的功能涉及客户。
In a regular application you would have a couple of Repositories that will give you access to different kinds of data. In a business function you would then use some of those repositories to build functionality.
在常规应用程序中,您将拥有几个存储库,可以访问不同类型的数据。在业务功能中,您将使用其中一些存储库来构建功能。
Then the UnitOfWork pattern comes around, because this helps you to group a set of related operations. The Unit of Work keeps track of the changes until you save them to the database as a whole. (The ObjectContext in EF is an implementation of the UoW pattern)
然后出现UnitOfWork模式,因为这可以帮助您对一组相关操作进行分组。工作单元会跟踪更改,直到您将它们保存到整个数据库中。 (EF中的ObjectContext是UoW模式的实现)
In your example showing a master form and then loading and updating the details of one of those items, is a group of related operations that you want to update together.
在显示主表单然后加载和更新其中一个项的详细信息的示例中,是一组要一起更新的相关操作。
This means that you should use one UoW for the master and the details view.
这意味着您应该使用一个UoW作为主视图和详细信息视图。
This is nice article which shows the basics of how an implementation of the Repository and UoW patterns could look like when using EF.
这篇文章很好,它展示了使用EF时Repository和UoW模式的实现方式的基础知识。
Here you can find an explanation of the Repository pattern and here of the Unit Of Work (those references are both from Patterns of Enterprise Applications, a really good book if you want to learn more)
在这里你可以找到Repository模式的解释和这里的工作单元(这些参考都来自企业应用程序模式,如果你想了解更多,这是一本非常好的书)
#1
5
The answer is in the two design patterns you mention.
答案是你提到的两种设计模式。
A Repository is a design pattern that helps you to create a single access point to your data. For example a CustomerRepository which has functions like GetById(int customerId)
, Update(Customer customer)
, Delete(Customer customer)
and Add(Customer customer)
and, depending on your specific flavor of implementing the pattern, other more specific functions for handling data that involves a customer.
存储库是一种设计模式,可帮助您创建数据的单一访问点。例如,CustomerRepository具有GetById(int customerId),Update(客户客户),Delete(客户客户)和Add(客户客户)等功能,并且根据您实现模式的具体风格,还有其他更具体的处理数据的功能涉及客户。
In a regular application you would have a couple of Repositories that will give you access to different kinds of data. In a business function you would then use some of those repositories to build functionality.
在常规应用程序中,您将拥有几个存储库,可以访问不同类型的数据。在业务功能中,您将使用其中一些存储库来构建功能。
Then the UnitOfWork pattern comes around, because this helps you to group a set of related operations. The Unit of Work keeps track of the changes until you save them to the database as a whole. (The ObjectContext in EF is an implementation of the UoW pattern)
然后出现UnitOfWork模式,因为这可以帮助您对一组相关操作进行分组。工作单元会跟踪更改,直到您将它们保存到整个数据库中。 (EF中的ObjectContext是UoW模式的实现)
In your example showing a master form and then loading and updating the details of one of those items, is a group of related operations that you want to update together.
在显示主表单然后加载和更新其中一个项的详细信息的示例中,是一组要一起更新的相关操作。
This means that you should use one UoW for the master and the details view.
这意味着您应该使用一个UoW作为主视图和详细信息视图。
This is nice article which shows the basics of how an implementation of the Repository and UoW patterns could look like when using EF.
这篇文章很好,它展示了使用EF时Repository和UoW模式的实现方式的基础知识。
Here you can find an explanation of the Repository pattern and here of the Unit Of Work (those references are both from Patterns of Enterprise Applications, a really good book if you want to learn more)
在这里你可以找到Repository模式的解释和这里的工作单元(这些参考都来自企业应用程序模式,如果你想了解更多,这是一本非常好的书)