Skip to content

Add support for projectable constructors#155

Closed
PhenX wants to merge 36 commits intomasterfrom
feature/projectable-constructor
Closed

Add support for projectable constructors#155
PhenX wants to merge 36 commits intomasterfrom
feature/projectable-constructor

Conversation

@PhenX
Copy link
Collaborator

@PhenX PhenX commented Feb 22, 2026

No description provided.

Copilot AI and others added 30 commits February 14, 2026 11:26
- Created BlockStatementConverter to transform block bodies to expressions
- Added support for simple return statements
- Added support for if-else statements (converted to ternary)
- Added support for local variable declarations (inlined)
- Added diagnostics for unsupported statements (EFP0003)
- Added comprehensive test cases
- Updated existing test that expected block methods to fail

Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
- Created 7 functional tests demonstrating EF Core SQL translation
- Added comprehensive documentation explaining feature, limitations, and benefits
- All 174 tests passing across all projects

Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
- Support if statements without else clause (uses default or fallback return)
- Support switch statements (converted to nested conditionals)
- Handle if { return x; } return y; pattern
- Added 5 generator tests and 4 functional tests
- Updated documentation with new features and SQL examples
- All 182 tests passing (84 generator + 76 functional + 22 unit)

Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
- Rename test to better reflect implicit return pattern
- Add clarifying comment about control flow in BlockStatementConverter
- All tests still passing

Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
…tion

# Conflicts:
#	tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.cs
- Add diagnostic for empty block bodies (EFP0003)
- Fix transitive local variable inlining (var a = 1; var b = a + 2; now fully expands)
- Add warning when local variables are used multiple times (semantics preservation)
- Prevent locals in nested blocks from leaking into outer scopes
- Fix documentation to show compilable C# code (no implicit returns)
- Add tests for transitive locals and nested block restrictions
- All 197 tests passing (96 net8.0 + 101 net10.0)

Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
- Apply ReplaceLocalVariables to if statement conditions
- Apply ReplaceLocalVariables to switch expressions
- Apply ReplaceLocalVariables to case label values
- Remove double BOM character from ExpressionSyntaxRewriter.cs
- Fix documentation to match actual behavior (no multiple usage warning)
- Add tests for locals in if conditions and switch expressions
- All 201 tests passing (98 net8.0 + 103 net10.0)

Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
- Add specific diagnostics (EFP0004, EFP0005) for side effects
- Detect property assignments with clear error messages
- Detect compound assignments (+=, -=, etc.)
- Detect increment/decrement operators (++, --)
- Warn about non-projectable method calls
- Error messages now point to the exact problematic line
- All 209 tests passing (102 net8.0 + 107 net10.0)

Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
- Document all detected side effects with examples
- Show before/after comparison of error messages
- Explain diagnostic codes EFP0004 and EFP0005
- Provide clear guidance for developers

Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
- Add VisitIsPatternExpression to ExpressionSyntaxRewriter
- Convert pattern matching to equivalent expressions:
  - RecursivePattern: entity is { Prop: value } → null check + property checks
  - RelationalPattern: value is > 100 → value > 100
  - ConstantPattern: entity is null → entity == null
  - UnaryPattern: entity is not null → !(entity == null)
- Add comprehensive tests for all pattern types
- All 217 tests passing (106 net8.0 + 111 net10.0)

Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
- Document all supported pattern types with examples
- Explain conversion logic and benefits
- Provide complex examples showing nested patterns
- Document limitations and error handling

Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Pattern matching support has been moved to a separate branch/PR.
This PR now focuses solely on block-bodied method support:
- If-else statements
- Switch statements
- Local variables
- Side effect detection

Reverted commits:
- adc95f5: Add documentation for pattern matching support
- 31f4267: Fix pattern matching support in block-bodied methods
- f2a805e: Initial exploration - understand pattern matching crash issue

All 209 tests passing (102 net8.0 + 107 net10.0 generator tests, plus functional and unit tests)

Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Code Review Fixes:
- Use proper type resolution for [Projectable] attribute check (SymbolEqualityComparer)
- Add parentheses when inlining local variables to preserve operator precedence
- Restrict multiple if-without-else pattern to simple return bodies

Documentation Updates:
- Add block-bodied methods FAQ section to README with examples
- Merge SideEffectDetection.md content into BlockBodiedMethods.md
- Remove standalone SideEffectDetection.md file
- Link to BlockBodiedMethods.md from README

All 209 tests passing

Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds support for [Projectable] constructors in Entity Framework Core Projectables. This feature allows developers to mark constructors with the [Projectable] attribute, enabling EF Core to convert constructor calls in LINQ queries into SQL-compatible projections. The implementation transforms constructor bodies into object-initializer expressions (new T() { Prop = ... }) that EF Core can translate to SQL.

Changes:

  • Extended ProjectableAttribute to support constructors (AttributeTargets.Constructor)
  • Added source generator logic to process projectable constructors and convert their bodies into member-initializer expressions
  • Implemented runtime expression replacement for NewExpression nodes in LINQ queries
  • Added diagnostic EFP0007 for missing parameterless constructors
  • Added comprehensive test coverage with 22 test scenarios covering various constructor patterns

Reviewed changes

Copilot reviewed 102 out of 102 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/EntityFrameworkCore.Projectables.Abstractions/ProjectableAttribute.cs Extended attribute to allow constructors as targets
src/EntityFrameworkCore.Projectables/Services/ProjectionExpressionResolver.cs Added constructor lookup support using "_ctor" naming convention
src/EntityFrameworkCore.Projectables/Services/ProjectableExpressionReplacer.cs Added VisitNew method to expand projectable constructors at runtime with recursion protection
src/EntityFrameworkCore.Projectables.Generator/ProjectableInterpreter.cs Main code generation logic for constructors including base/this initializer handling
src/EntityFrameworkCore.Projectables.Generator/ConstructorBodyConverter.cs New file that converts constructor bodies to member-init expressions with support for locals, if/else, and property references
src/EntityFrameworkCore.Projectables.Generator/Diagnostics.cs Added EFP0007 diagnostic for missing parameterless constructors
src/EntityFrameworkCore.Projectables.Generator/AnalyzerReleases.Shipped.md Documentation for new diagnostic
tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.cs 22 new test cases covering constructor scenarios
tests/EntityFrameworkCore.Projectables.FunctionalTests/ProjectableConstructorTests.cs Functional tests verifying SQL generation for various constructor patterns
tests/.../ProjectionExpressionGeneratorTests.ProjectableConstructor_*.verified.txt Verified snapshots of generated code for generator tests
tests/.../ProjectableConstructorTests.Select_*.verified.txt Verified snapshots of generated SQL for functional tests

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

# Conflicts:
#	docs/BlockBodiedMembers.md
#	src/EntityFrameworkCore.Projectables.Generator/AnalyzerReleases.Shipped.md
#	src/EntityFrameworkCore.Projectables.Generator/BlockStatementConverter.cs
#	src/EntityFrameworkCore.Projectables.Generator/Diagnostics.cs
#	src/EntityFrameworkCore.Projectables.Generator/ProjectableInterpreter.cs
#	tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.BlockBodiedMethod_IfWithoutElse_ImplicitReturn.verified.txt
#	tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.BlockBodiedMethod_SwitchStatement_WithoutDefault.verified.txt
#	tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.cs
@PhenX PhenX changed the base branch from copilot/support-classic-methods-transformation to master March 1, 2026 14:29
@PhenX PhenX closed this Mar 1, 2026
@PhenX PhenX deleted the feature/projectable-constructor branch March 1, 2026 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants