Skip to content

proposal: Add "reverse conversion" methods from DTOs to the original Entities in the auto-generated APIΒ #228

@arika0093

Description

@arika0093

Feature Description

Currently, Linqraft generates conversion methods from Entities to DTOs, but does not generate reverse conversion methods.
This is due to the design consideration that "reverse conversion" can be complex in certain scenarios (e.g., when functions like Select, Count, FirstOrDefault are used internally).
However, in many common cases, reverse conversion is useful (as supported by many mapping libraries). Therefore, we propose providing an option to generate reverse conversion methods for simple cases.

Triggering Example

// A pattern where a separate factory class is provided and attributes are applied there could also be effective.
query.SelectExpr<Entity,EntityDto>(x => new { ... });

[LinqraftReverseConvertion<EntityDto>(IsStatic = true)]
public partial class EntityDtoReverseConverter;

// generated
public partial class EntityDtoReverseConverter
{
    public static Entity FromDto(EntityDto){ ... }
    public static IEnumerable<Entity> FromDtoProjection(IEnumerable<EntityDto>){ ... }
}

Generated Expected Output

Natural "reverse conversion" methods. Generate ToEntity methods for both EntityDto and IEnumerable<EntityDto>.

Additional Information

Whether reverse transformation is possible can be determined based on the following criteria. Using a whitelist-based transformation approach is simple.

  1. Simple property access (Item = x.Foo.Bar.Buz): Reverse transformation is possible. You simply perform the reverse operation (e.g., x.Foo.Bar.Buz = Item).
  2. Select/SelectMany/SelectExpr access: Reverse transformation is possible. You perform the reverse selection operation based on the original data structure.
  3. Where/First/Last/FirstOrDefault filtering: Reverse transformation is possible. You simply ignore it and behave as if there is no Where. The data after reverse transformation will be in a "missing" state.
  4. ToList/ToArray, etc.: Reverse transformation is possible. You simply ignore it and behave as if it were an IEnumerable.
  5. Aggregation operations (Count, Sum, Average, etc.): Reverse transformation is not possible. The transformation itself is not performed.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestgeneratorrelational source-generator

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions