Closed aaron-kruse closed 1 month ago
Hopefully I'm not overstepping any boundaries, we had a need for this so I looked into it then thought I'd share so it could hopefully be included in the official package and others might also find it useful. If you have any feedback or would like any changes, just let me know!
Hi @aaron-kruse,
You might not know, but I have dementia - see this section, which means I rarely change any of my libraries that. So, I'm sorry to say that I won't your pull because I then have to manage your code going forward.
Thanks for the response, and sorry to hear about your diagnosis. No problem at all about the pull request, I completely understand your position on maintaining the library.
I appreciate you taking the time to explain and provide some guidance for going forward, and I wish you all the best.
Add
ICrudServices.MapEntityToDto()
Exposes a new method that can be used to map an existing entity instance to a DTO using the same mapping config that
ReadSingle()
would use.Description
We're using GenericServices in our project to retrieve entities and map them to DTOs, sometimes using
PerDtoConfig
when needed. UsingReadSingle()
andReadManyNoTracked()
work great for most scenarios, but we sometimes run into scenarios where with entities directly. These changes will allow us to continue to use GenericServices to handle mapping an entity to a DTO, even when GenericServices wasn't used to retrieve the data.One example use case is using static factory methods to get back an instance of an entity with default values assigned. Since this instance isn't persisted anywhere, GenericServices can't be used to retrieve and map it (ex. via
ReadSingle()
). These changes make this possible by callingMapEntityToDto
and passing in the entity instance to map.Notes
No promises on code quality or placement 🤫 but it is functional and all tests pass. I started by looking at
ReadSingle()
andProjectFromEntityToDto()
and essentially swapping out theIQueryable
requirement that retrieves an entity with one that is passed in, so I put the new method in the same area. Somewhere in the process I came acrossMapDtoToEntity()
so I thought that was a reasonable place to add a newMapEntityToDto()
.