在 WinForms MDI 中使用具有存储库模式的实体框架 [英] Using Entity Framework with repository pattern in WinForms MDI

查看:23
本文介绍了在 WinForms MDI 中使用具有存储库模式的实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们即将启动一个与之前的项目类似的新项目.我可以复制旧设计,但我对旧设计不太满意.

We are about to start up a new project similar to a previous one. I could just copy the old design but I am not all too satisified with the old design.

它是一个标准"业务系统(销售、库存盘点、仓储等),构建在 .Net 3.5(Winforms MDI)之上,后端带有实体框架.

It is a "standard" business system (sales,stocktaking,warehousing etc) built ontop of .Net 3.5 (Winforms MDI) with Entity Framework in the backend.

所有表单都继承自一个基本表单(它继承了 Windows.Form).该表单公开了一个名为 ObjectContext 的属性,它在第一次调用时实例化一个新的 ObjectContext.我认为这构成了一个非常好的 UnitOfWork,在每种形式中隔离了所有数据访问.

All forms inherit from a baseform (which inherits Windows.Form). The form exposes a property called ObjectContext, which on the first call instantiates a new ObjectContext. This makes up a pretty good UnitOfWork i think, having all data-access isolated in each form.

不过.

我已将所有查询和常见 CRUD 封装在poor mans 存储库"中.这些存储库作为 ObjectContext 的属性公开.

I have encapsulated all queries and common CRUD in "poor mans repositories". These Repositories are exposed as properties of the ObjectContext.

所以如果我想绑定和订购一个表格,我会打电话OrderLinesGrid = ObjectContext.OrderRepository.GetOrderLinesByID(orderID).

So if I wanted to bind and order to a form I would call OrderLinesGrid = ObjectContext.OrderRepository.GetOrderLinesByID(orderID).

OrderRepository 获取对为表单创建的 objectcontext 的引用,如下所示

The OrderRepository gets a reference to the objectcontext created for the form, like this

(在我的部分 ObjectContext 类中)

(In my partial ObjectContext class)

Private _OrderRepository as OrderRepository
Public ReadOnly Property OrderRepository as OrderRepository
Get
if _orderrepository is nothing then
_orderrepository = New OrderRepository(me)
end if
return _orderrepository
End Get
End Property

我不喜欢这个的地方是:

What I do not like about this is:

  1. 调用存储库通过 ObjectContext.因此,我做之间没有得到抽象查询和数据访问层 I愿意.

  1. The call to the repository is made through ObjectContext. Hence, I do not get abstraction between the query and the dataaccesslayer I would like.

对于我域中的每个新类型 I需要在我的中创建一个属性对象上下文

For each new type in my Domain I need to create a property in my ObjectContext

我对 OrderRepository 的调用应该只返回域对象,而不必担心它是如何持久化的.此外,我不能让每个 Repository 拥有它自己的 ObjectContext,因为这需要我在将 Country 引用到 Order.Country 属性时附加和分离对象.

My call to OrderRepository should just return domain objects and not worry about how it is persisted. Also, I cannot have each Repository have it's own ObjectContext since that would require me to Attach and Detach objects when referencing i.e Country to a Order.Country property.

如果您对此设计有任何想法和反馈,我将不胜感激:)

I would appreciate any ideas and feedback on this design :)

推荐答案

我建议你研究一下 "洋葱"原则 , 存储库模式 和 Luw 模式.网上有很多例子.

I suggest you research "onion" principle , Repository pattern and Luw pattern. There are many examples on the web.

本质上,您使用的是 POCO 模型核心项目.没有对 DAL 项目的引用.您在 CORE 项目中为 Repository 和 luw 模式声明接口.

Essentially you use POCO Model Core Project. No references to DAL project. You declare interfaces for the Repository and luw patterns in the CORE project.

所以现在一个

UI layer -> instantiate context and DAL Object eg repository -> inject into CORE services.

与此方法相关的还有控制反转或依赖注入模式.

Also connected to this approach is Inversion of Control or dependency injection pattern.

如果您针对 Dummy Repositories 使用测试驱动开发,则可以确保遵守设计原则.

If you use test driven development against Dummy Repositories you can make sure the design principles are adhered to.

这篇关于在 WinForms MDI 中使用具有存储库模式的实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆