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

Generic Existing Object Mapping #1409

Closed
SunnyVachhetA opened this issue Jul 22, 2024 · 5 comments
Closed

Generic Existing Object Mapping #1409

SunnyVachhetA opened this issue Jul 22, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@SunnyVachhetA
Copy link

Is your feature request related to a problem? Please describe.
I am trying to map existing object from DTO to Model in Entity Framework Core. It works for normal case by declaring void method as described in Existing Object Mapping.

In below code snippet it gives me error of - RMG001 A mapping method has an unsupported signature.

Describe the solution you'd like
API for updating directly working on existing object with Generic Mapping described in https://mapperly.riok.app/docs/configuration/generic-mapping/.

// Relevant new / adjusted API surface
// Expected generated code

[Mapper]
public partial class CarMapper
{
    public partial void Map<TDestination, TDestination>(TSource source, TDestination destination);
    public partial CarDto CarToCarDto(Car car);
    public partial Car CarDtoToCar(CarDto carDto);
}

Describe alternatives you've considered
I have tried with mentioning return type. But it gives error of method must have an implementation.

@SunnyVachhetA SunnyVachhetA added the enhancement New feature or request label Jul 22, 2024
@latonz
Copy link
Contributor

latonz commented Jul 22, 2024

How would you implement this method manually?

@SunnyVachhetA
Copy link
Author

SunnyVachhetA commented Jul 22, 2024

How would you implement this method manually?

For non-generic, It uses the existing object for mapping without creating new one.

Following that I can think of using the same instance of TDestination passed for mapping source entity.

TSource may or may not have properties that are available in TDestination. Some property values available in TDestination should not be override null by TSource.

I am thinking by above approach. Please note that I am learning about source generator, and I may have unexplored areas in this section.

@latonz
Copy link
Contributor

latonz commented Jul 22, 2024

My question is not really related to source generators. Instead I‘m interested in how you would implement this method manually, without any source generator support.

but I think i understood you wrong, you are not referring to queryable projection mappings correct?
then this looks like a duplicate of #884

@latonz latonz closed this as not planned Won't fix, can't repro, duplicate, stale Jul 22, 2024
@SunnyVachhetA
Copy link
Author

Yes @latonz, I was not referring to queryable projection. Generic existing object mapping for class or records only.

Without source generator, I will create a method that takes a existing destination object reference and second one DTO or other object, map them and return the same destination object reference.

@ste4net
Copy link

ste4net commented Jul 25, 2024

This was exactly what i was searching for... i reach this page from the RMG001 error and with this post i understand it it not possible to do what i was trying to do.
Just to add additional information i was searching for something like this.

public static partial void Map<Tsrc, Ttar>(Tsrc source, Ttar destination);
public static partial void CarToCarDto(Car src, CarDto target);
public static partial void CarDtoToCar(CarDto src, Car target);

with generated code..

   public static void Map<Tsrc, Ttar>(Tsrc source, Ttar targ)
   {
       Type targetType = typeof(Ttar);
       switch (source)
       {
           case Car c when targetType.IsAssignableFrom(typeof(CarDto)):
               CarToCarDto(source as Car, targ as CarDto);
               break;
           case CarDto c when targetType.IsAssignableFrom(typeof(Car)):
               CarDtoToCar(source as CarDto,targ as Car);
               break;
       };
   }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants