In my code, I would like to make my IQueryable repositories. Thus, the linq expression tree will be the selection criterion.
Now, if I want to make fun of my repository in theory, it is very simple: just implement the interface of my repository (which is also an IQueryable object).
The implementation of my mock repository will only be in the memory collection, but my question is: do you know an easy way to implement the IQueryable interface of my layout, pass the request to my in-memory (IEnumerable) collection?
Thanks for your reply,
Some precision
The client object of my repository will use my repository this way:
The result is var = from the entry in MyRepository, where entry.Product == "SomeProduct" selects the entry;
What ToList or AsEnumerable does to execute the query and returns the result as a list or as IEnumerable. But I have to implement the IQueryable interface in my repository using IQueryProvider, which converts the expression into an IEnumerable object call.
Decision
The implementation of the solution is to delegate the IQueryable call to my inmemory collection using AsQueryable.
public class MockRepository : IQueryable<DomainObject>
{
private List<DomainObject> inMemoryList = new List<DomainObject>();
#region IEnumerable<DomainObject> Members
public IEnumerator<DomainObject> GetEnumerator()
{
return inMemoryList.GetEnumerator();
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return inMemoryList.GetEnumerator();
}
#endregion
#region IQueryable Members
public Type ElementType
{
get
{
return inMemoryList.AsQueryable().ElementType;
}
}
public Expression Expression
{
get
{
return inMemoryList.AsQueryable().Expression;
}
}
public IQueryProvider Provider
{
get
{
return inMemoryList.AsQueryable().Provider;
}
}
#endregion
}
source
share