Created
April 30, 2018 21:50
-
-
Save andre-f-paggi/d5f88a93279d60323f44c3c7d3802ffd to your computer and use it in GitHub Desktop.
MultiMapping Dapper + AutoMapper - .Net Core
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public CompraParaAprovarPagamentoDTO ObterCompraParaAprovarPagamentoDTO(int idCompra) | |
{ | |
using (SqlConnection conexao = new SqlConnection(_config.GetConnectionString("DbLoja"))) | |
{ | |
var sql = $@" | |
select | |
'IdCompra' = Compra.IdCompra, | |
'Pagamentos_Sequencial' = CompraFormaPagamento.Sequencial, | |
'Pagamentos_ValorJuros' = (select sum(CompraFormaPagamento.ValorJuros) from CompraFormaPagamento CFP where CFP.IdCompraFormaPagamento = CompraFormaPagamento.IdCompraFormaPagamento), | |
'Pagamentos_ValorMeioPagamento' = (select sum(CompraFormaPagamento.ValorComJuros) from CompraFormaPagamento CFP where CFP.IdCompraFormaPagamento = CompraFormaPagamento.IdCompraFormaPagamento), | |
'Pagamentos_ValorPago' = (select sum(CompraFormaPagamento.ValorComJuros) from CompraFormaPagamento CFP where CFP.IdCompraFormaPagamento = CompraFormaPagamento.IdCompraFormaPagamento), | |
'Skus_IdSku' = CompraEntregaSku.IdSku, | |
'Skus_Quantidade' = sum(CompraEntregaSku.Sequencial), | |
'Skus_ValorUnitario' = CompraEntregaSku.ValorVendaUnidade | |
from | |
Compra (nolock) | |
inner join CompraFormaPagamento (nolock) | |
on CompraFormaPagamento.IdCompra = Compra.IdCompra | |
inner join FormaPagamento (nolock) | |
on FormaPagamento.IdformaPagamento = CompraFormaPagamento.IdFormaPagamento | |
inner join CompraFormaPagamentoStatus (nolock) | |
on CompraFormaPagamentoStatus.IdCompraFormaPagamentoStatus = CompraFormaPagamento.IdCompraFormaPagamentoStatus | |
inner join CompraEntregaSku (nolock) | |
on CompraEntregaSku.IdCompraEntrega IN ( | |
select | |
IdCompraEntrega | |
from | |
CompraEntrega nolock | |
where | |
IdCompra = Compra.IdCompra) | |
where | |
Compra.IdCompra = @IdCompra | |
group by | |
Compra.IdCompra, | |
Compra._ValorTotalComDesconto, | |
CompraEntregaSku.IdSku, | |
CompraFormaPagamento.IdCompraFormaPagamento, | |
CompraFormaPagamento.Sequencial, | |
CompraEntregaSku.ValorVendaUnidade | |
"; | |
// Step 1: Use Dapper to return the flat result as a Dynamic. | |
dynamic queryResults = conexao.Query<dynamic>(sql, new { IdCompra = idCompra }); | |
// Step 2: Use Slapper.Automapper for mapping to the POCO Entities. | |
// - IMPORTANT: Let Slapper.Automapper know how to do the mapping; | |
// let it know the primary key for each POCO. | |
// - Must also use underscore notation ("_") to name parameters; | |
// see Slapper.Automapper docs. | |
AutoMapper.Configuration.AddIdentifier( | |
typeof(CompraParaAprovarPagamentoDTO), "IdCompra"); | |
AutoMapper.Configuration.AddIdentifier( | |
typeof(CompraParaAprovarPagamentoDTO.Sku), "IdSku"); | |
var compra = (AutoMapper.MapDynamic<CompraParaAprovarPagamentoDTO>(queryResults) as IEnumerable<CompraParaAprovarPagamentoDTO>).First(); | |
return compra; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment