C
C#2y ago
Anton

❔ Multiple configurations with automapper

I have two automapper configurations, the first one is used to project entities to dtos when querying ef core normally or when just mapping objects, while the second one is for use with OData. A while back I have asked about conventions of service configuration, and the answer was quite different from my intuitive solution. So what I'm asking here is what's the conventional way of registering multiple automapper configurations? A factory by name or index does not seem to be officially supported by the public API, should I make a factory myself? Maybe some other library or option exists?
16 Replies
Tvde1
Tvde12y ago
manual mapping is always a solution 🙂 libraries are there to help you, not stand in your way
Anton
AntonOP2y ago
you can't really use OData with DTOs without automapper
Tvde1
Tvde12y ago
ye you can source: me :p
Anton
AntonOP2y ago
it would be a shit ton of boilerplate and how are you planning to implement expand generically
Tvde1
Tvde12y ago
oftentimes I prefer a manual
public static XDto ToDto(this XEntity x)
{
return new XDto
{
...
};
}
public static XDto ToDto(this XEntity x)
{
return new XDto
{
...
};
}
hmm not sure about expand though
Anton
AntonOP2y ago
this is wrong for many reasons
Tvde1
Tvde12y ago
I've only done select and filters and orderbys
Anton
AntonOP2y ago
it would still fetch the whole entity from the db even if you need minimal data in the dto you can't use that for projections
Tvde1
Tvde12y ago
the .ProjectTo<>(configuration) has a configuration parameter. Can you use that to distinguish and use different configurations?
Anton
AntonOP2y ago
you can't remap properties back and forth yes I can, I can also create a mapper I'm asking mostly about di here what's the convention for in fact registering multiple mappers I'm thinking of just making a factory
Tvde1
Tvde12y ago
I wouldn't be so worried about doing everything through DI you're not going to mock automapper configurations, right?
Anton
AntonOP2y ago
no
Tvde1
Tvde12y ago
You could also make multiple interfaces
interface IODataMappingConfiguration : IConfiguration
interface IODataMappingConfiguration : IConfiguration
and then get that interface from DI
Anton
AntonOP2y ago
yeah I was thinking like a provider that gets you the odata mapper otherwise IMapper would get the default one that's kinda logical still, I'm pretty new to this, and want to learn best practices I do realize that any single solution is rarely the best at all things
Tvde1
Tvde12y ago
if you make your own "factory"/"provider" that provides the correct IMapper, that's also fine as long as it's readable code and your team is also ok with it
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server