I've got a problem with an implementation of the repository pattern for my WCF Data Service. To sum up, I'm trying to use a repository pattern within a client application that utilizes a plugable model for the repositories that it requires. The root problem is that my repository interface IRepository can only know about the interface type variant of the item that it stores. This is to abstract the client application away from the Data Service implementation of the order e.g.
IRepository<IOrder> : IQueryable<T>
In general, this concept works fine until Data Services gets involved. I'm currently using Data Services v5.4 from NuGet. If I perform an action as simple as ToList() on my repository, everything works fine and the translation turns the result into a list of Orders from the OData service. However if I attempt to perform an orderby, the query expression become more complex and fails to populate the return type as it attempts to create an instance of the interface variant of the queryable type. i.e. tries to create an instance of IOrder instead of Order.
repository.OrderBy(order => order.Id).ToList(); // Fails
At the client level, however if I force the type by using Cast then it works...
repository.Cast<Order>(order => order.Id).ToList(); // Suceeds
Obviously, this is against the mantra of disconnected types as my client application would then have to be aware of the strong type 'Order' from the Data Services Library which I want to avoid in case I want to switch out OData in the future.
Next I stumbled across the Data Service Client side Context class which allows you to get a handle on methods for resolving types, however in my case the resolution didn't ever get called for some reason.
What I subsequently started looking at was ExpressionVisitors which I still believe to be my ticket out of this mess, which has to be capable of converting any IOrder parameters into Order parameters within my repository so that the query returns strong types but continues the facade of the interface type.
When using an empty shell ExpressionVisitor implementation e.g.
MyVisitor : ExpressionVisitor
This works fine for non Data Service queries (e.g. something like a List as queryable), but for data service repository pattern queries you receive the unhelpful (at least to me) message 'must be reducible node'. What I'm guessing is that the expression tree visitor has hit a node type that is custom to the data service query and doesn't know what to do about it. However I thought that an ExpressionVisitor would simply copy what was there in the tree unless told to do something differently?
Anyway, what I am asking is, how do I replace the return type of my query by using this ExpressionVisitor with OData queries? I have tried to look for some sort of exisiting Data Services Expression Visitor but am unable to find one. I want my client side data service to generate the query from the interface type query but return me strong types of the same name.
Have a look at MongOData, it's a MongoDB OData provider built using WCF Data Services custom provider:
https://github.com/object/MongOData
It deals with expression visitors, both when converting queries to MongoDB LINQ expressions and when converting results to DSPResource.
NB! What you're trying to achieve is not easy. But hope this example helps.