Skip to content

Commit

Permalink
Generate change-tracking delegates in the compiled model.
Browse files Browse the repository at this point in the history
Part of #29761
  • Loading branch information
AndriySvyryd committed Aug 10, 2023
1 parent 5b556d3 commit f1f199e
Show file tree
Hide file tree
Showing 65 changed files with 2,373 additions and 647 deletions.
40 changes: 38 additions & 2 deletions src/EFCore.Design/Design/Internal/CSharpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Security;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Simplification;
using Microsoft.CodeAnalysis.Text;
Expand Down Expand Up @@ -1541,8 +1543,42 @@ public virtual string Statement(Expression node, ISet<string> collectedNamespace
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string Expression(Expression node, ISet<string> collectedNamespaces)
=> ToSourceCode(_translator.TranslateExpression(node, collectedNamespaces));
public virtual string Expression(
Expression node,
Dictionary<object, string>? knownInstances,
Dictionary<(MemberInfo, bool), string>? replacementMethods,
ISet<string> collectedNamespaces,
out bool isStatement)
{
Dictionary<object, ExpressionSyntax>? knownInstanceExpressions = null;
if (knownInstances != null)
{
knownInstanceExpressions = new();

foreach (var instancePair in knownInstances)
{
knownInstanceExpressions[instancePair.Key] = SyntaxFactory.IdentifierName(instancePair.Value);
}
}

Dictionary<(MemberInfo, bool), ExpressionSyntax>? replacementMethodExpressions = null;
if (replacementMethods != null)
{
replacementMethodExpressions = new();

foreach (var methodPair in replacementMethods)
{
replacementMethodExpressions[methodPair.Key] = SyntaxFactory.IdentifierName(methodPair.Value);
}
}

return ToSourceCode(_translator.TranslateExpression(
node,
knownInstanceExpressions,
replacementMethodExpressions,
collectedNamespaces,
out isStatement));
}

private static bool IsIdentifierStartCharacter(char ch)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Microsoft.EntityFrameworkCore.Query.Internal;

Expand Down Expand Up @@ -34,15 +35,23 @@ public interface ILinqToCSharpSyntaxTranslator
/// Translates a node representing an expression into a Roslyn syntax tree.
/// </summary>
/// <param name="node">The node to be translated.</param>
/// <param name="knownInstances">Collection of translations for statically known instances.</param>
/// <param name="replacementMethods">Collection of translations for non-public member accesses.</param>
/// <param name="collectedNamespaces">Any namespaces required by the translated code will be added to this set.</param>
/// <param name="isStatement">A value indicating whether the translation can only be used in a statement context.</param>
/// <returns>A Roslyn syntax tree representation of <paramref name="node" />.</returns>
/// <remarks>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </remarks>
SyntaxNode TranslateExpression(Expression node, ISet<string> collectedNamespaces);
SyntaxNode TranslateExpression(
Expression node,
Dictionary<object, ExpressionSyntax>? knownInstances,
Dictionary<(MemberInfo, bool), ExpressionSyntax>? replacementMethods,
ISet<string> collectedNamespaces,
out bool isStatement);

/// <summary>
/// Returns the captured variables detected in the last translation.
Expand Down
211 changes: 147 additions & 64 deletions src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs

Large diffs are not rendered by default.

Loading

0 comments on commit f1f199e

Please sign in to comment.