Skip to content

Commit 4b61c06

Browse files
feat: use AutoMapper in Armory
1 parent ddb6033 commit 4b61c06

File tree

55 files changed

+346
-289
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+346
-289
lines changed
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
using System.Threading;
22
using System.Threading.Tasks;
33
using Armory.Shared.Domain.Bus.Query;
4+
using AutoMapper;
45

56
namespace Armory.Armament.Ammunition.Application.Find
67
{
78
public class FindAmmunitionQueryHandler : IQueryHandler<FindAmmunitionQuery, AmmunitionResponse>
89
{
10+
private readonly IMapper _mapper;
911
private readonly AmmunitionFinder _searcher;
1012

11-
public FindAmmunitionQueryHandler(AmmunitionFinder searcher)
13+
public FindAmmunitionQueryHandler(AmmunitionFinder searcher, IMapper mapper)
1214
{
1315
_searcher = searcher;
16+
_mapper = mapper;
1417
}
1518

1619
public async Task<AmmunitionResponse> Handle(FindAmmunitionQuery request, CancellationToken cancellationToken)
1720
{
1821
var ammunition = await _searcher.Find(request.Code);
19-
return ammunition == null ? null : AmmunitionResponse.FromAggregate(ammunition);
22+
return ammunition == null ? null : _mapper.Map<AmmunitionResponse>(ammunition);
2023
}
2124
}
2225
}

src/Armory/Armament/Ammunition/Application/SearchAll/AmmunitionSearcher.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Linq;
32
using System.Threading.Tasks;
43
using Armory.Armament.Ammunition.Domain;
54

@@ -14,10 +13,9 @@ public AmmunitionSearcher(IAmmunitionRepository repository)
1413
_repository = repository;
1514
}
1615

17-
public async Task<IEnumerable<AmmunitionResponse>> SearchAll()
16+
public async Task<IEnumerable<Domain.Ammunition>> SearchAll()
1817
{
19-
var ammunition = await _repository.SearchAll();
20-
return ammunition.Select(AmmunitionResponse.FromAggregate);
18+
return await _repository.SearchAll();
2119
}
2220
}
2321
}

src/Armory/Armament/Ammunition/Application/SearchAll/SearchAllAmmunitionQueryHandler.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@
22
using System.Threading;
33
using System.Threading.Tasks;
44
using Armory.Shared.Domain.Bus.Query;
5+
using AutoMapper;
56

67
namespace Armory.Armament.Ammunition.Application.SearchAll
78
{
89
public class
910
SearchAllAmmunitionQueryHandler : IQueryHandler<SearchAllAmmunitionQuery, IEnumerable<AmmunitionResponse>>
1011
{
12+
private readonly IMapper _mapper;
1113
private readonly AmmunitionSearcher _searcher;
1214

13-
public SearchAllAmmunitionQueryHandler(AmmunitionSearcher searcher)
15+
public SearchAllAmmunitionQueryHandler(AmmunitionSearcher searcher, IMapper mapper)
1416
{
1517
_searcher = searcher;
18+
_mapper = mapper;
1619
}
1720

1821
public async Task<IEnumerable<AmmunitionResponse>> Handle(SearchAllAmmunitionQuery request,
1922
CancellationToken cancellationToken)
2023
{
21-
return await _searcher.SearchAll();
24+
var ammunition = await _searcher.SearchAll();
25+
return _mapper.Map<IEnumerable<AmmunitionResponse>>(ammunition);
2226
}
2327
}
2428
}
Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,11 @@
1-
using Armory.Armament.Equipments.Domain;
2-
31
namespace Armory.Armament.Equipments.Application
42
{
53
public class EquipmentResponse
64
{
7-
public EquipmentResponse(string code, string type, string model, string series, int quantityAvailable)
8-
{
9-
Code = code;
10-
Type = type;
11-
Model = model;
12-
Series = series;
13-
QuantityAvailable = quantityAvailable;
14-
}
15-
16-
public string Code { get; }
17-
public string Type { get; }
18-
public string Model { get; }
19-
public string Series { get; }
20-
public int QuantityAvailable { get; }
21-
22-
public static EquipmentResponse FromAggregate(Equipment equipment)
23-
{
24-
return new EquipmentResponse(equipment.Code, equipment.Type, equipment.Model, equipment.Series,
25-
equipment.QuantityAvailable);
26-
}
5+
public string Code { get; init; }
6+
public string Type { get; init; }
7+
public string Model { get; init; }
8+
public string Series { get; init; }
9+
public int QuantityAvailable { get; init; }
2710
}
2811
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
using System.Threading;
22
using System.Threading.Tasks;
33
using Armory.Shared.Domain.Bus.Query;
4+
using AutoMapper;
45

56
namespace Armory.Armament.Equipments.Application.Find
67
{
78
public class FindEquipmentQueryHandler : IQueryHandler<FindEquipmentQuery, EquipmentResponse>
89
{
10+
private readonly IMapper _mapper;
911
private readonly EquipmentFinder _searcher;
1012

11-
public FindEquipmentQueryHandler(EquipmentFinder searcher)
13+
public FindEquipmentQueryHandler(EquipmentFinder searcher, IMapper mapper)
1214
{
1315
_searcher = searcher;
16+
_mapper = mapper;
1417
}
1518

1619
public async Task<EquipmentResponse> Handle(FindEquipmentQuery request, CancellationToken cancellationToken)
1720
{
1821
var equipment = await _searcher.Find(request.Code);
19-
return equipment == null ? null : EquipmentResponse.FromAggregate(equipment);
22+
return equipment == null ? null : _mapper.Map<EquipmentResponse>(equipment);
2023
}
2124
}
2225
}

src/Armory/Armament/Equipments/Application/SearchAll/AllEquipmentsSearcher.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Linq;
32
using System.Threading.Tasks;
43
using Armory.Armament.Equipments.Domain;
54

@@ -14,10 +13,9 @@ public AllEquipmentsSearcher(IEquipmentsRepository repository)
1413
_repository = repository;
1514
}
1615

17-
public async Task<IEnumerable<EquipmentResponse>> SearchAll()
16+
public async Task<IEnumerable<Equipment>> SearchAll()
1817
{
19-
var equipment = await _repository.SearchAll();
20-
return equipment.Select(EquipmentResponse.FromAggregate);
18+
return await _repository.SearchAll();
2119
}
2220
}
2321
}

src/Armory/Armament/Equipments/Application/SearchAll/SearchAllEquipmentsQueryHandler.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@
22
using System.Threading;
33
using System.Threading.Tasks;
44
using Armory.Shared.Domain.Bus.Query;
5+
using AutoMapper;
56

67
namespace Armory.Armament.Equipments.Application.SearchAll
78
{
89
public class
910
SearchAllEquipmentsQueryHandler : IQueryHandler<SearchAllEquipmentsQuery, IEnumerable<EquipmentResponse>>
1011
{
12+
private readonly IMapper _mapper;
1113
private readonly AllEquipmentsSearcher _searcher;
1214

13-
public SearchAllEquipmentsQueryHandler(AllEquipmentsSearcher searcher)
15+
public SearchAllEquipmentsQueryHandler(AllEquipmentsSearcher searcher, IMapper mapper)
1416
{
1517
_searcher = searcher;
18+
_mapper = mapper;
1619
}
1720

1821
public async Task<IEnumerable<EquipmentResponse>> Handle(SearchAllEquipmentsQuery request,
1922
CancellationToken cancellationToken)
2023
{
21-
return await _searcher.SearchAll();
24+
var equipments = await _searcher.SearchAll();
25+
return _mapper.Map<IEnumerable<EquipmentResponse>>(equipments);
2226
}
2327
}
2428
}
Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,13 @@
1-
using Armory.Armament.Explosives.Domain;
2-
31
namespace Armory.Armament.Explosives.Application
42
{
53
public class ExplosiveResponse
64
{
7-
public ExplosiveResponse(string code, string type, string caliber, string mark, string lot, string series,
8-
int quantityAvailable)
9-
{
10-
Code = code;
11-
Type = type;
12-
Caliber = caliber;
13-
Mark = mark;
14-
Lot = lot;
15-
Series = series;
16-
QuantityAvailable = quantityAvailable;
17-
}
18-
19-
public string Code { get; }
20-
public string Type { get; }
21-
public string Caliber { get; }
22-
public string Mark { get; }
23-
public string Lot { get; }
24-
public string Series { get; }
25-
public int QuantityAvailable { get; }
26-
27-
public static ExplosiveResponse FromAggregate(Explosive explosive)
28-
{
29-
return new ExplosiveResponse(explosive.Code, explosive.Type, explosive.Caliber, explosive.Mark,
30-
explosive.Lot,
31-
explosive.Series, explosive.QuantityAvailable);
32-
}
5+
public string Code { get; init; }
6+
public string Type { get; init; }
7+
public string Caliber { get; init; }
8+
public string Mark { get; init; }
9+
public string Lot { get; init; }
10+
public string Series { get; init; }
11+
public int QuantityAvailable { get; init; }
3312
}
3413
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
using System.Threading;
22
using System.Threading.Tasks;
33
using Armory.Shared.Domain.Bus.Query;
4+
using AutoMapper;
45

56
namespace Armory.Armament.Explosives.Application.Find
67
{
78
public class FindExplosiveQueryHandler : IQueryHandler<FindExplosiveQuery, ExplosiveResponse>
89
{
910
private readonly ExplosiveFinder _finder;
11+
private readonly IMapper _mapper;
1012

11-
public FindExplosiveQueryHandler(ExplosiveFinder finder)
13+
public FindExplosiveQueryHandler(ExplosiveFinder finder, IMapper mapper)
1214
{
1315
_finder = finder;
16+
_mapper = mapper;
1417
}
1518

1619
public async Task<ExplosiveResponse> Handle(FindExplosiveQuery request, CancellationToken cancellationToken)
1720
{
1821
var explosive = await _finder.Find(request.Code);
19-
return explosive == null ? null : ExplosiveResponse.FromAggregate(explosive);
22+
return explosive == null ? null : _mapper.Map<ExplosiveResponse>(explosive);
2023
}
2124
}
2225
}

src/Armory/Armament/Explosives/Application/SearchAll/AllExplosivesSearcher.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Linq;
32
using System.Threading.Tasks;
43
using Armory.Armament.Explosives.Domain;
54

@@ -14,10 +13,9 @@ public AllExplosivesSearcher(IExplosivesRepository repository)
1413
_repository = repository;
1514
}
1615

17-
public async Task<IEnumerable<ExplosiveResponse>> SearchAll()
16+
public async Task<IEnumerable<Explosive>> SearchAll()
1817
{
19-
var explosives = await _repository.SearchAll();
20-
return explosives.Select(ExplosiveResponse.FromAggregate);
18+
return await _repository.SearchAll();
2119
}
2220
}
2321
}

0 commit comments

Comments
 (0)