3

We have an huge data driven application that involves multiple systems therefore a lot of mapping is needed. Due to performance issues we are gonna migrate from AutoMapper to Mapster.

Everything's good so far with Mapster but when mapping the Collections Mapster returns null value instead of empty Collection.

Automapper used to return Empty Collections by default but I am unable to figure out how to do that in Mapster.

I have tried doing the following but it does not work

TypeAdapterConfig.GlobalSettings.ForDestinationType<ICollection>().IgnoreNullValues(true);

TypeAdapterConfig.GlobalSettings.ForType(typeof(ICollection), typeof(ObservableCollection<>))
                        .IgnoreNullValues(true);

TypeAdapterConfig.GlobalSettings.ForType(typeof(ObservableCollection<>), typeof(ICollection))
                        .IgnoreNullValues(true);

Any help would be great

Hannan Ayub
  • 378
  • 2
  • 15

1 Answers1

0

I got it working thanks to Mapster team. Posting it here in case anyone else needs it

You can either configure it explicitly by:

TypeAdapterConfig.GlobalSettings.Default
    .AddDestinationTransform((IReadOnlyList<ChildDto> list) => list ?? new List<ChildDto>());

Mapster has also added generic support for AddDestinationTransform

config.Default.AddDestinationTransform(DestinationTransform.EmptyCollectionIfNull);

This worked for me.

Hannan Ayub
  • 378
  • 2
  • 15