Property Spread Initializer #9725
Replies: 1 comment
-
A big problem with these proposals is they fail to address a few things.
Not to mention, with EntityFramework Core, the need for "mappers" is greatly reduced. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Property Spread Initializer
Summary
This proposal introduces a property spread initializer syntax that allows automatic property assignment from another object when property names match. This reduces boilerplate code in object initialization scenarios where multiple properties need to be copied from a source object to a target object, similar to object spread operators in JavaScript and other modern languages.
Motivation
Current Verbose Pattern
In many C# scenarios, we frequently need to initialize an object by copying properties from another object with matching property names. Currently, this requires manually assigning each property individually:
Problems with Current Approach
Verbose and Repetitive: Code becomes unnecessarily long when dealing with objects with many properties
Error-Prone: Manual copying increases the risk of missing properties or typos
Maintenance Burden: When new properties are added, developers must remember to update all initialization sites
Reduced Readability: Important business logic gets buried in repetitive assignment code
Real-World Use Cases
DTO to Entity Mapping: Converting API DTOs to domain entities
Configuration Objects: Initializing complex configuration objects
Factory Patterns: Creating instances with pre-configured properties
Builder Patterns: Simplifying builder implementations
Detailed Design
Basic Syntax
Advanced Scenarios
Multiple Spread Operations
With Anonymous Types
Type Safety and Compilation
Compile-Time Type Checking
Null Safety
Safety Considerations
Property Visibility Rules
Only public readable properties from source are considered
Only public writable properties on target are assigned
Private, protected, and internal properties are ignored
Assignment Precedence
Circular Reference Prevention
The spread operation is shallow and compile-time only, preventing:
Infinite recursion
Circular object graphs
Stack overflow scenarios
Performance Characteristics
Zero runtime overhead compared to manual assignments
Compile-time transformation to individual property assignments
No reflection or dynamic dispatch involved
Compiler Transformation
The spread operator would be compiled to explicit property assignments:
Benefits
Code Quality
Reduced Boilerplate: Eliminates repetitive assignment code
Improved Maintainability: Automatic inclusion of new matching properties
Better Readability: Clear intent when copying object state
Developer Experience
Intuitive Syntax: Familiar to developers from other languages
Tooling Support: Full IntelliSense and refactoring support
Easy Adoption: Non-breaking change that can be adopted incrementally
Alternatives Considered
Drawback: Restricted to records and same types
Language Design Principles Alignment
This proposal aligns with several C# design principles:
Productivity: Reduces repetitive code and cognitive load
Type Safety: Maintains C#'s strong typing guarantees
Performance: Zero-cost abstraction with compile-time resolution
Consistency: Follows pattern established by collection initializers and with-expressions
Implementation Considerations
Compiler Changes: Parser and semantic analysis updates
IDE Support: IntelliSense, completion, and quick actions-
Documentation: Clear guidelines and best practices
Backward Compatibility: Fully non-breaking change
Beta Was this translation helpful? Give feedback.
All reactions