Dapper Multi Mapping
tundebabzy opened this issue · 2 comments
tundebabzy commented
Thanks for this lovely application.
How does DapperQueryBuilder work with Dapper's multi mapping? Is it supported?
Drizin commented
Our CommandBuilder does not have facades for invoking Dapper Multi-mapper extensions directly, but you can combine CommandBuilder with Multi-mapper methods by explicitly passing CommandBuillder Sql and Parameters:
// DapperQueryBuilder CommandBuilder
var orderAndDetailsQuery = cn
.QueryBuilder($@"
SELECT * FROM Orders AS A
INNER JOIN OrderDetails AS B ON A.OrderID = B.OrderID
/**where**/
ORDER BY A.OrderId, B.OrderDetailId");
// Dynamic Filters
orderAndDetailsQuery.Where($"[ListPrice] <= {maxPrice}");
orderAndDetailsQuery.Where($"[Weight] <= {maxWeight}");
orderAndDetailsQuery.Where($"[Name] LIKE {search}");
// Dapper Multi-mapping extensions
var orderAndDetails = cn.Query<Order, OrderDetail, Order>(
/* orderAndDetailsQuery.Sql contains [ListPrice] <= @p0 AND [Weight] <= @p1 etc... */
sql: orderAndDetailsQuery.Sql,
map: (order, orderDetail) =>
{
// whatever..
},
/* orderAndDetailsQuery.Parameters contains @p0, @p1 etc... */
param: orderAndDetailsQuery.Parameters,
splitOn: "OrderDetailID")
.Distinct()
.ToList();
To sum, instead of using DapperQueryBuilder CommandBuilder.Query<T>
extensions (which invoke Dapper IDbConnection.Query<T>
) you just invoke Dapper multimapper .Query<T1, T2, T3>
directly, and use DapperQueryBuilder only for dynamically building your filters.
tundebabzy commented
Awesome. Thanks