C
C#5mo 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
Angius5mo 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
Kymera5mo ago
Hmmn how do i do that? 😵‍💫
Angius
Angius5mo 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
Kymera5mo ago
But isn't that if i am using entity framework?
Angius
Angius5mo ago
What else are you using?
Kymera
Kymera5mo 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
Kymera5mo ago
for reference i have this config for the mapper
No description
Angius
Angius5mo ago
But... you are using EF somewhere, no?
Kymera
Kymera5mo ago
no :/
Angius
Angius5mo ago
What are you using, then?
Kymera
Kymera5mo 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
Angius5mo ago
So, uh, you're buidling your app outside-in...? First the API, then the services, lastly the database...?
Kymera
Kymera5mo ago
yea xD
Angius
Angius5mo ago
Well if you don't have the database then do whatever Makes no difference
Kymera
Kymera5mo ago
I started with some logic and started to be fun and all no mapping?
Angius
Angius5mo 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
Kymera5mo 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
leowest5mo ago
have u ever worked with PostgreSQL, MySQL, MSSQL?
Kymera
Kymera5mo ago
I have, MySQL, MSSQL and noSql too
leowest
leowest5mo ago
ok I got a lil worried when u said split by tables or whatever
Kymera
Kymera5mo ago
Dont worry, this is in early stages... i will create the bd soon
leowest
leowest5mo ago
im not worried I just relate to what Z said above and can see it happening here
Kymera
Kymera5mo ago
just was trying to understant the best practices
Mayor McCheese
Mayor McCheese5mo ago
Best practice is not to use automapper