TAILIEUCHUNG - ASP.NET 4 Unleased - p 104

Điều đầu tiên chúng ta làm trong mã này là tạo ra một thể hiện của các dịch vụ proxy, đó là một lớp tạo được thừa kế từ lớp . Lớp này cho thấy nhiều đặc tính cho mỗi đơn vị bộ tiếp xúc của dịch vụ. Trong trường hợp của chúng tôi, chúng tôi có những cầu thủ, vũ khí, và các sinh vật và các mối quan hệ chính nước ngoài cho phép chúng liên kết với các đăng nhập diệt | 1004 CHAPTER 21 Data Access with WCF Data Services .AddCueryOption playerId 1 .Expand Weapon .Expand Player .Expand Creature orderby descending select kill q The first thing we do in this code is create an instance of the service proxy which is a generated class that inherits from the class. This class exposes properties for each of the entity sets exposed by the service. In our case we have players weapons and creatures and foreign key relationships that enable them all to link to the kill log. A Kill is another entity exposed by the service. The custom service context client proxy takes as a constructor parameter a Uri that contains the address of the service. In a production scenario you might use service discovery a service registry or files to store the URLs of the services. The next thing we do is call CreateQuery Kill GetMyKills . There s quite a bit of functionality packed into this one method call. CreateQuery doesn t actually talk to the service it is merely asking us for the shape of the rows that will be returned from the query the Kill class and for either the name of a service method to invoke or the name of an entity set to query. If we had simply supplied the string Kills to the method we would be querying the Kills entity set directly and not invoking a custom method. Next you can see that the output of the CreateQuery method is something queryable so we can chain method calls onto this method in a fluent style. We use the Expand method to tell the WCF Data Service that for every Kill we return also expand the specified relationship properties. This enables us to bring over all the data related to an entity in a single method call rather than making a bunch of chatty calls with high overhead and latency. In our case we want the names of the weapons and creatures that were killed. If we wanted we could supply virtually any .