在 Silverlight 的 ADO.Net Data Services 中批量加载属性

发布于 2024-08-15 01:09:12 字数 1239 浏览 2 评论 0原文

我在 Silverlight 3 中使用 ADO.Net Data Services (Astoria),并且希望延迟在实体上加载属性,直到初始加载之后。但是,当我准备好加载它们时,我想将加载请求一起批处理。

// 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 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);

但是,我不知道如何获取 DataServiceRequest 对象以将加载属性请求传递给 BeginExecuteBatch。是否可以通过获取 DataServiceQuery 在同一批中发出这些请求(以及可能与客户属性加载无关的其他请求)?

像这样的东西:

// 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()
});

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.GetLoadQuery(c, "Phone"),
    ctx.GetLoadQuery(c, "Email"),
    GetOtherNonPropertyQuery()
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵情客 2024-08-22 01:09:12

LoadProperty 方法不使用数据服务中可用的任何标准类型。然而,数据服务足够智能,可以计算出

LoadProperty(person, "Gender")

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

与生成的 Uri 相同

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

因此,如果您想在批量查询中调用LoadProperty,您可以非常轻松地生成所需的Uri。见下文。

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));
    }
}

所以现在你可以这样做。

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

剩下的唯一一件事是,这只会返回给您对象,它不会做的是将返回值分配给实体属性,您必须在 BatchCallback 上自己执行此操作。

无论如何,彼得希望这对你有所帮助。

如果您有任何需要,请告诉我

问候

丹尼尔

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文