【问题标题】:Load a property in a batch in ADO.Net Data Services in Silverlight在 Silverlight 的 ADO.Net 数据服务中批量加载属性
【发布时间】:2009-12-15 01:13:14
【问题描述】:

我在 Silverlight 3 中使用 ADO.Net 数据服务 (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);

但是,我不知道如何获取要传递给 BeginExecuteBatch 的加载属性请求的 DataServiceRequest 对象。是否可以通过获取 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()
});

【问题讨论】:

    标签: entity-framework silverlight-3.0 wcf-data-services


    【解决方案1】:

    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 上执行此操作。

    无论如何,彼得希望这可以帮助您实现您想要的。

    如果有什么需要告诉我

    问候

    丹尼尔

    【讨论】:

    • 非常感谢,这正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 2010-12-11
    • 2010-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多