Skip to content

Conversation

@roohial57
Copy link

This PR introduces two new overloads of the Adapt extension method in Mapster that make applying a small, temporary configuration extremely simple and concise:

Purpose:
Allow mapping with a temporary TypeAdapterConfig or a dedicated TypeAdapterSetter without modifying GlobalSettings.

Key Features:
Simplifies one-off mappings with minimal configuration.
Does not affect GlobalSettings, keeping existing mappings intact.
Supports both generic mapping (TSource -> TDestination) and mapping with a temporary configuration.

Tests:
Includes MSTest + Shouldly tests covering:
Mapping with temporary config
Mapping with setter
Ensuring GlobalSettings remain unchanged
Mapping with object initializer

Usage Example:

var result = source.Adapt(cfg =>
{
cfg.NewConfig<SourceDto, DestinationDto>()
.Map(dest => dest.Id, src => 42);
});

var result2 = source.Adapt<SourceDto, DestinationDto>(setter =>
{
setter.Map(dest => dest.Id, src => 99);
});

@DocSvartz
Copy link

@roohial57 Great job 👍

@andrerav @stagep I think this should be part of Mapster.Fluent, what do you think about this?

@roohial57
Copy link
Author

@roohial57 Great job 👍

@andrerav @stagep I think this should be part of Mapster.Fluent, what do you think about this?

I do not have any idea; what do you think?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roohial57 If you want to create a new configuration (.NewConfig()) both cases, then leave the original option.

It seems to me that the Option with Setter is more suitable for cases when you need to slightly change an existing configuration :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly for that reason, based on your earlier guidance, I kept the global configuration intact and apply changes only through these methods.
commit: use TypeAdapterConfig.GlobalSettings.Clone() as base config

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In you case using this :

var setter = config.NewConfig<TSource, TDestination>();

means that all the mapping settings need to be re-created. Is this what you were trying to achieve?

Here's a more detailed example.
Here, local settings use the existing Globalconfig settings, without changing the GlobalConfig.

     [TestMethod]
       public void Adapt_TemporaryConfig_ShouldNotModifyGlobalSettings()
       {
           // Arrange
           var source = new SourceDto("Charlie", 40);
           long id = 123;

           TypeAdapterConfig<SourceDto, DestinationDto>
                                          .NewConfig()
                                          .Map(dest => dest.Id, src => id)
                                          .Map(dest => dest.DestinationName, src => src.Name)
                                          .Map(dest => dest.DestinationAge, src => src.Age);

           // Act

           var mary = source.Adapt<SourceDto, DestinationDto>(setter =>
           {
               setter
               .Map(dest => dest.DestinationName, src => "Mary")
               .Map(dest => dest.DestinationAge, src => 25);
           });

           var john = source.Adapt<SourceDto, DestinationDto>(setter =>
           {
               setter
               .Map(dest => dest.DestinationName, src => "John")
               .Map(dest => dest.Id, src => 125);
           });

           var Charlie = source.Adapt<DestinationDto>(); // GlobalSettings not modify

           mary.DestinationAge.ShouldBe(25);
           mary.DestinationName.ShouldBe("Mary");
           mary.Id.ShouldBe(id);    // reusing GlobalSettings

           john.DestinationAge.ShouldBe(40); // reusing GlobalSettings
           john.DestinationName.ShouldBe("John");
           john.Id.ShouldBe(125);


           Charlie.DestinationAge.ShouldBe(40);  // GlobalSettings not modify
           Charlie.DestinationName.ShouldBe("Charlie"); // GlobalSettings not modify
           Charlie.Id.ShouldBe(id);  // GlobalSettings not modify
       }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants