MapsterMapper / Mapster

A fast, fun and stimulating object to object Mapper
MIT License
4.37k stars 332 forks source link

[Feature Request] Allow AddDestinationTransform to be run conditionally #464

Open smitty3268 opened 2 years ago

smitty3268 commented 2 years ago

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