Converting id's to entities
I have this kind of request:
BookRequest(
string name,
string description,
ICollection<long> authorIds)
I want to map it to an entity.
I consider there approaches:
1) Map it partially (name, description), then in a handler find the authors associated with passed authorIds and populate the entity Authors collection with them
2) Inject DB context into Mapper profile instance (Is it SOLID? Somebody said that it is not, but I feel like it is pretty ok)
3) Implement id->entity converter class to retrieve Authors by Ids and use it in the mapper.
Which do you consider the best? What are the other options?
17 Replies
in what piece of code do you want to get the entities associated with the
authorIds
?
somewhere it should do
In a mapping method
Preferably
But not sure if it is ok to do
like in an extension method?
yes
ToDto() something?
yes!
I would never do any database calls in such a method
yeah.
so another approach is to make a class that would do it?
or idk
usually for me a controller calls a Service (e..g AuthorService) class which would do everything needed for the functionality
I think of calling ToDto() that doesnt maps ids, and then map them manually in a service
a
BookRequest
already feels like a DTOyes, it is
is it appropriate naming?
well is it to create a book, retrieve a book, update a book?
I'd put a verb in front of it
create
CreateBookRequest then :D
ty!
the problem is I may get confused because I already have CreateBookCommand from mediatR 😄
but i guess it is a bad practice to use mediatr objects there
thank you!
wait with doing db calls until your class actually needs them, then you're sending less data through events