Can I use Mapperly with dependency injection? If so, how? and what are the advantages, like can I use IMapper? #1280
-
I love that it's source generated but, should I even be using it with dependency injection? if not, why not? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
My 2 cents; mappers should be static classes, as it reinforces their nature. i.e. they don't have dependencies of their own, and don't have side-effects. If it feels like you want to have it behind an interface, usually at that point it's a sign that the idea of a mapper is misunderstood, or the functionality that is being added belongs somewhere else. |
Beta Was this translation helpful? Give feedback.
-
gotcha gotcha, that's kind of what I was thinking, cuz either way the functionality of a mapper should be minimal enough and not involve business logic beyond runtime/language-based optimization that it does-not testing directly on the functions it provides besides transformation using the generated code or be complex enough to have multiple implementations right? but if this is the generally accepted philosophy behind object mappers, why is AutoMapper so popular yet comes with a huge range of features. And lastly, the benchmarks usually show that Mapperly is slightly faster than manual mapping, can you help me understand why that is? I have an idea that it uses .net source generater to create the code during compile time, reducing runtime overhead, I think I get it but is there like an example code or something somewhere, I want to understand this more. I am quite interested in this topic I newly found. |
Beta Was this translation helpful? Give feedback.
My 2 cents; mappers should be static classes, as it reinforces their nature. i.e. they don't have dependencies of their own, and don't have side-effects.
If it feels like you want to have it behind an interface, usually at that point it's a sign that the idea of a mapper is misunderstood, or the functionality that is being added belongs somewhere else.