-
Notifications
You must be signed in to change notification settings - Fork 10
/
AddCustomerCommand.cs
41 lines (35 loc) · 1.23 KB
/
AddCustomerCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System.Data;
using System.Threading;
using System.Threading.Tasks;
namespace Dapper.UnitOfWork.Example.Data.Commands
{
public class AddCustomerCommand : ICommand, IAsyncCommand
{
private const string Sql = @"
INSERT INTO Customers(
CustomerID,
CompanyName)
VALUES(
@customerId,
@companyName)
";
private readonly CustomerEntity _entity;
// Set this to true prevents invoking the command without an explicit transaction
public bool RequiresTransaction => false;
public AddCustomerCommand(CustomerEntity entity)
=> _entity = entity;
// this is pure Dapper code
public void Execute(IDbConnection connection, IDbTransaction transaction)
=> connection.Execute(Sql, new
{
customerId = _entity.CustomerId,
companyName = _entity.CompanyName
}, transaction);
public Task ExecuteAsync(IDbConnection connection, IDbTransaction transaction, CancellationToken cancellationToken = default)
=> connection.ExecuteAsync(Sql, new
{
customerId = _entity.CustomerId,
companyName = _entity.CompanyName
}, transaction);
}
}