开发者

Check if records with a certain value exist in database, using WCF RIA Services

开发者 https://www.devze.com 2023-01-26 03:53 出处:网络
A Silverlight application uses WCF RIA Services for connection to a SQL Server database. Before inserting a bunch of new records into a table, I should check if this table contains any records with a

A Silverlight application uses WCF RIA Services for connection to a SQL Server database. Before inserting a bunch of new records into a table, I should check if this table contains any records with a certain value in one of the fields.

I'm somewhat new to Silverlight, so need some advice on right approach in handling WCF RIA Services.

Should I make a request filtering out records on given value, Load it and then, on the client, check if it contains any such records? Something like the following method in Domain service cla开发者_如何学Goss ProductService:

public IQueryable<Product> GetProducts(string nameFilter)
{
   return this.ObjectContext.Products.Where(p => p.Name.StartsWith(nameFilter));
}

Or should I compose (in the Domain service class) a custom method, which will do all the check on the server-side and only return boolean confirmation?

Which approach is more correct in the context of Silverlight WCF RIA Services?


Do it on the server and only send the data you actually need across the network. Though if you are ultimately going to use the result I'd recommend returning that in the same call, otherwise you're making two calls for the same information.

This is the way we're doing it in our application.

The only caveats I'd add are if you want to do some expensive filtering on the data or expand the result set, it may be more efficient to return the full set and work in the client or if your data object is large and you're not using it over half the time then making duplicate calls may actually be more efficient.

Really the same rules as "standard" web/database programming apply - you don't want to make two (or more) calls to the server where one would do, but equally you don't want to send data you aren't going to use. You will have to profile your application and see where there are any bottlenecks and analyse the code on a case by case basis.

Your question is slightly confusing as the code you quote is the server side code.

0

精彩评论

暂无评论...
验证码 换一张
取 消