Closed TimBandelin closed 1 year ago
@TimBandelin For mapping scenarios like this you may want to consider using mapping configuration with rule based settings as described here: https://github.com/MapsterMapper/Mapster/wiki/Configuration#rule-based-settings. Can you take a look and see if this solves your problem?
No feedback, assuming this is solved. Please let me know if this is still an issue with the latest version.
So I was working with Mapster on an API that communicates with a database using EF Core. I have a class that looks something like this:
The connections to the database are managed in a class library, and because I don't want to have my database objects flying around somewhere outside, I have an additial class for communication:
The API (ASP.NET) receives and gives instances of
MyObject
when communicating with the database library. But because these are implementation detail, the API send/receives special DTOs when communication with the frontend, similar to this:Now, to map between these I use Mapster. There is obviously no problem with mapping between
MyObject
andMyObjectEntity,
but when I want to map fromMyObjectDto
toMyObject
and the other way around, I can apply the AdaptMember attribute to the properties. But when I putAdaptMember("DifferentName")
to theName
-property ofMyObject,
the value turns intonull
whenn mappingMyObject
toMyObjectEntity.
The attribute shouldn't override the mapping entirely, so it is possible to map
MyObject
to bothMyObjectEntity
andMyObjectDto,
only using the AdaptMember attribute when it can't find a property called "Name".