4
votes

I have a repository interface as below:

public interface IDataContext<TId> : IDisposable
{
    IQueryable<T> Repository<T>() where T : class, IEntity<TId>;
    T FindById<T>(TId id) where T : class, IEntity<TId>;
    void Insert<T>(T item) where T : class, IEntity<TId>;
    void Delete<T>(T item) where T : class, IEntity<TId>;
    void Commit();
}

Note that Repository<T> returns an IQueryable<T>.

I have a class that can wrap a LinqToSQL data context, with the Repository<T> method as below:

public IQueryable<T> Repository<T>() where T : class, IEntity<int>
{
    ITable table = _context.GetTable(GetEntityType<T>());
    return table.Cast<T>();
}

This works fine, I can do something like

new Repository(new SQLDataContext())
    .Repository<MyEntity>().Where(e => SqlMethods.Like(e.Id, "123%");

Now I've started thinking about caching but I have a problem.

I've created a class that wraps and implements an IDataContext<TId> that will cache results from calls to Repository<T> in memory. Something like the below:

public IQueryable<T> Repository<T>() where T : class, IEntity<TId>
{
    // Actual caching logic here.....        

    return _CachedEntities[typeof(T)].OfType<T>().AsQueryable<T>();
}

The issue I have is that now the IQueryable<T> I return is in-memory, not translated to SQL, so I get an exception about using SqlMethods.Like.

TL;DR: So, how can I create my caching repository wrapper in such a way that the calling classes don't need to worry about whether the IDataContext<T> it's dealing with is an in-memory repository (i.e. the caching one) or a normal LinqToSQL repository?

1
You're most likely calling AsQueryable on the cached IEnumerable<T>s when returning?Eren Ersönmez
@ErenErsönmez: Correct.George Duckett

1 Answers

2
votes

It's possible, you need to write custom IQueryProvider and IQueryable<T>:

public static class MySqlMethods
{
    public static bool Like(string matchExpression, string pattern)
    {
        //Your implementation
        return true;
    }
}

public class ChangeMethodsVisitor : ExpressionVisitor
{
    //This method will change SqlMethods to MySqlMethods.
    protected override Expression VisitMethodCall(MethodCallExpression node)
    {
        if (node.Method.DeclaringType == typeof(SqlMethods))
        {
            //Getting method from MySqlMethods class.
            var method = typeof(MySqlMethods).GetMethod(node.Method.Name,
                                                         node.Method.GetParameters()
                                                             .Select(info => info.ParameterType)
                                                             .ToArray());
            return Expression.Call(method, node.Arguments);
        }
        return base.VisitMethodCall(node);
    }
}

public class MyQueryProvider : IQueryProvider
{
    private static readonly ExpressionVisitor ExpressionVisitor = new ChangeMethodsVisitor();
    private readonly IQueryProvider _queryProvider;

    public MyQueryProvider(IQueryProvider queryProvider)
    {
        _queryProvider = queryProvider;
    }

    public IQueryable CreateQuery(Expression expression)
    {
        expression = ExpressionVisitor.Visit(expression);
        var queryable = _queryProvider.CreateQuery(expression);
        //Wrap queryable to MyQuery class.
        var makeGenericType = typeof(MyQuery<>).MakeGenericType(queryable.ElementType);
        return (IQueryable)makeGenericType.GetConstructor(new[] { typeof(IQueryable<>).MakeGenericType(queryable.ElementType) })
                                           .Invoke(new object[] { queryable });
    }

    public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
    {
        expression = ExpressionVisitor.Visit(expression);
        //Wrap queryable to MyQuery class.
        var queryable = _queryProvider.CreateQuery<TElement>(expression);
        return new MyQuery<TElement>(queryable);
    }

    public object Execute(Expression expression)
    {
        expression = ExpressionVisitor.Visit(expression);
        return _queryProvider.Execute(expression);
    }

    public TResult Execute<TResult>(Expression expression)
    {
        expression = ExpressionVisitor.Visit(expression);
        return _queryProvider.Execute<TResult>(expression);
    }
}

public class MyQuery<T> : IOrderedQueryable<T>
{
    private readonly IQueryable<T> _queryable;

    public MyQuery(IQueryable<T> queryable)
    {
        _queryable = queryable;
        Provider = new MyQueryProvider(_queryable.Provider);
    }

    public MyQuery(IEnumerable<T> enumerable)
        : this(enumerable.AsQueryable())
    {
    }

    public IEnumerator<T> GetEnumerator()
    {
        return _queryable.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public Expression Expression
    {
        get { return _queryable.Expression; }
    }

    public Type ElementType
    {
        get { return _queryable.ElementType; }
    }

    public IQueryProvider Provider { get; private set; }
}

And then you can use it:

var list = new List<string>(){"test", "test1"};

var myQuery = new MyQuery<string>(list);
var queryable = myQuery.Where(s => SqlMethods.Like(s, "123%")).ToArray();