cezarypiatek / MappingGenerator

:arrows_counterclockwise: "AutoMapper" like, Roslyn based, code fix provider that allows to generate mapping code in design time.
https://marketplace.visualstudio.com/items?itemName=54748ff9-45fc-43c2-8ec5-cf7912bc3b84.mappinggenerator
MIT License
1.03k stars 120 forks source link

Use the existing instance instead of creating a new one #146

Closed ducmerida closed 4 years ago

ducmerida commented 4 years ago

With the following code

public class PersonEntity
{
     public int Id { get; set; }
     public string FirstName { get; set; }
     public string LastName { get; set; }
}

public class PersonModel
{
     public int Id { get; set; }
     public string FirstName { get; set; }
     public string LastName { get; set; }
}

MyPersonService.cs
public async Task SaveAsync(PersonModel model)
{
   var entity = (await repository.GetAsync(model.Id)) ?? new PersonEntity();

   entity = model;///Cannot convert implicit type => Generate explicit conversion

   repository.Save(entity)
}

I get the current result

MyPersonService.cs

public async Task SaveAsync(PersonModel model)
{
   var entity = (await repository.GetAsync(model.Id)) ?? new PersonEntity();

   entity = new PersonEntity
   {
        Id = model.Id,
        FirstName = model.FirstName,
    LastName = model.LastName
   };

   repository.Save(entity)
}

I would like to get the folowing code

MyPersonService.cs

public async Task SaveAsync(PersonModel model)
{
   var entity = (await repository.GetAsync(model.Id)) ?? new PersonEntity();

   entity.Id = model.Id;
   entity.FirstName = model.FirstName;
   enity.LastName = model.LastName;

   repository.Save(entity)
}
cezarypiatek commented 4 years ago

Hi @ducmerida Thanks for bringing this use case. I can implement it, maybe even as a separated code fix.

In the meantime please consider supporting MappingGenerator project

ducmerida commented 4 years ago

@cezarypiatek thanks, I've already bought you a coffee but I will buy 2 more :)

cezarypiatek commented 4 years ago

ohh I didn't know that, I though that the first coffee was from a guy who requested #145. Anyway, I truly appreciate.

cezarypiatek commented 4 years ago

@ducmerida

Here's a pre-release version that contains requested feature. Would you mind testing it and letting me know if it works as you expected? This new code fix can be triggered using option "Generate explicit conversion (Try re-use instance)"

VSIX: https://ci.appveyor.com/api/buildjobs/to5ovokll01202r2/artifacts/MappingGenerator%2FMappingGenerator%2FMappingGenerator.Vsix%2Fbin%2FRelease%2FMappingGenerator.vsix

Nuget: https://ci.appveyor.com/api/buildjobs/to5ovokll01202r2/artifacts/MappingGenerator%2FMappingGenerator%2FMappingGenerator%2Fbin%2FRelease%2FMappingGenerator.1.19.460.nupkg

ducmerida commented 4 years ago

@cezarypiatek thanks a lot for you quick implementation, I've tried the VSIX and it doesn't work, you have a little error on your code, I leave you a comment on your PR #147

cezarypiatek commented 4 years ago

Thanks for testing and spotting the mistake. Should be fixed now.

Fixed VSIX: https://ci.appveyor.com/api/buildjobs/3hubq0elm10fjrio/artifacts/MappingGenerator%2FMappingGenerator%2FMappingGenerator.Vsix%2Fbin%2FRelease%2FMappingGenerator.vsix

ducmerida commented 4 years ago

@cezarypiatek thanks, i confirm that this last VSIX works perfectly :)

cezarypiatek commented 4 years ago

Many thanks for requesting this feature, seems to be very useful, and for your collaboration. I will release an official version today later.

cezarypiatek commented 4 years ago

An official version 1.20.463 with this feature has been released