-
Notifications
You must be signed in to change notification settings - Fork 383
Add Adapt extensions with temporary TypeAdapterConfig and TypeAdapter… #828
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Conversation
…Setter without modifying GlobalSettings
|
@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? |
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
}
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);
});