Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

[Feature Request] Allow AddDestinationTransform to be run conditionally

Open smitty3268 opened this issue 3 years ago • 0 comments

I would like to run a specific transform on any DateTime's in my class, but only for those that have a specific custom Attribute applied to their property.

This can be accomplished a variety of different ways, but I think the cleanest way that would match up with the existing Mapster API is to add an optional conditional property to the AddDestinationTransform method.

Then I could configure the following:

TypeAdapterConfig<object, TDestination>.NewConfig().AddDestinationTransform(
        (DateTime dt) => CustomTransformMethod(dt), 
        member => member.GetCustomAttributes(true).OfType<MyCustomAttribute>().Any());

I'd be happy with any other alternative way to accomplish the same thing as well. Right now I've just added a custom interface which manually declares all the fields with the logic I want to use with them, but it would be nice to just use my pre-existing attributes directly.

TypeAdapterConfig.GlobalSettings.ForDestinationType<IMyTransform>().AfterMapping(result => result.MyTransform());
//MyTransform in the class then directly updates all the DateTime values I want, or alternatively it could use reflection but that seems like it would be worse for performance

smitty3268 avatar Aug 07 '22 06:08 smitty3268