TLDR: Expression in Mapster config is invoked immediately, Is there any right way to invoke the executing the query?
I am using mapster to project the efcore entity to a dto in my project.
Originally I was doing this on my request handler which can do the job correctly, but according to mapster I should not be doing this due to performance issues. TypeAdapterConfig should only be defined in the code entry only once.
// original code
TypeAdapterConfig<ProductEntity, ProductDto>.NewConfig()
.Map(d => d.Available, ProductExpression.AvailableForStoreId(request.ForStoreId));
public static Expression<Func<ProductEntity, bool>> AvailableForStoreId(DefaultIdType storeId)
{
return s => s.ProductGroups.Any(gp => gp.storeRelProductGroup.Any(r => r.StoreId== storeId));
}
for the reason above, I changed the code to follow the approach of using MapContext.Current.Parameters["storeId"] from MapsterMapper
and put the config in the code entry.
// Parameters approach
TypeAdapterConfig<ProductEntity, ProductDto>.NewConfig()
.Map(d => d.HasPricing, ProductExpression.AvailableForStoreId((int)MapContext.Current.Parameters["storeId"]));
But the problem is when I start the project, null reference error is thrown immediately for MapContext.Current as it's null.
Seems the expression is invoked on the TypeAdapterConfig being defined, so is there any way to fix the problem or what is the right way to keep AvailableForStoreId logic as an expression and supply the dynamic parameter(storeId) per request? thank you.