Queryable projections: Inline user-implemented mapping expressions
Is your feature request related to a problem? Please describe. I am trying to map a complex property in my projection, which can't be modified before/after the mapping due to the nature of how IQueryables work.
Describe the solution you'd like Example: if I had a car types in the database, something like below. I know you can add logic before and after the mapping, but that is more complicated with projections, as you can't modify with a select with essentially mapping twice. This is what I'm essentially trying to do:
public class Car
{
public double Weight { get; set; }
public ModelT ModelT {get;set;}
public ModelB ModelB {get;set;}
}
public class CarDto
{
public double Mass { get; set; }
public string CarType {get;set;}
}
[Mapper]
public partial class CarMapper
{
public partial IQueryable<CarDto> Project(IQueryable<Car> query);
[MapProperty(nameof(@Car.Weight), nameof(@CarDto.Mass))]
[MapProperty("MethodNameToComputeCarType", nameof(@CarDto.CarType))]
public partial CarDto Dto(Car dto);
public string MethodNameToComputeCarType(Car c) => c.ModelT != null ? "ModelT" : "ModelB";
}
//Desired Result...
public partial global::System.Linq.IQueryable<global::Moonshot.Agency.User.Mappers.CarDto> Project(global::System.Linq.IQueryable<global::Moonshot.Agency.User.Mappers.Car> query)
{
#nullable disable
return System.Linq.Queryable.Select(query, x => new global::Moonshot.Agency.User.Mappers.CarDto()
{
Mass = x.Weight,
CarType = x.ModelT != null ? "ModelT" : "ModelB",
});
#nullable enable
}
Describe alternatives you've considered An alternative solution could be adding a computed property to the EFCore entity (Car), which should work, but that has the disadvantage of bloating the entity when it's not needed for every query.
Another solution is to simply do this mapping in memory, which in some cases would reduce performance unnecessarily.
Hi, happy new year! Any updates on this?
No updates. Feel free to contribute 😉
This is dependent on https://github.com/riok/mapperly/issues/783.
https://github.com/riok/mapperly/issues/783 handles the ability to map specific properties with a specific user-implemented mapping. This should soon land on main.
This issue focuses on the inlining of user-implemented mapping expressions to allow better translations by EF Core database providers.
3.5.0-next.3 tries to inline all expression, see the updated docs. Feedback is welcome.