实体框架 4 - 在 N 层困境中更新单个实体属性

发布于 2024-10-30 14:54:24 字数 825 浏览 1 评论 0原文

我们有一个 n 层应用程序,其中包括一个 DB 、其上的 EF4 、然后是 WCF 服务,最后是 silverlight 4 客户端。

我们正在考虑更新单个实体属性的最佳方法是什么。我们有一个拥有大约 15 个属性的 Customer 实体。现在,在客户端中,用户已更改客户名称属性,并希望将此更改保存到数据库中。因此想到了两种方法:

1)只需通过 WCF 服务发送整个客户实体并更新数据库。这非常简单,因为在服务器的 Update 方法中,我们需要做的就是将传入的实体附加到上下文,将其 objectState 更改为 Modified,然后点击 SaveChanges()。

2) 假设我们所有的实体都有一个 int 键,我们可以在 WCF 服务中公开一个方法,如下所示:

public void UpdtaeEntityProperty(Type i_EntityType, int i_EntityId, string i_PropertyName, string i_NewValue); 

在第一个方法中,我们通过线路发送大量不需要的信息。 第二种方法只发送需要的内容,而且它适合所有实体。另一个优点是,当用户正在更新 UI 中的多个字段时,我们可以更好地控制在他输入值时抛出异常。

在所有 EF4 示例\视频\书籍中,更新属性值的过程始终涉及发送完整的对象。您永远不会看到更新单个值的方法。就好像我们在客户端中缺少某种数据绑定器:将特定文本字段绑定到数据库的组件,即使它是通过 WCF 服务进行的。

1) EF4/WCF 的下一个版本是否正在设计这样的解决方案?

2)我们的 UpdateEntityProperty 方法的解决方案是一个好的实践吗?

We have an n-tier app that includes a DB , on top of it EF4 , then WCF services and finally a silverlight 4 client.

We are thinking what is the best way to update a single entity property. We have a Customer entity which has about 15 properties. Now, in the client the user has changed the customer Name property and want to save this change to the DB. So two methods come in mind :

1) Just send the entire Customer entity over the WCF servie and update the DB. This is very simple, because in Update method in the server, all we need to do is attach the incoming entity to the context, change it's objectState to modified, and hit SaveChanges().

2) Asumming all our entites have a single int key, we can expose a method in the WCF service that looks something like this:

public void UpdtaeEntityProperty(Type i_EntityType, int i_EntityId, string i_PropertyName, string i_NewValue); 

In the first method, we are sending a lot of information over the wire which is not needed.
The second method sends just what is needed, plus it fits all the entites. Another advantage is that when the user is in the process of updating several fields in the UI, we have more control on throwing exceptions just as he enters the values.

In all EF4 examples\videos\books , the process of updating a value of a property always involves sending a complete object. You never see an aproach that updates a single value. It's as if we are missing some kind of data binder in the client : A componnet that will bind a certain text field to the DB, even it's over a WCF service.

1) Is such a solution is being in desgin for next realses of EF4/WCF ?

2) Is our solution of the UpdateEntityProperty method is a good practice ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱她像谁 2024-11-06 14:54:24

你永远不会看到更新的方法
单一值。就好像我们是
缺少某种数据绑定器
客户端:一个组件,它将
将某个文本字段绑定到数据库,
即使它是通过 WCF 服务实现的。

我认为您看到的大多数示例都处理更新传递的分离实体的一般情况。这并不意味着您必须这样做,这完全取决于您的特定场景,因为这是通用性与性能/自定义解决方案之间的权衡。

如果您的实体很大并且性能对您很重要,那么无论如何只需将 ID 和更新后的值发送到服务器 - 此解决方案会执行得更好,但不是很通用,并且取决于您的实体类型的属性 - 但是可能适合你的项目。

You never see an approach that updates
a single value. It's as if we are
missing some kind of data binder in
the client : A component that will
bind a certain text field to the DB,
even it's over a WCF service.

I think most examples you have seen handle the general case of updating detached entities which are passed around. That doesn't mean you have to do this, it all depends on your particular scenario, since it's a trade off generality vs. performance/custom solution.

If your entities are big and performance is important to you, by all means just send the ID and the updated value to the server - this solution will perform better, but is not very generic and depends on the properties of your entity type - but that might be ok for your project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文