我正在关注MSDN 上的一个示例系列,用于创建 LINQ 提供程序并且已经碰壁了。
我期望当我编写以下测试时,下面ExpressionVisitor
的源代码中的子类会VisitMethodCall
被调用。
[Fact]
public void DatabaseModeler_provides_table_modeler()
{
var q = new LightmapQuery<AspNetRoles>(new SqliteProvider2());
q.Where(role => role.Name == "Admin");
var result = q.ToList();
}
相反,会发生该VisitConstant
方法被调用。我认为这是因为当Provider
实例化时,它分配了它的Expression
属性 a ConstantExpression
。我不确定我是否做错了什么,或者 MSDN 上的指南是否存在问题,阻止我获取包含Where
方法调用的表达式。
IQueryable<T>
这是我拥有的和IQueryProvider
实现的源代码。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
namespace Lightmap.Querying
{
internal class SqliteQueryTranslator : ExpressionVisitor
{
internal StringBuilder queryBuilder;
internal string Translate(Expression expression)
{
this.queryBuilder = new StringBuilder();
this.Visit(expression);
return this.queryBuilder.ToString();
}
public override Expression Visit(Expression node)
{
return base.Visit(node);
}
protected override Expression VisitMethodCall(MethodCallExpression node)
{
if (node.Method.DeclaringType != typeof(IQueryable) && node.Method.Name != nameof(Enumerable.Where))
{
throw new NotSupportedException($"The {node.Method.Name} method is not supported.");
}
this.queryBuilder.Append($"SELECT * FROM {node.Method.DeclaringType.Name}");
return node;
}
protected override Expression VisitConstant(ConstantExpression node)
{
return node;
}
private static Expression StripQuotes(Expression expression)
{
while (expression.NodeType == ExpressionType.Quote)
{
expression = ((UnaryExpression)expression).Operand;
}
return expression;
}
}
public abstract class LightmapProvider : IQueryProvider
{
public IQueryable CreateQuery(Expression expression)
{
Type genericParameter = expression.GetType().GetGenericArguments().First();
return (IQueryable)Activator.CreateInstance(typeof(LightmapQuery<>)
.MakeGenericType(genericParameter), new object[] { this, expression });
}
public IQueryable<TElement> CreateQuery<TElement>(Expression expression) => new LightmapQuery<TElement>(this, expression);
object IQueryProvider.Execute(Expression expression)
{
return this.Execute(expression);
}
public TResult Execute<TResult>(Expression expression)
{
return (TResult)this.Execute(expression);
}
public abstract string GetQueryText(Expression expression);
public abstract object Execute(Expression expression);
}
public class SqliteProvider2 : LightmapProvider
{
public override object Execute(Expression expression)
{
var x = new SqliteQueryTranslator().Translate(expression);
return Activator.CreateInstance(typeof(List<>).MakeGenericType(TypeCache.GetGenericParameter(expression.Type, t => true)));
}
public override string GetQueryText(Expression expression)
{
throw new NotImplementedException();
}
}
public class LightmapQuery<TTable> : IOrderedQueryable<TTable>
{
public LightmapQuery(IQueryProvider provider)
{
this.Provider = provider;
this.Expression = Expression.Constant(this);
}
public LightmapQuery(IQueryProvider provider, Expression expression)
{
if (!typeof(IQueryable<TTable>).IsAssignableFrom(expression.Type))
{
throw new Exception();
}
this.Expression = expression;
this.Provider = provider;
}
public Type ElementType => typeof(TTable);
public Expression Expression { get; }
public IQueryProvider Provider { get; }
public IEnumerator<TTable> GetEnumerator()
{
return (this.Provider.Execute<IEnumerable<TTable>>(Expression)).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return (Provider.Execute<IEnumerable>(Expression)).GetEnumerator();
}
}
}
编辑
更新了我的单元测试以实际存储IQueryable
from .Where
,我仍然没有看到我的VisitMethodCall
调用。
[Fact]
public void DatabaseModeler_provides_table_modeler()
{
var q = new LightmapQuery<AspNetRoles>(new SqliteProvider2())
.Where(role => role.Name == "Admin");
var result = q.ToList();
}