Download the property in the package in ADO.Net Data Services in Silverlight

I am using ADO.Net Data Services (Astoria) in Silverlight 3, and I want to delay the loading of the property on the entity only after the initial loading. However, when I am ready to download them, I would like to combine the download requests.

// this is what I want to avoid var c = (from c in ctx.Customers.Expand("Address,Phone,Email") where c.Id = 12 select c).Take(1) as DataServiceQuery<Customer>; 

I got this far:

 // I can do this instead var c = (from c in ctx.Customers // .Expand("Address,Phone,Email") where c.Id = 12 select c).Take(1) as DataServiceQuery<Customer>; c.BeginExecute(CustomerCallback, objState); ... // Later, when I want properties, I need to do this ctx.BeginLoadProperty(c, "Address", AddressCallback, objState); ctx.BeginLoadProperty(c, "Phone", PhoneCallback, objState); ctx.BeginLoadProperty(c, "Email", EmailCallback, objState); 

However, I cannot figure out how to get a DataServiceRequest object to request load properties in order to go to BeginExecuteBatch. Is it possible to issue these requests (and potentially others that are not related to the load of the client’s property) in the same batch, having received a DataServiceQuery?

Something like that:

 // c is the customer from the above load ctx.BeginExecuteBatch(BatchCallback, objState, new []{ ctx.GetLoadQuery(c, "Address"), ctx.GetLoadQuery(c, "Phone"), ctx.GetLoadQuery(c, "Email"), GetOtherNonPropertyQuery() }); 
+4
source share
1 answer

The LoadProperty method does not use the standard types available to you in the dataservice. However, the data service is intelligent enough to understand that

 LoadProperty(person, "Gender") 

coincides with

 person.Gender = (from g in ent.Person where g.ID == person.ID select g.Gender).FirstOrDefault(); 

The generated Uri is the same.

 http://localhost/WebDataService.svc/Person(1)/Gender 

So, if you want to call LoadProperty in a batch request, you can easily create a Uri. See below.

 public static class DataServiceContextExtensions { public static Uri GetLoadPropertyUri(this DataServiceContext context, object entity, string property) { Uri entityUri = null; if(context.TryGetUri(entity, out entityUri)) { return new Uri(entityUri.AbsoluteUri + "/" + property); } throw new DataServiceClientException("Entity Uri not found."); } public static DataServiceRequest<T> GetLoadPropertyRequest<T>(this DataServiceContext context, object entity, string property) { return new DataServiceRequest<T>(context.GetLoadPropertyUri(entity, property)); } } 

So you can do it.

 ctx.BeginExecuteBatch(BatchCallback, objState, new []{ ctx.GetLoadPropertyRequest<Address>(c, "Address"), ctx.GetLoadPropertyRequest<Phone>(c, "Phone"), ctx.GetLoadPropertyRequest<Email>(c, "Email"), GetOtherNonPropertyQuery() }); 

All that remains is that this will only return the object (s) to you, what it will not do is assign the return value (s) to the entity property, which you will need to do yourself on your BatchCallback.

Anyway, Peter Hope helps you with what you want.

If you need anything let me know

Hi

Daniel

+5
source

All Articles