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();
}
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.
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.
My Repo looks like:
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?
Regards,
Gideon
Just a quick, off the cuff answer.
How about using the Reactive Extensions for .NET (Rx)?
You could then define your repository as:
All of the returned observables would contain single values, except for
GetAllwhich would have zero or more.The
Unittype isvoidin the Rx world. It’s just a way of not needing to define a non-genericIObservableinterface.You would then query like so:
And submit could be done like this:
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:
Submit would be a bit nicer now:
Like I said, off the cuff. 🙂