-
|
I’m using Mapperly with EF Core and I’m a bit unsure if I’m doing things the right way. Right now, to avoid circular references, I have this Mapperly mapping: [MapperIgnoreSource(nameof(VehicleModelEntity.Vehicles))]
[MapperIgnoreSource(nameof(VehicleModelEntity.VehicleMake.VehicleModels))]
public static partial VehicleModelDto ToDto(this VehicleModelEntity entity);This works fine for plain mapping. However, my actual mapping also needs to use a blob service to build full image URLs, so I currently do it manually like this: public static VehicleModelDto ToDto(this VehicleModelEntity entity, IBlobStorageService blobService)
{
return new VehicleModelDto
{
Id = entity.Id,
Name = entity.Name,
VehicleMakeId = entity.VehicleMakeId,
VehicleMake = entity.VehicleMake != null ? new VehicleMakeDto
{
Id = entity.VehicleMake.Id,
Name = entity.VehicleMake.Name,
ThumbnailImageUrl = blobService.GetFullUrl(entity.VehicleMake.ThumbnailImageUrl), // This Part
DefaultImageUrl = blobService.GetFullUrl(entity.VehicleMake.DefaultImageUrl), // And This Part
IsActive = entity.VehicleMake.IsActive,
VehicleModels = new List<VehicleModelDto>() // to avoid circular reference
} : null,
VehicleType = entity.VehicleType,
ThumbnailImageUrl = blobService.GetFullUrl(entity.ThumbnailImageUrl),
DefaultImageUrl = blobService.GetFullUrl(entity.DefaultImageUrl),
IsActive = entity.IsActive
};
}What I’m trying to figure out is: What’s the recommended way to mix Mapperly-generated mappings with an external service like |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Ideally, mapping code should be "pure", meaning it takes data in and transforms its shape without side effects, external dependencies, or complex logic. You could use an instance mapper instead of the extension methods based mapper, inject your service and use ignore + after map. |
Beta Was this translation helpful? Give feedback.
Ideally, mapping code should be "pure", meaning it takes data in and transforms its shape without side effects, external dependencies, or complex logic.
You could use an instance mapper instead of the extension methods based mapper, inject your service and use ignore + after map.