Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate change-tracking delegates in the compiled model. #31443

Merged
merged 3 commits into from
Jan 26, 2024
Merged
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
93 changes: 79 additions & 14 deletions src/EFCore.Design/Design/Internal/CSharpHelper.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections;
using System.Globalization;
using System.Numerics;
using System.Runtime.CompilerServices;
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 All @@ -27,7 +28,7 @@ public class CSharpHelper : ICSharpHelper
{
private readonly ITypeMappingSource _typeMappingSource;
private readonly Project _project;
private readonly LinqToCSharpSyntaxTranslator _translator;
private readonly RuntimeModelLinqToCSharpSyntaxTranslator _translator;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -45,11 +46,11 @@ public CSharpHelper(ITypeMappingSource typeMappingSource)
var projectInfo = ProjectInfo.Create(projectId, versionStamp, "Proj", "Proj", LanguageNames.CSharp);
_project = workspace.AddProject(projectInfo);
var syntaxGenerator = SyntaxGenerator.GetGenerator(workspace, LanguageNames.CSharp);
_translator = new LinqToCSharpSyntaxTranslator(syntaxGenerator);
_translator = new RuntimeModelLinqToCSharpSyntaxTranslator(syntaxGenerator);
}

private static readonly IReadOnlyCollection<string> Keywords = new[]
{
private static readonly IReadOnlyCollection<string> Keywords =
[
"__arglist",
"__makeref",
"__reftype",
Expand Down Expand Up @@ -131,7 +132,7 @@ public CSharpHelper(ITypeMappingSource typeMappingSource)
"void",
"volatile",
"while"
};
];

private static readonly IReadOnlyDictionary<Type, Func<CSharpHelper, object, string>> LiteralFuncs =
new Dictionary<Type, Func<CSharpHelper, object, string>>
Expand Down Expand Up @@ -1435,13 +1436,13 @@ public virtual string Fragment(AttributeCodeFragment fragment)
}

builder
.Append("[")
.Append('[')
.Append(attributeName);

if (fragment.Arguments.Count != 0
|| fragment.NamedArguments.Count != 0)
{
builder.Append("(");
builder.Append('(');

var first = true;
foreach (var value in fragment.Arguments)
Expand Down Expand Up @@ -1475,10 +1476,10 @@ public virtual string Fragment(AttributeCodeFragment fragment)
.Append(UnknownLiteral(item.Value));
}

builder.Append(")");
builder.Append(')');
}

builder.Append("]");
builder.Append(']');

return builder.ToString();
}
Expand Down Expand Up @@ -1552,17 +1553,81 @@ private string ToSourceCode(SyntaxNode node)
/// 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 Statement(Expression node, ISet<string> collectedNamespaces)
=> ToSourceCode(_translator.TranslateStatement(node, collectedNamespaces));
public virtual string Statement(
Expression node,
Dictionary<object, string>? constantReplacements,
Dictionary<MemberAccess, string>? memberAccessReplacements,
ISet<string> collectedNamespaces)
{
Dictionary<object, ExpressionSyntax>? constantReplacementExpressions = null;
if (constantReplacements != null)
{
constantReplacementExpressions = [];

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

Dictionary<MemberAccess, ExpressionSyntax>? memberAccessReplacementExpressions = null;
if (memberAccessReplacements != null)
{
memberAccessReplacementExpressions = [];

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

return ToSourceCode(_translator.TranslateStatement(
node,
constantReplacementExpressions,
memberAccessReplacementExpressions,
collectedNamespaces));
}

/// <summary>
/// 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.
/// </summary>
public virtual string Expression(Expression node, ISet<string> collectedNamespaces)
=> ToSourceCode(_translator.TranslateExpression(node, collectedNamespaces));
public virtual string Expression(
Expression node,
Dictionary<object, string>? constantReplacements,
Dictionary<MemberAccess, string>? memberAccessReplacements,
ISet<string> collectedNamespaces)
{
Dictionary<object, ExpressionSyntax>? constantReplacementExpressions = null;
if (constantReplacements != null)
{
constantReplacementExpressions = [];

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

Dictionary<MemberAccess, ExpressionSyntax>? memberAccessReplacementExpressions = null;
if (memberAccessReplacements != null)
{
memberAccessReplacementExpressions = [];

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

return ToSourceCode(_translator.TranslateExpression(
node,
constantReplacementExpressions,
memberAccessReplacementExpressions,
collectedNamespaces));
}

private static bool IsIdentifierStartCharacter(char ch)
{
Expand Down

This file was deleted.

Loading
Loading