I have been investigating how to decouple my DomainServices from their datasource so I can test them in unit tests. I'm starting to think fully decoupling them is not possible.
There is a decent amount of info out there, such as this question and this blog post. The blog post in particular gets you really far into mocking ObjectContext.
But my DomainServices have methods like this:
public IQueryable<Client> GetClients()
{
return ObjectContext.Clients
.Include("Foo")
.Include("Bar")
.Where(c => c.IsBaz);
}
It doesn't seem possible to fully mock the Include
method, as it returns an ObjectQuery<T>
, and the Include method is not captured in an interface anywhere (There is no IObjectQuery interface). ObjectQuery
implements IQueryable<T>
, and so I thought making my own Include method that returns IQueryable would work, but only if I plan to call Include at most once per query.
I am using EF4, .NET 4, Silverlight 4 and RIA Services RTW.
As a bit of a rant, I'm disappointed at how test unfriendly LINQ to Entities and by extension RIA Services is :(