Skip to content
This repository has been archived by the owner on Jan 16, 2022. It is now read-only.

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

Closed
ducmerida opened this issue Nov 11, 2020 · 9 comments
Closed

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

ducmerida opened this issue Nov 11, 2020 · 9 comments
Labels
enhancement New feature or request

Comments

@ducmerida
Copy link

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 cezarypiatek added the enhancement New feature or request label Nov 11, 2020
@cezarypiatek
Copy link
Owner

cezarypiatek commented Nov 11, 2020

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
Copy link
Author

ducmerida commented Nov 11, 2020

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

@cezarypiatek
Copy link
Owner

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

@cezarypiatek
Copy link
Owner

cezarypiatek commented Nov 11, 2020

@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
Copy link
Author

@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
Copy link
Owner

cezarypiatek commented Nov 11, 2020

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
Copy link
Author

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

@cezarypiatek
Copy link
Owner

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

@cezarypiatek
Copy link
Owner

An official version 1.20.463 with this feature has been released

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

No branches or pull requests

2 participants