问题
I have been reading through Alex D. James three year old blog posts as found here:
http://blogs.msdn.com/b/alexj/archive/2010/02/24/creating-a-data-service-provider-part-8-relationships.aspx
It is a wonderful blog series about exposing custom data through WCF Data Services. In the 8th post titled "Relationships" he talks about how to describe and expose related data. The problem with the post as pointed out by the one commenter is that he does it in memory, but does not explain how to dynamically supply related data.
Using his example, I would like to fetch my "Products" dynamically through a database for instance. How do I do that?
回答1:
You would need to write a custom IQueryable implementation. The WCF DS Server will construct a LINQ query which describes the query (including all expansions and accesses to all the properties). With custom implementation you can translate such query to whatever you want. Or you can overwrite the query evaluation (GetEnumerator and enumerator execution) to retrieve the related data from somewhere.
Note that this is definitely not a trivial task. I wrote a series of blog posts about the LINQ expression trees which are generated and what each part means. You would need to at least partially recognize these in your code to implement the delay loading of related properties. http://blogs.msdn.com/b/vitek/archive/2010/02/25/data-services-expressions-part-1-intro.aspx
来源:https://stackoverflow.com/questions/14639514/how-do-i-expose-related-data-through-a-wcf-data-service