开发者

WCF Data services parent entities link

I am learning WCF data services (cannot upgrade to RIA) and one of the major benefits I thougth I would get was that it would maintain relations for me and do lazy loads...

Example:

A bidire开发者_StackOverflow中文版ctional relation:

Order.Items --> OrderItems
OrderItem.Order --> Order

Say I have already a reference to an order. Then I populate its items by calling BeginLoadProperty(order, "OrderItems"). After that I would expect that the following would be true:

order.OrderItems[0].Order == order;

Unfortunatelly order.OrderItems[0].Order is null...

Is this scenario supported? Will it WCF data services handle for you? Or are you left with your custom implementation?

I use Entity Framework as the underlying data service.

Thanks!


WCF Data Services doesn't do lazy loading for several reasons. One is that most users want complete control of when a web request is made (as it can be very expensive) and second sometimes it's not technically possible. For example in Silverlight all HTTP requests can be done only through asynchronous API and as such lazy load can't really be implemented since accessing a property is a synchronous operation. As to your question: The WCF Data Services client doesn't know about bidirectional relations. It sees it as two separate relations. So it can't fixup the links for you. For this to work you could built the knowledge of bidirectional relations into your client side entities (for example your Orders property can fixup the back link when items are added into it). The other possible solution would be to use more complex queries using the $expand query option to load the parent entity in the same request. Unfortunately the LoadProperty/BeginLoadProperty API doesn't support extending the query this way, you would have to construct the query yourself. So to answer you last question, you are left with your custom implementation.


in WCF - ADO.NET Entity Data Service. You can use .Expand() to include related property

var orderItems = dataServiceContext.OrderItems
                .Expand("Order/Customer, Product")
                .Where(oi => oi.Order.Status == 1)
                .ToList();

for sample. Customer -> Order -> OrderItems <- Product

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜