C
C#10mo ago
Kymera

AutoMapper best practices

Hey guys! I am using AutoMapper to map my service objects to DTOs, but i was wondering if I am doing it clean. [HttpPost] public IActionResult Get() { var map = _mapService.GetMap(); var result = _mapper.Map<MapDto>(map); return Ok(result); } How do I make sure that this works? I have created some tests for the controller but they don't feel quite right... Test startup method: public MapControllerTests() { mapService = new Mock<IMapService>(); mapper = new Mock<IMapper>(); var serviceResponse = new Map { Points = [new Point()] }; mapService.Setup(m => m.GetMap()).Returns(serviceResponse); mapper.Setup(m => m.Map<MapDto>(serviceResponse)).Returns(new MapDto { Points = [] }); // <-- Feels like too much logic in test mapController = new MapController(mapper.Object, mapService.Object); }
No description
24 Replies
Angius
Angius10mo ago
The best practice is to use the IQueryable extension Instead of fetching everything and the kitchen sink, and then discarding half of the data in mapping
Kymera
KymeraOP10mo ago
Hmmn how do i do that? 😵‍💫
Angius
Angius10mo ago
https://docs.automapper.org/en/stable/Queryable-Extensions.html You make it a part of your query
var thingDto = await context.Things
.Where(thing => thing.Id == id)
.ProjectTo<ThingDto>(mapper.ConfigurationProvider)
.FirstOrDefaultAsync();
var thingDto = await context.Things
.Where(thing => thing.Id == id)
.ProjectTo<ThingDto>(mapper.ConfigurationProvider)
.FirstOrDefaultAsync();
Something like that, IIRC (note the use of async/await as well, btw)
Kymera
KymeraOP10mo ago
But isn't that if i am using entity framework?
Angius
Angius10mo ago
What else are you using?
Kymera
KymeraOP10mo ago
I am trying to map an object "map" that is used in the service layer to "mapDto" that will only be used for the output of the controller
Kymera
KymeraOP10mo ago
for reference i have this config for the mapper
No description
Angius
Angius10mo ago
But... you are using EF somewhere, no?
Kymera
KymeraOP10mo ago
no :/
Angius
Angius10mo ago
What are you using, then?
Kymera
KymeraOP10mo ago
I don't have a database yet just wanted to separate DTOs from the models used in the service or is that stupid?
Angius
Angius10mo ago
So, uh, you're buidling your app outside-in...? First the API, then the services, lastly the database...?
Kymera
KymeraOP10mo ago
yea xD
Angius
Angius10mo ago
Well if you don't have the database then do whatever Makes no difference
Kymera
KymeraOP10mo ago
I started with some logic and started to be fun and all no mapping?
Angius
Angius10mo ago
Just be prepared to rip it all out and rewrite it once you do hook a database into it I mean, sure, use the mapper wherever Without a database it makes no difference where or how
Kymera
KymeraOP10mo ago
to be honest, I don't think that will be an issue... I am using a object as bd (while running) and when its time to have persistence just need to have that type on the bd split by tables or whatever its a singleton
leowest
leowest10mo ago
have u ever worked with PostgreSQL, MySQL, MSSQL?
Kymera
KymeraOP10mo ago
I have, MySQL, MSSQL and noSql too
leowest
leowest10mo ago
ok I got a lil worried when u said split by tables or whatever
Kymera
KymeraOP10mo ago
Dont worry, this is in early stages... i will create the bd soon
leowest
leowest10mo ago
im not worried I just relate to what Z said above and can see it happening here
Kymera
KymeraOP10mo ago
just was trying to understant the best practices
Mayor McCheese
Mayor McCheese10mo ago
Best practice is not to use automapper
Want results from more Discord servers?
Add your server