Use .NET 8 Interceptors to allow Mapperly to be added to existing methods? #646
Replies: 2 comments 8 replies
-
Hey, thanks for the idea. I've been pretty excited by interceptors as well. I'm not sure if this idea would be approved. How would configuration work? Interestingly, what you suggest is already possible with current source generators. By scanning for usages of Iirc someone made a project which does this I'll try to find it later |
Beta Was this translation helpful? Give feedback.
-
The idea of the new .NET interceptors is to generate the implementation of methods dynamically at compile time by a source generator. The problem here is that at compile time it is not known what types the arguments will have at runtime. However, Mapperly is designed to generate all mappings for all supported types at compile time, which cannot work here. |
Beta Was this translation helpful? Give feedback.
-
Hi,
I've been reading a bit about a new feature coming in .NET 8 called "Interceptors" and it seems to me like these could be leveraged to extend Mapperly in a different direction. (I found this post by Khalid Abuhakmeh from the JetBrains team to also be helpful in understanding Interceptors)
Currently in our application, we use reflection to map from DTOs used by our front end to DTOs used by our backend. This is done with a generic extension method like this:
The benefit to this approach is that it can be applied to any type, but the drawback is that it's using reflection. To use Mapperly in its current form, we would need to implement partial classes/methods for each unique combination of frontend and backend DTO.
Would it be possible to use .NET 8 Interceptors to allow us to decorate our existing methods such that Mapperly would then generate the needed mapping across our application for any combination of DTOs passed to the method? Something like
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions