开发者

Load a property in a batch in ADO.Net Data Services in Silverlight

开发者 https://www.devze.com 2022-12-13 21:00 出处:网络
I\'m using ADO.Net Data Services (Astoria) in Silverlight 3 and I want to delay loading properties on an entity until after the initial load. However, when I\'m ready to load them I\'d like to batch t

I'm using ADO.Net Data Services (Astoria) in Silverlight 3 and I want to delay loading properties on an entity until after the initial load. However, when I'm ready to load them I'd like to batch the load requests together.

// 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've gotten 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 can't figure how how to get a DataServiceRequest object for a load property request to pass to BeginExecuteBatch. Is it possible to issue these requests (and potentially others that aren't associated with the customer property load) in the same batch by getting a DataServiceQuery?

Something like this:

// c is the customer from the above load
ctx.BeginExecuteBatch(BatchCallback, objState, new []{
    ctx.GetLoadQuery(c, "Address"),
    ctx.GetLoadQ开发者_JS百科uery(c, "Phone"),
    ctx.GetLoadQuery(c, "Email"),
    GetOtherNonPropertyQuery()
});


The LoadProperty method does not use any standard types that are available to you in dataservice. However data service is intelligent enough to figure that

LoadProperty(person, "Gender")

is the same as

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

The Uri generated is the same.

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

So if you want to Call LoadProperty in batch query you can generate the Uri required quite easily. 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 Now you can do this.

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

The only thing left is that this will only return to you the object(s) what it wont do is assign the return value(s) to the entity property, that will you have to do it yourself on your BatchCallback.

Anyhow Peter Hope this helps you on what you want.

If there is anything you need let me know

Regards

Daniel

0

精彩评论

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

关注公众号