Entity Framework Core – Custom Functions (Using IMethodCallTranslator)

In the previous blog post (Entity Framework Core: ROW_NUMBER Support) we saw how to use the custom function RowVersion with Entity Framework Core (EF). Now, we will look into the implementation.

In diesem Artikel:

pg
Pawel Gerr ist Architekt und Consultant bei Thinktecture. Er hat sich auf .NET Core Backends spezialisiert und kennt Entity Framework von vorne bis hinten.

In the next and the last post of this series we will look ath the 2nd approach that uses the method HasDbFunction in combination with HasTranslation.

  1. Entity Framework Core: ROW_NUMBER Support

  2. Entity Framework Core: Custom Functions (using IMethodCallTranslator)
  3. Entity Framework Core: Custom Functions (using HasDbFunction)

As an example we will use the extension method RowVersion with the ORDER BY part only. The actual code can be found on Azure DevOps: Thinktecture.EntityFrameworkCore

Define a new extension method for "DbFunctions"

At first we define an extension method RowNumber to be used in a LINQ query. The method itself contains no logic in it because it won’t be executed in .NET but translated to SQL.

Actually, it does not matter what class the extension method is defined for but I’m following the pattern the EF team uses for their functions like the methods DateDiffDay or Like.

				
					public static long RowNumber(this DbFunctions _, object orderBy)
{
   throw new InvalidOperationException("This method is for use with Entity Framework Core only and has no in-memory implementation.");
}
				
			

Having the extension method we are now able to define a RowNumber but this will lead to errors during the execution of the query because EF doesn’t know what to do with it.

				
					// Expected output: ROW_NUMBER() OVER(ORDER BY ProductId)
// but will throw an error at this point
...
  .Select(i => new
             {
                ...,
                RowNumber = EF.Functions.RowNumber(i.ProductId)
             })
				
			

Provide custom implementation of "IMethodCallTranslator"

EF needs some help to be able to translate the newly implemented method RowNumber. For that we create a new class and implement the method Translate of the interface IMethodCallTranslator

				
					Expression Translate(MethodCallExpression methodCallExpression);
				
			

Our implementation is going to be called with different method call expressions so we need to check whether the method is the one we are interested in or not. For easier check we will save the MethodInfo of RowNumber to a variable.

				
					public class SqlServerRowNumberTranslator : IMethodCallTranslator
{
   private static readonly MethodInfo _rowNumberMethod 
              = typeof(DbFunctionsExtensions).GetMethod(
                        nameof(DbFunctionsExtensions.RowNumber), 
                        new[] { typeof(DbFunctions), typeof(object) });
				
			

In Translate we check the method being passed in and if it’s not the RowNumber we return the methodCallExpression unchanged, otherwise we extract the ORDER BY columns.

				
					 public Expression Translate(MethodCallExpression methodCallExpression)
   {
      if (methodCallExpression.Method != _rowNumberMethod)
         return null;

      var orderByParams = ExtractParams(methodCallExpression.Arguments[1]);

				
			

Luckily, EF translated the parameter(s) provided to RowVersion (like i.ProductId) to expressions EF understands (like ColumnExpression). The only thing we do is to check whether we have 1 or more „columns„.

				
					private static ReadOnlyCollection<Expression> ExtractParams(Expression parameter)
{
   if (parameter is ConstantExpression constant
       && constant.Value is IEnumerable<Expression> enumerable)
   {
       return enumerable.ToList().AsReadOnly();
   }

   return new List<Expression> { parameter }.AsReadOnly();
}
				
			

Back to method Translate.

After having the ORDER BY expressions we return a new custom expression RowNumberExpression that is responsible for generation of the SQL.

				
					 //...
     return new RowNumberExpression(orderByParams);
}

				
			

The RowNumberExpression derives from Expression and has 2 methods of interest: Accept for rendering SQL and VisitChildren for leting all kind of visitors to visit „our“ inner expressions.

First, we implement the method VisitChildren and other few not-that-exciting members.

				
					public class RowNumberExpression : Expression
{
   private readonly IReadOnlyCollection<Expression> _orderBy;

   public override ExpressionType NodeType => ExpressionType.Extension;
   public override Type Type => typeof(long);
   public override bool CanReduce => false;

   public RowNumberExpression(IReadOnlyCollection<Expression> orderBy)
   {
      _orderBy = orderBy;
   }

   protected override Expression VisitChildren(ExpressionVisitor visitor)
   {
      var visitedOrderBy = visitor.VisitExpressions(_orderBy);

      if (ReferenceEquals(_orderBy, visitedOrderBy))
         return this;

      return new RowNumberExpression(visitedOrderBy);
   }
				
			

The method VisitExpressions inside VisitChildren is a helper method that iterates over the expressions and calls visitor.Visit(expression). The implementation can be found in RelationalExpressionVisitorExtensions.cs on Azure DevOps.

The remaining method Accept is responsible for the generation of the SQL fragment. Yet again, this method is called with different kind of visitors but we are interested in IQuerySqlGenerator only. Having the SQL generator we let it generate some static SQL fragments and the ORDER BY columns.

				
					protected override Expression Accept(ExpressionVisitor visitor)
{
   if (!(visitor is IQuerySqlGenerator))
      return base.Accept(visitor);

   visitor.Visit(new SqlFragmentExpression("ROW_NUMBER() OVER( ORDER BY "));

   RenderColumns(visitor, _orderBy);

   visitor.Visit(new SqlFragmentExpression(")"));

   return this;
}

private static void RenderColumns(ExpressionVisitor visitor,
                                  IEnumerable<Expression> columns)
{
   var insertComma = false;

   foreach (var column in columns)
   {
      if (insertComma)
         visitor.Visit(new SqlFragmentExpression(", "));

      visitor.Visit(column);
      insertComma = true;
   }
}
				
			

Now, the extension method RowNumber can be translated by the SqlServerRowNumberTranslator to SQL. In theory… because we have to register the translater with EF.

Register custom "IMethodCallTranslator" with EF

An implementation of IMethodCallTranslator can be registered using an implementation of IMethodCallTranslatorPlugin that has just 1 property Translators.

				
					public class SqlServerMethodCallTranslatorPlugin : IMethodCallTranslatorPlugin
{
   public IEnumerable<IMethodCallTranslator> Translators { get; }

   public SqlServerMethodCallTranslatorPlugin()
   {
      Translators = new List<IMethodCallTranslator> 
                    {
                        new SqlServerRowNumberTranslator()
                    };
   }
}
				
			

Now, the plugin has to get into the dependency injection (DI) of the EF. For that we need to implement IDbContextOptionsExtension to get to an instance of IServiceCollection.

Please note that EF has its own DI container by default.

				
					public class SqlServerDbContextOptionsExtension : IDbContextOptionsExtension
{
      public string LogFragment => "'RowNumberSupport'=true";
    
      public bool ApplyServices(IServiceCollection services)
      {
         services.AddSingleton<IMethodCallTranslatorPlugin, 
                               SqlServerMethodCallTranslatorPlugin>();
         return false;
      }

      public long GetServiceProviderHashCode()
      {
         return 0;
      }

      public void Validate(IDbContextOptions options)
      {
      }
}
				
			

Last but not least, we need an easy method to add RowNumber support to a DbContext. Due to the fact that this is a SQL Server feature we implement an extension method for SqlServerDbContextOptionsBuilder instead of the general DbContextOptionsBuilder.

				
					public static class SqlServerDbContextOptionsBuilderExtensions
{
   public static SqlServerDbContextOptionsBuilder AddRowNumberSupport(
              this SqlServerDbContextOptionsBuilder sqlServerOptionsBuilder)
   {
       var infrastructure = (IRelationalDbContextOptionsBuilderInfrastructure)
                            sqlServerOptionsBuilder;
       var builder = (IDbContextOptionsBuilderInfrastructure)
                         infrastructure.OptionsBuilder;

       // if the extension is registered already then we keep it 
       // otherwise we create a new one
       var extension = infrastructure.OptionsBuilder.Options
                                     .FindExtension<SqlServerDbContextOptionsExtension>()
                       ?? new SqlServerDbContextOptionsExtension();
      builder.AddOrUpdateExtension(extension);

      return sqlServerOptionsBuilder;
   }
}

				
			

Summary

Although there are quite a few components we need to implement a custom function, in the end the amount of code is not that big. Especially, if you consider that the last part (registration with EF) is reusable for all your custom EF features you need.

In the last post of this series we will look at the 2nd approach that requires less code and is less flexible.

UPDATE

  • IMethodCallTranslator.Translate must return null if the method is not the one we wait for so EF continues on with other translators.
Kostenloser
Newsletter

Aktuelle Artikel, Screencasts, Webinare und Interviews unserer Experten für Sie

Verpassen Sie keine Inhalte zu Angular, .NET Core, Blazor, Azure und Kubernetes und melden Sie sich zu unserem kostenlosen monatlichen Dev-Newsletter an.

Newsletter Anmeldung
Diese Artikel könnten Sie interessieren
Database Access with Sessions
.NET
KP-round

Data Access in .NET Native AOT with Sessions

.NET 8 brings Native AOT to ASP.NET Core, but many frameworks and libraries rely on unbound reflection internally and thus cannot support this scenario yet. This is true for ORMs, too: EF Core and Dapper will only bring full support for Native AOT in later releases. In this post, we will implement a database access layer with Sessions using the Humble Object pattern to get a similar developer experience. We will use Npgsql as a plain ADO.NET provider targeting PostgreSQL.
15.11.2023
Old computer with native code
.NET
KP-round

Native AOT with ASP.NET Core – Overview

Originally introduced in .NET 7, Native AOT can be used with ASP.NET Core in the upcoming .NET 8 release. In this post, we look at the benefits and drawbacks from a general perspective and perform measurements to quantify the improvements on different platforms.
02.11.2023
.NET
KP-round

Optimize ASP.NET Core memory with DATAS

.NET 8 introduces a new Garbage Collector feature called DATAS for Server GC mode - let's make some benchmarks and check how it fits into the big picture.
09.10.2023
.NET CORE
pg

Incremental Roslyn Source Generators: High-Level API – ForAttributeWithMetadataName – Part 8

With the version 4.3.1 of Microsoft.CodeAnalysis.* Roslyn provides a new high-level API - the method "ForAttributeWithMetadataName". Although it is just 1 method, still, it addresses one of the biggest performance issue with Source Generators.
16.05.2023
AI
favicon

Integrating AI Power into Your .NET Applications with the Semantic Kernel Toolkit – an Early View

With the rise of powerful AI models and services, questions come up on how to integrate those into our applications and make reasonable use of them. While other languages like Python already have popular and feature-rich libraries like LangChain, we are missing these in .NET and C#. But there is a new kid on the block that might change this situation. Welcome Semantic Kernel by Microsoft!
03.05.2023
.NET
sg

.NET 7 Performance: Regular Expressions – Part 2

There is this popular quote by Jamie Zawinski: Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems."

In this second article of our short performance series, we want to look at the latter one of those problems.
25.04.2023