Mapster
Mapster copied to clipboard
ProjectToType Expression Generation Customization (Parameter Naming)
I'm using ProjectToType
to generate mapping expressions for both SQL Server projects and a MongoDB project: I haven't run into any issues with expressions generated for SQL Server, but with MongoDB, if my expression contains children collections, the generated expression results in their driver producing a query that throws this error:
Command aggregate failed: '_p1' starts with an invalid character for a user variable name.
I'm assuming p1
is the name of the expression parameter (here maybe? https://github.com/MapsterMapper/Mapster/blob/master/src/Mapster/Adapters/CollectionAdapter.cs#L120). If I don't let Mapster automatically handle the collection and instead do it manually (.Map(i => i.Mapped, i => i.Original.Select(...)
), I don't receive that error.
Being able to control expression parameter naming would be an obscure request, but is there a way I can tap into the expression generation pipeline to add in an expression visitor (to correct the parameters, for example)?
Hi @jayoungers,
Is it possible for you to show me the query where ProjectToType
is used that triggers this error?
@andrerav - Here's the expression that gets generated along with the resulting applicable mongodb query:
.Select(Param_0 => new ItemFacade() {
Id = Param_0.Id.ToString(),
Children = Param_0.Children.Select(Param_1 => new ChildFacade() {ChildId = Param_1.ChildId})
})
{ "$map" : { "input" : "$Children", "as" : "_p1", "in" : { "ChildId" : "$$_p1.ChildId" } } }
And this is the result if I manually map the child collection (naming the parameter i
):
.Select(Param_0 => new ItemFacade() {
Id = Param_0.Id.ToString()
Children = Param_0.Children.Select(i => new ChildFacade() {ChildId = i.ChildId})
{ "$map" : { "input" : "$Children", "as" : "i", "in" : { "ChildId" : "$$i.ChildId" } } }
My assumption is this is potentially an issue with their driver, but I'm hoping if there's an existing hook in mapster to adjust the expression prior to it being compiled, I could rename the params that weren't named
the mongodb driver was updated to handle these parameters