-1

I want to create a deep copy of an object. I chose to use the automapper because this way I dont have to edit classes and add there any extra code. Plus I use automapper for mapping my classes to DTOs.

On my surprise when I wanted to do a copy like:

var original = new TrainingSetDto()
var output = _mapper.Map<TrainingSetDto>(original);

the output variable retrieved reference to the original variable (shallow copy).

How to achieve my wanted result (deep copy of the original) to get new instance with same properties ?

E.g. on this blog https://jshowers.com/create-deep-copies-of-object-in-c-using-automapper/ was mentioned that static call Mapper.Map<Person>(originalPerson); is the way how to do so. But these static methods are not there anymore.

Could you please point me to the right direction ?

Kebechet
  • 1,461
  • 15
  • 31
  • 1
    You need to explicitly create maps for all the classes you want copied, otherwise they'll just be assigned. – Lucian Bargaoanu Oct 16 '22 at 14:22
  • I have tried to add `CreateMap();` into my AutoMapper config but even then I received just the copy by reference. – Kebechet Oct 16 '22 at 14:25
  • That's just wrong. A repro would help. Make a [gist](https://gist.github.com/lbargaoanu/9c7233441c3a3413cc2b9b9ebb5964a9) that we can execute and see fail. – Lucian Bargaoanu Oct 16 '22 at 14:33
  • @LucianBargaoanu It works as you said but just in Console app. In API project it doesnt. Repro here: https://github.com/Kebechet/AutoMapperProblem Project: `ApiTest` ...just trigger the Get endpoint through swagger to trigger the functionality – Kebechet Oct 16 '22 at 15:52
  • Seems like a bug in AutoMapper dependency injection package. See my discussion: https://github.com/AutoMapper/AutoMapper/discussions/4107#discussioncomment-3890041 – Kebechet Oct 16 '22 at 16:10

1 Answers1

0

As @LucianBargoanu pointed out it is possible to do a deep copy by AutoMapper. By firstly specifying map for your object e.g.

CreateMap<TrainingSetDto, TrainingSetDto>();

and then just calling your mapping function

var copy = _mapper.Map<TrainingSetDto>(original);

I was able to reproduce the correct behavior in the Console app but in the API project it still didnt work correctly.

Finally the problem in the API project was in the dependency injection package: AutoMapper.Extensions.Microsoft.DependencyInjection. And thats why when I replaced:

services.AddAutoMapper(provider => new MapperConfiguration(cfg =>
{
    cfg.AddProfile(new AutoMapperProfile());
    cfg.AddMaps(Assembly.GetExecutingAssembly().GetReferencedAssemblies().Select(x => x.FullName));
}));

by:

services.AddSingleton(provider => new MapperConfiguration(cfg => {
    cfg.AddProfile(new AutoMapperProfile());
    cfg.AddMaps(Assembly.GetExecutingAssembly().GetReferencedAssemblies().Select(x => x.FullName));
}).CreateMapper());

it started to work correcly.

PS: I put my findings also to the discussion thread on AutoMapper github, but for some reason they deleted my comment and locked the thread.

Kebechet
  • 1,461
  • 15
  • 31
  • Since I was already using automapper within the exsiting project, this solution was the most appropriate to achieve deepcopy, over other deep copy methods. – Aravind Mar 12 '23 at 01:27