Writing Your Own LINQ Provider- Part 3
This is the third in a short series of posts on writing your own LINQ provider. A quick outline of the series:
- A primer
- Provider basics
- A simple, pointless solution (this post)
- A tiny ORM of our own
A simple, pointless solution
In the previous post, we took a look at what happens when you call LINQ methods on IQueryable<T>
, and how you can use that to build your own provider. We take that a step further this time by building an actual provider - albeit a somewhat pointless one, in that it adds LINQ support to something that doesn’t really need it. The point, though, is to keep it simple and try to understand how the process works.
The best way to understand is to take a look at the source code first:
Now, a quick summary of what this is.
We have an interface, INextProvider
. It has one method, GetNext
that is supposed to get the next one in a sequence of items. An example implementation that uses a simple array as the underlying store is also included. Once you have an instance of INextProvider<T>
, say, called nextProvider
, you can then extract an IQueryable<T>
out of it with this call:
var query = nextProvider.AsQueryable();
You can then use standard LINQ on top of it. Now, I know what you’re thinking: this INextProvider
seems uncomfortably similar to IEnumerator
- why would we need a query provider for this? We don’t, hence the “pointless” part, but again - the idea is to examine how building a provider works.
The entry point is NextProviderQueryable
which implements IQueryable<T>
and uses NextProviderQueryProvider
as its Provider
and returns a NextProviderEnumerator
from its GetEnumerator()
call. This means that whenever one of the LINQ methods are called on an instance of NextProviderQueryable
, one of the following happens:
If the method is something that creates another queryable out of the existing one (e.g.
Where
,Select
,SelectMany
,Cast
, etc.),NextProviderQueryProvider.CreateQuery()
is called. That, in turn, creates a new instance ofNextProviderQueryable
, but with theExpression
set to what has been passed in. Thus, every call toCreateQuery
ends up creating a new queryable with theExpression
property representing the complete call.If the method is something that enumerates a queryable (e.g.
ToList
,ToArray
, etc. or aforeach
loop), theGetEnumerator()
method is called and enumeration starts. This means thatNextProviderEnumerator
takes place. This object is initialized with the current value ofExpression
as of the time of enumeration, thus it has complete information to parse it, figure out what needs to be done, and then do it using theINextProvider
that it is assigned. The classExpressionParser
is used to convert the expression into a series of “nodes” that act on each item in the underlyingINextProvider
and do the appropriate thing based on what it is (e.g. if it’s aWhereNode
, it will have a predicate that it will run on each item).If the method is something that returns a scalar (e.g.
Any
,All
,First
, etc.),NextProviderQueryProvider.Execute
is called. In our case, we simply pass control toNextProviderEnumerator
to enumerate as mentioned in the previous point, and then perform the appropriate action. We do this by getting anIEnumerable<T>
that usesNextProviderEnumerator
as its enumerator (and that is theNextProviderEnumerable
class), and then calling the appropriateIEnumerable
version of theIQueryable
method that has been called. All of this is handled by theExpressionExecutor
class.
As of now, only the following methods are supported: All
, Any
, Cast
, Count
, Distinct
, ElementAt
, ElementAtOrDefault
, First
, FirstOrDefault
, Last
, LastOrDefault
, LongCount
, OfType
, Select
, SelectMany
, Single
, SingleOrDefault
, Skip
, Take
and Where
. If you try to use any other methods, you will get an exception. Even within these methods, if you try to use a variant that is not supported, you will get an exception.
Next time, we’ll try our hands at a more real world implementation, i.e. a tiny, tiny ORM.
Tags: csharp dotnet linq
Previous: Writing Your Own LINQ Provider- Part 2
Next: Writing Your Own LINQ Provider- Part 4