在WinForms MDI中将实体框架与存储库模式一起使用 [英] Using Entity Framework with repository pattern in WinForms MDI

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

问题描述

我们将开始一个与上一个项目相似的新项目。我可以复制旧的设计,但对旧的设计并不满意。



这是一个标准业务系统(销售,盘点,仓储等)



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



但是。



<我已将所有查询和通用CRUD封装在穷人资源库中。这些存储库作为ObjectContext的属性公开。



因此,如果我想绑定并订购表单,我将调用
OrderLinesGrid = ObjectContext.OrderRepository.GetOrderLinesByID (订单ID)。



OrderRepository获取对为表单创建的objectcontext的引用,例如



(在我的部分ObjectContext类中)

 将_OrderRepository作为OrderRepository 
将公共ReadOnly属性OrderRepository作为OrderRepository
获取
如果_orderrepository为空,则
_orderrepository =新OrderRepository(me)
如果
返回_orderrepository
End获取
结束属性

我对此不满意的是:


  1. 通过ObjectContext对存储库的调用是
    。因此,我确实
    不会在
    查询和
    想要的数据访问层之间获得抽象。


  2. 对于每个新对象在我的域中输入
    需要在
    ObjectContext中创建一个属性


我的通话到OrderRepository的操作应该只返回域对象,而不用担心它如何持久化。另外,我不能让每个存储库都有它自己的ObjectContext,因为这要求我在将Country引用到Order.Country属性时附加和分离对象。



关于此设计的任何想法和反馈:)

解决方案

我建议您研究洋葱原则存储库模式和Luw模式。
网络上有很多示例。



基本上,您使用POCO模型核心项目。没有引用DAL项目。
您可以在CORE项目中声明存储库和luw模式的接口。



所以现在a

  UI层->实例化上下文和DAL对象,例如存储库->注入CORE服务。 

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



如果对虚拟存储库使用测试驱动的开发,则可以确保遵守设计原则。


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.

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

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.

However.

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

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

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

(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. The call to the repository is made through ObjectContext. Hence, I do not get abstraction between the query and the dataaccesslayer I would like.

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

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 :)

解决方案

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

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.

So Now a

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.

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

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

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