Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ public ExpressionifyQueryTranslationPreprocessor(
public override Expression Process(Expression query)
{
var visitor = new ExpressionifyVisitor();
query = _innerPreprocessor.Process(query);
query = visitor.Visit(query);

if (visitor.HasReplacedCalls)
query = EvaluateExpression(query);

return _innerPreprocessor.Process(query);
return query;
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "EF1001:Internal EF Core API usage.", Justification = "<Pending>")]
Expand All @@ -50,7 +51,7 @@ private Expression EvaluateExpression(Expression query)
generateContextAccessors: false);

return visitor.ExtractParameters(query);

/* With EF9 something along these lines would have been the ideal solution:
ExpressionTreeFuncletizer funcletizer = new(
QueryCompilationContext.Model,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,11 @@ public TestDbContext(DbContextOptions options) : base(options)
{ }

public DbSet<TestEntity> TestEntities { get; set; } = null!;
public DbSet<TestEntity2> TestEntities2 { get; set; } = null!;

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<TestEntity2>().HasQueryFilter(x => x.IsFoo());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ public class TestEntity
public DateTime Created { get; set; }
}

public class TestEntity2
{
public int Id { get; set; }
public string Name { get; set; } = "";
}

public class TestAddress
{
public string? City { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ public static partial class TestEntityExtensions
[Expressionify]
public static bool IsJohnDoe(this TestEntity testEntity) => testEntity.Name == "John Doe";

[Expressionify]
public static bool IsFoo(this TestEntity2 testEntity) => testEntity.Name == "Foo";

[Expressionify]
public static string GetFoo(this TestEntity2 testEntity) => "Foo " + testEntity.Name;

[Expressionify]
public static bool IsSomething(this TestEntity testEntity) => testEntity.Name == Name;

Expand Down
12 changes: 12 additions & 0 deletions tests/Clave.Expressionify.Tests/DbContextExtensions/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ public void UseExpressionify_EvaluationMode_DefaultsToLimitedCompatibilityButCac
debugInfo["Expressionify:EvaluationMode"].ShouldBe(ExpressionEvaluationMode.LimitedCompatibilityButCached.ToString());
}

//[TestCase(ExpressionEvaluationMode.FullCompatibilityButSlow)]
[TestCase(ExpressionEvaluationMode.LimitedCompatibilityButCached)]
public void UseExpressionify_InFilter(ExpressionEvaluationMode mode)
{
using var dbContext = new TestDbContext(GetOptions(o => o.WithEvaluationMode(mode)));
var query = dbContext.TestEntities2.Select(e => e.GetFoo());

var sql = query.ToQueryString();
sql.ShouldStartWith("SELECT 'Foo ' || \"t\".\"Name\"");
sql.ShouldEndWith("WHERE \"t\".\"Name\" = 'Foo'");
}

private DbContextOptions GetOptions(Action<ExpressionifyDbContextOptionsBuilder>? optionsAction = null, bool useExpressionify = true)
{
var builder = new DbContextOptionsBuilder<TestDbContext>().UseSqlite("DataSource=:memory:");
Expand Down
Loading