by Corrado Cavalli via Corrado's BLogs on 12/31/2010 2:51:22 PM
I’m working on an Silverlight 4.0 application that spans several technologies and this time I’m in charge of the “server side”, in specific I’m developing the WCF RIA Service that will be consumed by the main application and in these weeks I have to admit that, while RIA Services are a real boost in productivity and allows you to forget all low level WCF stuff, you can sometime waste a lot of time figuring why basic things don’t work as expected, so the truth is: If you need to develop a production application you’d better understand how things work under the hood. I’ve hit several walls during service development, and I’ve learnt many things about RIA services, if you need a good starting point Mike Taulty has a series of very good posts about RIA Services inner workings. On this post I’d like to point out one of the various issues I’ve found, other might come in future posts. (time permitting) An example is the best way to describe the scenario: I have a DomainService that exposes a collection of cars, and since cars can be bought the service also allows me to get the list of orders placed by the user.
Let’s start with the entities and since database technology is not important (you can use any data access technology you love with RIA Services BTW…) all server side data on following example will be code generated:
[DataContract]public class Car{ [DataMember] [Key] public int Key { get; set; } [DataMember] public string Plate { get; set; } [DataMember] public int OrderId { get; set; }}
[DataContract]public class Order{ [DataMember] [Key] public int Key { get; set; } [DataMember] public string OrderName { get; set; } [DataMember] [Include] [Association("Ordercars", "Key", "OrderId")] public List<Car> OrderCars { get; set; }}
If you already know WCF RIA Services everything should look familiar including the Include and Association stuff required to expose children entities from Order class.
Let now write some client side code that consumes domainService’s methods:
private void LoadCars() { this.context.Load(this.context.GetCarsQuery(), lop => { List<Car> cars = lop.Entities.ToList(); Debug.WriteLine("--Cars--"); foreach (var car in cars) { Debug.WriteLine(car.Plate); } }, null); } private void LoadOrders() { this.context.Load(this.context.GetOrdersQuery(), lop => { List<Order> orders = lop.Entities.ToList(); Debug.WriteLine("--Orders--"); foreach (var order in orders) { Debug.WriteLine(order.OrderName); foreach (var car in order.OrderCars) { Debug.WriteLine(car.Plate); } } }, null); }
Let’s suppose now that the application initially loads all cars and lately asks for all orders placed by the user, something we can easily simulate using:
private void button1_Click(object sender, RoutedEventArgs e) { LoadCars(); LoadOrders(); }
private void LoadOrders() { this.context.Load(this.context.GetOrdersQuery(), LoadBehavior.RefreshCurrent, lop => { List<Order> orders = lop.Entities.ToList(); Debug.WriteLine("--Orders--"); foreach (var order in orders) { Debug.WriteLine(order.OrderName); foreach (var car in order.OrderCars) { Debug.WriteLine(car.Plate); } } }, null); }
Original Post: Understanding LoadBehavior in WCF RIA Services
The content of the postings is owned by the respective author. Silverlight Feeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on Silverlight Feeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.