-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuerySample.cs
45 lines (37 loc) · 1.5 KB
/
QuerySample.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
42
43
44
45
using System;
using System.Linq;
using System.Threading.Tasks;
using CoreDdd.Nhibernate.Repositories;
using CoreDdd.Nhibernate.UnitOfWorks;
using CoreDddSampleConsoleApp.Domain;
using CoreDddSampleConsoleApp.Dtos;
namespace CoreDddSampleConsoleApp.Samples.Query
{
public class QuerySample
{
public async Task QueryShipsByName(CoreDddSampleNhibernateConfigurator nhibernateConfigurator)
{
using (var unitOfWork = new NhibernateUnitOfWork(nhibernateConfigurator))
{
unitOfWork.BeginTransaction();
var shipRepository = new NhibernateRepository<Ship>(unitOfWork);
try
{
var ship = new Ship("lady starlight", tonnage: 10m);
await shipRepository.SaveAsync(ship);
unitOfWork.Flush();
var getShipByNameQuery = new GetShipsByNameQuery {ShipName = "lady"};
var getShipByNameQueryHandler = new GetShipsByNameQueryHandler(unitOfWork);
var shipDtos = await getShipByNameQueryHandler.ExecuteAsync<ShipDto>(getShipByNameQuery);
Console.WriteLine($"Ship by name query was executed. Number of ships queried: {shipDtos.Count()}");
await unitOfWork.CommitAsync();
}
catch
{
await unitOfWork.RollbackAsync();
throw;
}
}
}
}
}