简体   繁体   English

通过服务更新大型实体

[英]Update of large entity through service

We're building an n-tier system based on WCF (but not Entity Framework) and we've run into a discussion of the best way to implement updates of large entities. 我们正在基于WCF(而不是Entity Framework)构建n层系统,并且我们已经讨论了实现大型实体更新的最佳方法。 We've created DTOs and map data from our domain model into theese when data is sent to the client. 我们已经创建了DTO,并在将数据发送到客户端时将域模型中的数据映射到theese中。 The client then makes some changes and sends them back using the same DTO, in our current implementation. 然后,客户端进行一些更改,并在我们当前的实现中使用相同的DTO将其发送回去。 Some of our entities might have 80-100 properties, but perhaps the client only makes changes to one or a few of them. 我们的某些实体可能具有80-100个属性,但也许客户仅对其中一个或几个属性进行了更改。 It seems inefficient to populate the whole DTO and send it back and then try to figure out on the server side wich properties were actually modified. 填充整个DTO并将其发送回,然后尝试在服务器端找出实际上已修改的属性,效率似乎很低。 Is there a better way to implement this or should we go with the "brute force" method? 有没有更好的方法来实现这一目标,还是应该采用“蛮力”方法? In the future we need to support non .Net clients so we don't want to tie our solution to something .net-specific. 将来,我们需要支持非.Net客户,因此我们不想将我们的解决方案与特定于.net的东西联系在一起。

One thing you can do is allow the client to send delta updates. 您可以做的一件事是允许客户端发送增量更新。 All your objects could have a unique guid that the server mananges. 您所有的对象都可以具有服务器管理的唯一GUID。

You could have a WCF method like so to send the delta 您可能有这样的WCF方法来发送增量

  void UpdateProperty( Guid objguid ,
            string propertyname , string propertyvalue );

On the server side you can locate the object using the guid and update the corresponding property. 在服务器端,您可以使用guid找到对象并更新相应的属性。

If you're schema is that large, you could try using XML as a web method parameter along with an XSD that has optional elements. 如果您的架构太大,则可以尝试将XML与具有可选元素的XSD一起用作Web方法参数。 This wouldn't be tied to a particular platform. 这不会绑定到特定平台。 You could just send in the elements that have changed along with the unique key identifier. 您可以只发送已更改的元素以及唯一的密钥标识符。

We solved this problem by including a string array of changed property names on every DTO. 我们通过在每个DTO上包括一个更改属性名称的字符串数组来解决此问题。 The update code in the service only validates and writes properties whose names are in this array. 服务中的更新代码仅验证和写入名称在此数组中的属性。

To make coding easier, we put this string array in an abstract base class and inherited all "changeable" DTOs from that. 为了简化编码,我们将此字符串数组放在抽象的基类中,并从中继承了所有“可变的” DTO。 For example: 例如:

<DataContract>
Public MustInherit Class ChangeTrackableObject
    <DataMember> Public Property Changes As HashSet(Of String)
End Class

There are no <KnownType> attributes because DTO polymorphism is not required in this case. 没有<KnownType>属性,因为在这种情况下不需要DTO多态。 The DataContractSerializer simply uses the inheritance relationship to pull in the base class property, nothing more. DataContractSerializer仅使用继承关系来获取基类属性,仅此而已。

This system seems to work equally well from .NET and Java client code. 从.NET和Java客户端代码来看,该系统似乎同样有效。 Java clients see this as a string array. Java客户端将其视为字符串数组。 We have a library of extension methods for .NET clients to make it easier to populate the data. 我们为.NET客户端提供了扩展方法库,使填充数据变得更加容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM