IAsyncRepository或IObservableRepository Silverlight的4 + WCF数据服务 [英] IAsyncRepository or IObservableRepository for silverlight 4 + WCF Data Services

查看:160
本文介绍了IAsyncRepository或IObservableRepository Silverlight的4 + WCF数据服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新2 :@Enigmativity有着辉煌的答案。我实现了这个成 IObservableRepository< T> 。详情在我的回答如下

Update 2 : @Enigmativity has a brilliant answer. I've implemented this into a IObservableRepository<T>. Details in my answer below.

问:
所以我已经改变大部分的问题(见编辑历史)我只是想,如果有人评论/验证/吐在我的设计。 =)

Question: So I've changed most of the question (See edit history) I would just like it if someone commented/validated/puked on my design. =)

所以通常我的回购协议是这样的:

So typically my Repos look like this:

public interface IRepository<T> where T : class
{
    T GetById(int id);
    IQueryable<T> GetAll();
    void InsertOnSubmit(T entity);
    void DeleteOnSubmit(T entity);
    int SubmitChanges();
}



但是,当涉及到Silverlight和WCF数据服务,它会严重地困扰查询与所有的异步相关的数据。我必须加载异步第一父实体,然后查询其子实体异步。

But when it comes to Silverlight and WCF Data Services, it gets seriously annoying query related data with all the asynchrony. I have to load the parent entity async first and then query its child entities async.

于是我想出了一个 IAsyncRepository ,我想知道,如果设计是好的,它是否能够得到改善,(以及它是否使得这里使用接收任何意义?)

So I came up with an IAsyncRepository, I'd like to know if the design is ok, whether it could be improved, (and whether it makes any sense using Rx here?)

要解决。孩子的实体问题,我计划在调用回调之前加载所有的要求子实体

To Solve the child entities problem I plan to load all required child entities before calling the callback.

我的回购是这样的:

public interface IAsyncRepository<T> where T : class
{
    void GetById(int id, Action<T> callback);
    void GetAllFromQuery(Func<MyEntities, IQueryable<Product>> funcquery,
                                             Action<IList<Calculator>> callback)
}

您可以使用像这样的回购:

You could use the repo like this:

productRepo.GetAllFromQuery(
    x => x.Products.Where(p => p.ID > 5),
    y => Assert.IsTrue(y.Count > 0)); //y is a IList<Product>



你们有什么想的?

What do you guys think?

问候,
基甸

推荐答案

只是一个快速的,即兴的答案。

Just a quick, off the cuff answer.

如何使用的为.NET无扩展(RX)

您可以再定义你的资料库为:

You could then define your repository as:

public interface IObservableRepository<T> where T : class
{
    IObservable<T> GetById(int id);
    IObservable<T> GetAll(Func<IQueryable<T>, IQueryable<T>> query);
    IObservable<Unit> InsertOnSubmit(T entity);
    IObservable<Unit> DeleteOnSubmit(T entity);
    IObservable<int> SubmitChanges();
}



所有返回的观测值将包含单值,除了 GETALL 这将对零个或多个。

单位类型无效在接收世界。这只是不需要定义一个非通用的IObservable 接口的方式。

The Unit type is void in the Rx world. It's just a way of not needing to define a non-generic IObservable interface.

您会再查询像这样

IObservableRepository<Foo> repo = ...;

var foos = repo.GetAll(ts => ts.Where(t => t.Bar == "Hello"));

foos.Subscribe(foo =>
{
    // Do something asynchronously with each `Foo`.
});



并提交可以这样进行:

And submit could be done like this:

var submit =
    foos
        .Select(foo => repo.InsertOnSubmit(foo)).ToArray()
        .Select(s => repo.SubmitChanges());

submit.Subscribe(result =>
{
    // handle the asynchronous result of submit.
});

这是所有基于试图保持库的方法尽可能接近原来的,但它可能是值得重构对Silverlight的一边是这样的:

This is all based on trying to keep the repository methods as close as possible to the original, but it may be worth refactoring on the Silverlight side to something like this:

public interface IObservableRepository<T> where T : class
{
    IObservable<T> GetById(int id);
    IObservable<T[]> GetAll(Func<IQueryable<T>, IQueryable<T>> query);
    IObservable<int> Submit(T[] insertsOrUpdates);
    IObservable<int> Submit(T[] insertsOrUpdates, T[] deletes);
}

提交将是现在有点更好:

Submit would be a bit nicer now:

repo.Submit(foos).Subscribe(result =>
{
    // Handle asynchronous result of submit;
});



就像我说的,即兴的。 : - )

Like I said, off the cuff. :-)

这篇关于IAsyncRepository或IObservableRepository Silverlight的4 + WCF数据服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆