-
Notifications
You must be signed in to change notification settings - Fork 30
Implement partial class support for projectable members #193
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
Open
koenbeuk
wants to merge
1
commit into
master
Choose a base branch
from
feat/partial-class-companion-nesting
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...alClassWithPrivateMembersTests.FilterOnPrivateProjectableProperty.DotNet10_0.verified.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| SELECT [p].[Id] | ||
| FROM [PartialEntity] AS [p] | ||
| WHERE [p].[Id] * 2 + 1 > 5 |
3 changes: 3 additions & 0 deletions
3
...ialClassWithPrivateMembersTests.FilterOnPrivateProjectableProperty.DotNet8_0.verified.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| SELECT [p].[Id] | ||
| FROM [PartialEntity] AS [p] | ||
| WHERE [p].[Id] * 2 + 1 > 5 |
3 changes: 3 additions & 0 deletions
3
...ialClassWithPrivateMembersTests.FilterOnPrivateProjectableProperty.DotNet9_0.verified.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| SELECT [p].[Id] | ||
| FROM [PartialEntity] AS [p] | ||
| WHERE [p].[Id] * 2 + 1 > 5 |
2 changes: 2 additions & 0 deletions
2
...tialClassWithPrivateMembersTests.SelectPrivateProjectableProperty.DotNet10_0.verified.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| SELECT [p].[Id] * 2 + 1 | ||
| FROM [PartialEntity] AS [p] |
2 changes: 2 additions & 0 deletions
2
...rtialClassWithPrivateMembersTests.SelectPrivateProjectableProperty.DotNet8_0.verified.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| SELECT [p].[Id] * 2 + 1 | ||
| FROM [PartialEntity] AS [p] |
2 changes: 2 additions & 0 deletions
2
...rtialClassWithPrivateMembersTests.SelectPrivateProjectableProperty.DotNet9_0.verified.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| SELECT [p].[Id] * 2 + 1 | ||
| FROM [PartialEntity] AS [p] |
59 changes: 59 additions & 0 deletions
59
...s/EntityFrameworkCore.Projectables.FunctionalTests/PartialClassWithPrivateMembersTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using EntityFrameworkCore.Projectables.FunctionalTests.Helpers; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using VerifyXunit; | ||
| using Xunit; | ||
|
|
||
|
Comment on lines
+1
to
+7
|
||
| namespace EntityFrameworkCore.Projectables.FunctionalTests | ||
| { | ||
| /// <summary> | ||
| /// An entity that uses the partial class pattern to expose private projectable members. | ||
| /// Without the partial keyword, <see cref="PartialEntity.Total"/> could not call the private | ||
| /// <see cref="PartialEntity.DoubleId"/> because the generated companion class would be in a | ||
| /// separate namespace and lack access to private members. | ||
| /// </summary> | ||
| public partial record PartialEntity | ||
| { | ||
| public int Id { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Public projectable that delegates to a private projectable. | ||
| /// Requires the companion to be nested inside this type so it can call DoubleId. | ||
| /// </summary> | ||
| [Projectable] | ||
| public int Total => DoubleId + 1; | ||
|
|
||
| /// <summary> | ||
| /// Private projectable — only accessible from within this type. | ||
| /// The companion is generated as a nested class inside <see cref="PartialEntity"/> (partial). | ||
| /// </summary> | ||
| [Projectable] | ||
| private int DoubleId => Id * 2; | ||
| } | ||
|
|
||
| public class PartialClassWithPrivateMembersTests | ||
| { | ||
| [Fact] | ||
| public Task FilterOnPrivateProjectableProperty() | ||
| { | ||
| using var dbContext = new SampleDbContext<PartialEntity>(); | ||
|
|
||
| var query = dbContext.Set<PartialEntity>() | ||
| .Where(x => x.Total > 5); | ||
|
|
||
| return Verifier.Verify(query.ToQueryString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public Task SelectPrivateProjectableProperty() | ||
| { | ||
| using var dbContext = new SampleDbContext<PartialEntity>(); | ||
|
|
||
| var query = dbContext.Set<PartialEntity>() | ||
| .Select(x => x.Total); | ||
|
|
||
| return Verifier.Verify(query.ToQueryString()); | ||
| } | ||
| } | ||
| } | ||
16 changes: 16 additions & 0 deletions
16
...ore.Projectables.Generator.Tests/PartialClassTests.NonPartialClass_Unchanged.verified.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // <auto-generated/> | ||
| #nullable disable | ||
| using EntityFrameworkCore.Projectables; | ||
| using Foo; | ||
|
|
||
| namespace EntityFrameworkCore.Projectables.Generated | ||
| { | ||
| [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] | ||
| static class Foo_C_Foo | ||
| { | ||
| static global::System.Linq.Expressions.Expression<global::System.Func<global::Foo.C, int>> Expression() | ||
| { | ||
| return (global::Foo.C @this) => 1; | ||
| } | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
...sTests.PartialClass_MethodOverloads_EachGetsUniqueCompanionInsidePartialType.verified.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| [ | ||
| // <auto-generated/> | ||
| #nullable disable | ||
| using EntityFrameworkCore.Projectables; | ||
| using Foo; | ||
|
|
||
| namespace Foo | ||
| { | ||
| partial class C | ||
| { | ||
| [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] | ||
| static class Foo_C_Compute_P0_int | ||
| { | ||
| static global::System.Linq.Expressions.Expression<global::System.Func<global::Foo.C, int, int>> Expression() | ||
| { | ||
| return (global::Foo.C @this, int x) => x + 1; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // <auto-generated/> | ||
| #nullable disable | ||
| using EntityFrameworkCore.Projectables; | ||
| using Foo; | ||
|
|
||
| namespace Foo | ||
| { | ||
| partial class C | ||
| { | ||
| [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] | ||
| static class Foo_C_Compute_P0_string | ||
| { | ||
| static global::System.Linq.Expressions.Expression<global::System.Func<global::Foo.C, string, int>> Expression() | ||
| { | ||
| return (global::Foo.C @this, string s) => s.Length; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ] |
49 changes: 49 additions & 0 deletions
49
...sTests.PartialClass_MethodOverloads_Registry_BothEntriesUseClrNestedTypeName.verified.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // <auto-generated/> | ||
| #nullable disable | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq.Expressions; | ||
| using System.Reflection; | ||
|
|
||
| namespace EntityFrameworkCore.Projectables.Generated | ||
| { | ||
| [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] | ||
| internal static class ProjectionRegistry | ||
| { | ||
| private static Dictionary<nint, LambdaExpression> Build() | ||
| { | ||
| const BindingFlags allFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static; | ||
| var map = new Dictionary<nint, LambdaExpression>(); | ||
|
|
||
| Register(map, typeof(global::Foo.C).GetMethod("Compute", allFlags, null, new global::System.Type[] { typeof(int) }, null), "Foo.C+Foo_C_Compute_P0_int"); | ||
| Register(map, typeof(global::Foo.C).GetMethod("Compute", allFlags, null, new global::System.Type[] { typeof(string) }, null), "Foo.C+Foo_C_Compute_P0_string"); | ||
|
|
||
| return map; | ||
| } | ||
|
|
||
| private static readonly Dictionary<nint, LambdaExpression> _map = Build(); | ||
|
|
||
| public static LambdaExpression TryGet(MemberInfo member) | ||
| { | ||
| var handle = member switch | ||
| { | ||
| MethodInfo m => (nint?)m.MethodHandle.Value, | ||
| PropertyInfo p => p.GetMethod?.MethodHandle.Value, | ||
| ConstructorInfo c => (nint?)c.MethodHandle.Value, | ||
| _ => null | ||
| }; | ||
|
|
||
| return handle.HasValue && _map.TryGetValue(handle.Value, out var expr) ? expr : null; | ||
| } | ||
|
|
||
| private static void Register(Dictionary<nint, LambdaExpression> map, MethodBase m, string exprClass) | ||
| { | ||
| if (m is null) return; | ||
| var exprType = m.DeclaringType?.Assembly.GetType(exprClass); | ||
| var exprMethod = exprType?.GetMethod("Expression", BindingFlags.Static | BindingFlags.NonPublic); | ||
| if (exprMethod is not null) | ||
| map[m.MethodHandle.Value] = (LambdaExpression)exprMethod.Invoke(null, null)!; | ||
| } | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
...tor.Tests/PartialClassTests.PartialClass_NestedPartialType_TwoLevelShellWrap.verified.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // <auto-generated/> | ||
| #nullable disable | ||
| using EntityFrameworkCore.Projectables; | ||
| using Foo; | ||
|
|
||
| namespace Foo | ||
| { | ||
| partial class Outer | ||
| { | ||
| partial class Inner | ||
| { | ||
| [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] | ||
| static class Foo_Outer_Inner_Value | ||
| { | ||
| static global::System.Linq.Expressions.Expression<global::System.Func<global::Foo.Outer.Inner, int>> Expression() | ||
| { | ||
| return (global::Foo.Outer.Inner @this) => 99; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
...ialClassTests.PartialClass_PrivateFieldAccess_CompanionCanAccessPrivateField.verified.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // <auto-generated/> | ||
| #nullable disable | ||
| using EntityFrameworkCore.Projectables; | ||
| using Foo; | ||
|
|
||
| namespace Foo | ||
| { | ||
| partial class C | ||
| { | ||
| [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] | ||
| static class Foo_C_GetSecret | ||
| { | ||
| static global::System.Linq.Expressions.Expression<global::System.Func<global::Foo.C, int>> Expression() | ||
| { | ||
| return (global::Foo.C @this) => @this._secret; | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
projectable.IsDeclaringTypePartialis true, the generator wraps the companion in all containing types and emits each wrapper aspartial. This will break compilation if the projectable member is declared in a partial nested type whose outer containing type(s) are not partial (the generatedpartial class Outer { ... }will conflict with the user’s non-partialclass Outer). Consider requiring the entire containing type chain to be partial before nesting (or falling back to theEntityFrameworkCore.Projectables.Generatednamespace / emitting a diagnostic when an outer type is not partial).