在 WinForms MDI 中使用实体框架和存储库模式
我们即将启动一个与之前的项目类似的新项目。我可以复制旧的设计,但我对旧的设计不太满意。
它是一个“标准”业务系统(销售、盘点、仓储等),构建在.Net 3.5(Winforms MDI)之上,后端带有实体框架。
所有窗体都继承自基本窗体(它继承 Windows.Form)。该表单公开了一个名为 ObjectContext 的属性,该属性在第一次调用时实例化一个新的 ObjectContext。我认为这构成了一个非常好的工作单元,在每种表单中隔离了所有数据访问。
然而。
我已经将所有查询和常见的 CRUD 封装在“poor mans repositories”中。这些存储库作为 ObjectContext 的属性公开。
因此,如果我想绑定并订购表单,我会调用 OrderLinesGrid = ObjectContext.OrderRepository.GetOrderLinesByID(orderID)。
OrderRepository 获取对为表单创建的对象上下文的引用,如下所示
(在我的部分 ObjectContext 类中)
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
我不喜欢的是:
对存储库进行调用 通过ObjectContext。因此,我做 没有得到之间的抽象 查询和数据访问层 I 会喜欢。
对于我的域中的每个新类型我 需要在我的中创建一个属性 ObjectContext
我对 OrderRepository 的调用应该只返回域对象,而不用担心它是如何持久化的。另外,我不能让每个存储库都有自己的 ObjectContext,因为这需要我在将 Country 引用到 Order.Country 属性时附加和分离对象。
我将不胜感激对此设计的任何想法和反馈:)
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:
The call to the repository is made
through ObjectContext. Hence, I do
not get abstraction between the
query and the dataaccesslayer I
would like.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 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议你研究“洋葱”原则,存储库模式 和 Luw 模式。
网络上有很多例子。
本质上,您使用 POCO 模型核心项目。没有提及 DAL 项目。
您在 CORE 项目中声明 Repository 和 luw 模式的接口。
因此,现在
与此方法相关的还有控制反转或依赖注入模式。
如果您对虚拟存储库使用测试驱动开发,则可以确保遵守设计原则。
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
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.