实体框架:关注点分离 [英] Entity Framework: Separation of concerns

查看:122
本文介绍了实体框架:关注点分离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是EF,不知道别人是怎么分开的实体的数据上下文。

I'm using the EF and wondering how other people are separating the Data Context from the Entities.

基本上,我需要一个层来访问数据上下文(模型对象)来调用的SaveChanges()等。和其他层需要访问的实体类型本身。因此,举例来说,如果一个方法返回一个实体,我叫从我的UI方法,那么我将不得不引用从UI的模式,这就是不好的。

Basically i need one tier to access the Data Context (the model object) to call SaveChanges() etc.. and other tiers need access to the Entity type itself. So for example if a method returned an Entity and i called that method from my UI, then I would have to reference the Model from the UI and that's not good.

是有办法有一个项目的实体类型,但在另一项目中的数据上下文?

is there a way to have the entity types in one project and have the Data Context in another project?

我想另一种方式是创建界面为每个实体,并把接口在另一个项目,该项目的其他层次可以参考一下。但是,这是一个大量的维护和屁股痛

i guess another way would be to create Interfaces for each entity and put the interfaces in another project which other tiers could reference. But this is a lot of maintenance and a pain in the ass

推荐答案

在我看来,虽然这的确是一个痛苦的屁股,该接口是更好的方式去。您将真正实现松耦合。

In my opinion, although this is indeed a pain in the ass, the interfaces are the better way to go. You will really achieve loose-coupling.

使用的接口,你就可以在以后更改完全的实体层和你的用户界面甚至不会注意到。为此目的,明确分工和隔离创建界面。

With interfaces, you will be able later to change totally your entity layer and your UI won't even notice. Interfaces were created for that purpose, clear separation and isolation.

人们总是倾向于找接口无聊和浪费时间。但是你保存忽略它们的时候,你会后付款的话,我告诉你!

People always tend to find interfaces boring and time wasting. But the time you save by ignoring them, you will pay it later, I tell you !

Marc_s给了你很大的答案太(嘿,这是marc_s!),但在我看来不太纯。
您也可以去POCO实体对象,但耦合从这里开始。

Marc_s gave you a great answer too (hey, that's marc_s!), but less pure in my opinion.
You can also go for POCO entity objects, but coupling starts here.

这篇关于实体框架:关注点分离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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