简体   繁体   English

减少从Silverlight应用到数据库的旅行次数

[英]Reducing number of trips from Silverlight App to Database

I am working on a silverlight application. 我正在研究Silverlight应用程序。 It have a view which allows user to add a new object. 它具有一个允许用户添加新对象的视图。 The viewmodel bound to this view has a collection of that object type as a property. 绑定到该视图的viewmodel具有该对象类型的集合作为属性。 The constructor of viewmodel has a function, say GetData() that calls a WCF Service and fetches data from database, which adds the fetched list to that collection. viewmodel的构造函数具有一个称为GetData()的函数,该函数调用WCF服务并从数据库中获取数据,然后将获取的列表添加到该集合中。

After adding a new object to database through WCF Service, the method GetData needs to be called again to refresh the collection as having the latest data in collection is necessary. 通过WCF服务将新对象添加到数据库后,需要再次调用GetData方法以刷新集合,因为必须具有集合中的最新数据。

This process is a little slow as every time one adds anything, entire data from the tables need to be fetched again. 这个过程有点慢,因为每次添加任何东西时,都需要再次从表中获取整个数据。 It can get even more slower as data grows more and more large and when joining of multiple tables may be required to get the data. 随着数据越来越大,并且可能需要连接多个表来获取数据时,它可能变得越来越慢。

I am thinking of adding the object that I pass to service to add it to database, directly to the collection in viewmodel. 我正在考虑将传递给服务的对象添加到数据库中,然后直接添加到viewmodel中的集合中。 Obviously, I will add this object to the collection only when service called to it has not returned any error to make sure it has been successfully added to DB. 显然,只有在调用该对象的服务未返回任何错误以确保已成功将其添加到数据库时,我才会将该对象添加到集合中。 This way I can have the latest data in collection without any need to fetch it from database. 这样,我可以收集最新的数据,而无需从数据库中获取数据。

Can any one point out any drawback with this approach or any scenario where it may fail? 谁能指出这种方法的任何缺点或可能失败的任何情况? Also, please suggest if there are other better ways of achieving this goal. 另外,请提出是否还有其他更好的方法可以实现这一目标。

I often have my save methods fetch and return the object that was saved. 我经常使用我的保存方法来获取并返回已保存的对象。 That way if the service/DB layer has modified it at all (create date, ID etc. etc.) you get the changes. 这样,如果服务/数据库层完全修改了它(创建日期,ID等),您将获得更改。 Also if you want to update it, it does not think it is "new". 另外,如果您要更新它,则认为它不是“新的”。

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

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