Skip to content

Commit ad8e5a7

Browse files
author
Anthony Sneed
committed
Use EventDriven.CQRS.Extensions.
1 parent 4fb79ed commit ad8e5a7

File tree

29 files changed

+240
-462
lines changed

29 files changed

+240
-462
lines changed
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
1-
using Common.Helpers;
1+
using EventDriven.CQRS.Abstractions.Behaviors;
2+
using EventDriven.CQRS.Extensions;
23
using MediatR;
34
using Microsoft.Extensions.Logging;
45

56
namespace Common.Behaviors;
67

7-
public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
8+
public class LoggingBehavior<TRequest, TResponse> : IBehavior<TRequest, TResponse>
89
where TRequest : IRequest<TResponse>
910
{
1011
private readonly ILogger<LoggingBehavior<TRequest, TResponse>> _logger;
1112

1213
public LoggingBehavior(
1314
ILogger<LoggingBehavior<TRequest, TResponse>> logger)
1415
{
15-
_logger = logger;
16+
_logger = logger;
1617
}
1718

18-
public async Task<TResponse> Handle(
19-
TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
19+
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken,
20+
RequestHandlerDelegate<TResponse> next)
2021
{
21-
_logger.LogInformation("----- Handling command {CommandName}. Request: {@Command}",
22-
request.GetGenericTypeName(), request);
22+
string requestType = string.Empty;
23+
if (typeof(TRequest).IsCommandType())
24+
requestType = "command";
25+
else if (typeof(TRequest).IsQueryType())
26+
requestType = "query";
27+
_logger.LogInformation("----- Handling {RequestType} '{CommandName}'. Request: {@Request}",
28+
requestType, request.GetGenericTypeName(), request);
2329
var response = await next();
24-
_logger.LogInformation("----- Handled Command {CommandName}. Response: {@Response}",
25-
request.GetGenericTypeName(), response);
26-
30+
_logger.LogInformation("----- Handled {RequestType} '{CommandName}'. Response: {@Response}",
31+
requestType, request.GetGenericTypeName(), response);
2732
return response;
2833
}
2934
}

reference-architecture/Common/Common.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="EventDriven.CQRS.Extensions" Version="2.0.0" />
1011
<PackageReference Include="EventDriven.EventBus.Abstractions" Version="1.2.0" />
11-
<PackageReference Include="MediatR" Version="10.0.1" />
1212
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />
1313
</ItemGroup>
1414

reference-architecture/Common/Helpers/GenericTypeExtensions.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
using EventDriven.DependencyInjection.URF.Mongo;
22

3-
namespace CustomerService.Configuration
3+
namespace CustomerService.Configuration;
4+
5+
public class CustomerDatabaseSettings : IMongoDbSettings
46
{
5-
public class CustomerDatabaseSettings : IMongoDbSettings
6-
{
7-
public string ConnectionString { get; set; } = null!;
8-
public string DatabaseName { get; set; } = null!;
9-
public string CollectionName { get; set; } = null!;
10-
}
7+
public string ConnectionString { get; set; } = null!;
8+
public string DatabaseName { get; set; } = null!;
9+
public string CollectionName { get; set; } = null!;
1110
}
Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,61 @@
11
using AutoMapper;
22
using CustomerService.Domain.CustomerAggregate;
33
using CustomerService.Domain.CustomerAggregate.Commands;
4-
using CustomerService.Helpers;
54
using EventDriven.CQRS.Abstractions.Commands;
5+
using EventDriven.CQRS.Extensions;
66
using Microsoft.AspNetCore.Mvc;
77

8-
namespace CustomerService.Controllers
8+
namespace CustomerService.Controllers;
9+
10+
[Route("api/customer")]
11+
[ApiController]
12+
public class CustomerCommandController : ControllerBase
913
{
10-
[Route("api/customer")]
11-
[ApiController]
12-
public class CustomerCommandController : ControllerBase
14+
private readonly ICommandBroker _commandBroker;
15+
private readonly IMapper _mapper;
16+
17+
public CustomerCommandController(
18+
ICommandBroker commandBroker,
19+
IMapper mapper)
20+
{
21+
_commandBroker = commandBroker;
22+
_mapper = mapper;
23+
}
24+
25+
// POST api/customer
26+
[HttpPost]
27+
public async Task<IActionResult> Create([FromBody] DTO.Write.Customer customerDto)
28+
{
29+
var customerIn = _mapper.Map<Customer>(customerDto);
30+
var result = await _commandBroker.SendAsync(new CreateCustomer(customerIn));
31+
32+
if (result.Outcome != CommandOutcome.Accepted)
33+
return result.ToActionResult();
34+
var customerOut = _mapper.Map<DTO.Write.Customer>(result.Entity);
35+
return new CreatedResult($"api/customer/{customerOut.Id}", customerOut);
36+
}
37+
38+
// PUT api/customer
39+
[HttpPut]
40+
public async Task<IActionResult> Update([FromBody] DTO.Write.Customer customerDto)
41+
{
42+
var customerIn = _mapper.Map<Customer>(customerDto);
43+
var result = await _commandBroker.SendAsync(new UpdateCustomer(customerIn));
44+
45+
if (result.Outcome != CommandOutcome.Accepted)
46+
return result.ToActionResult();
47+
var customerOut = _mapper.Map<DTO.Write.Customer>(result.Entity);
48+
return result.ToActionResult(customerOut);
49+
}
50+
51+
// DELETE api/customer/id
52+
[HttpDelete]
53+
[Route("{id}")]
54+
public async Task<IActionResult> Remove([FromRoute] Guid id)
1355
{
14-
private readonly ICommandBroker _commandBroker;
15-
private readonly IMapper _mapper;
16-
17-
public CustomerCommandController(
18-
ICommandBroker commandBroker,
19-
IMapper mapper)
20-
{
21-
_commandBroker = commandBroker;
22-
_mapper = mapper;
23-
}
24-
25-
// POST api/customer
26-
[HttpPost]
27-
public async Task<IActionResult> Create([FromBody] DTO.Write.Customer customerDto)
28-
{
29-
var customerIn = _mapper.Map<Customer>(customerDto);
30-
var result = await _commandBroker.SendAsync(new CreateCustomer(customerIn));
31-
32-
if (result.Outcome != CommandOutcome.Accepted)
33-
return result.ToActionResult();
34-
var customerOut = _mapper.Map<DTO.Write.Customer>(result.Entity);
35-
return new CreatedResult($"api/customer/{customerOut.Id}", customerOut);
36-
}
37-
38-
// PUT api/customer
39-
[HttpPut]
40-
public async Task<IActionResult> Update([FromBody] DTO.Write.Customer customerDto)
41-
{
42-
var customerIn = _mapper.Map<Customer>(customerDto);
43-
var result = await _commandBroker.SendAsync(new UpdateCustomer(customerIn));
44-
45-
if (result.Outcome != CommandOutcome.Accepted)
46-
return result.ToActionResult();
47-
var customerOut = _mapper.Map<DTO.Write.Customer>(result.Entity);
48-
return result.ToActionResult(customerOut);
49-
}
50-
51-
// DELETE api/customer/id
52-
[HttpDelete]
53-
[Route("{id}")]
54-
public async Task<IActionResult> Remove([FromRoute] Guid id)
55-
{
56-
var result = await _commandBroker.SendAsync(new RemoveCustomer(id));
57-
return result.Outcome != CommandOutcome.Accepted
58-
? result.ToActionResult()
59-
: new NoContentResult();
60-
}
56+
var result = await _commandBroker.SendAsync(new RemoveCustomer(id));
57+
return result.Outcome != CommandOutcome.Accepted
58+
? result.ToActionResult()
59+
: new NoContentResult();
6160
}
62-
}
61+
}

reference-architecture/CustomerService/Controllers/CustomerQueryController.cs

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,40 @@
44
using EventDriven.CQRS.Abstractions.Queries;
55
using Microsoft.AspNetCore.Mvc;
66

7-
namespace CustomerService.Controllers
7+
namespace CustomerService.Controllers;
8+
9+
[Route("api/customer")]
10+
[ApiController]
11+
public class CustomerQueryController : ControllerBase
812
{
9-
[Route("api/customer")]
10-
[ApiController]
11-
public class CustomerQueryController : ControllerBase
12-
{
13-
private readonly IQueryBroker _queryBroker;
14-
private readonly IMapper _mapper;
13+
private readonly IQueryBroker _queryBroker;
14+
private readonly IMapper _mapper;
1515

16-
public CustomerQueryController(
17-
IQueryBroker queryBroker,
18-
IMapper mapper)
19-
{
20-
_queryBroker = queryBroker;
21-
_mapper = mapper;
22-
}
16+
public CustomerQueryController(
17+
IQueryBroker queryBroker,
18+
IMapper mapper)
19+
{
20+
_queryBroker = queryBroker;
21+
_mapper = mapper;
22+
}
2323

24-
// GET api/customer
25-
[HttpGet]
26-
public async Task<IActionResult> GetCustomers()
27-
{
28-
var customers = await _queryBroker.SendAsync(new GetCustomers());
29-
var result = _mapper.Map<IEnumerable<CustomerView>>(customers);
30-
return Ok(result);
31-
}
24+
// GET api/customer
25+
[HttpGet]
26+
public async Task<IActionResult> GetCustomers()
27+
{
28+
var customers = await _queryBroker.SendAsync(new GetCustomers());
29+
var result = _mapper.Map<IEnumerable<CustomerView>>(customers);
30+
return Ok(result);
31+
}
3232

33-
// GET api/customer/id
34-
[HttpGet]
35-
[Route("{id:guid}")]
36-
public async Task<IActionResult> GetCustomer([FromRoute] Guid id)
37-
{
38-
var customer = await _queryBroker.SendAsync(new GetCustomer(id));
39-
if (customer == null) return NotFound();
40-
var result = _mapper.Map<CustomerView>(customer);
41-
return Ok(result);
42-
}
33+
// GET api/customer/id
34+
[HttpGet]
35+
[Route("{id:guid}")]
36+
public async Task<IActionResult> GetCustomer([FromRoute] Guid id)
37+
{
38+
var customer = await _queryBroker.SendAsync(new GetCustomer(id));
39+
if (customer == null) return NotFound();
40+
var result = _mapper.Map<CustomerView>(customer);
41+
return Ok(result);
4342
}
44-
}
43+
}

reference-architecture/CustomerService/CustomerService.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
<ItemGroup>
1010
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
11-
<PackageReference Include="EventDriven.CQRS.Abstractions" Version="2.0.0-beta3" />
11+
<PackageReference Include="EventDriven.CQRS.Abstractions" Version="2.0.0" />
12+
<PackageReference Include="EventDriven.CQRS.Extensions" Version="2.0.0" />
1213
<PackageReference Include="EventDriven.DependencyInjection.URF.Mongo" Version="1.1.0" />
1314
<PackageReference Include="EventDriven.EventBus.Dapr" Version="1.2.0" />
1415
<PackageReference Include="EventDriven.EventBus.Dapr.EventCache.Mongo" Version="1.2.0" />
Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
namespace CustomerService.DTO.Read
1+
namespace CustomerService.DTO.Read;
2+
3+
public class CustomerView
24
{
3-
public class CustomerView
4-
{
5-
public Guid Id { get; set; }
6-
public string FirstName { get; set; } = null!;
7-
public string LastName { get; set; } = null!;
8-
public string Street { get; set; } = null!;
9-
public string City { get; set; } = null!;
10-
public string State { get; set; } = null!;
11-
public string Country { get; set; } = null!;
12-
public string PostalCode { get; set; } = null!;
13-
public string? ETag { get; set; }
14-
}
5+
public Guid Id { get; set; }
6+
public string FirstName { get; set; } = null!;
7+
public string LastName { get; set; } = null!;
8+
public string Street { get; set; } = null!;
9+
public string City { get; set; } = null!;
10+
public string State { get; set; } = null!;
11+
public string Country { get; set; } = null!;
12+
public string PostalCode { get; set; } = null!;
13+
public string? ETag { get; set; }
1514
}
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
namespace CustomerService.DTO.Write
1+
namespace CustomerService.DTO.Write;
2+
3+
public class Address
24
{
3-
public class Address
4-
{
5-
public string Street { get; set; } = null!;
6-
public string City { get; set; } = null!;
7-
public string State { get; set; } = null!;
8-
public string Country { get; set; } = null!;
9-
public string PostalCode { get; set; } = null!;
10-
}
5+
public string Street { get; set; } = null!;
6+
public string City { get; set; } = null!;
7+
public string State { get; set; } = null!;
8+
public string Country { get; set; } = null!;
9+
public string PostalCode { get; set; } = null!;
1110
}
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
namespace CustomerService.DTO.Write
1+
namespace CustomerService.DTO.Write;
2+
3+
public class Customer
24
{
3-
public class Customer
4-
{
5-
public Guid Id { get; set; }
6-
public string FirstName { get; set; } = null!;
7-
public string LastName { get; set; } = null!;
8-
public Address ShippingAddress { get; set; } = null!;
9-
public string ETag { get; set; } = null!;
10-
}
5+
public Guid Id { get; set; }
6+
public string FirstName { get; set; } = null!;
7+
public string LastName { get; set; } = null!;
8+
public Address ShippingAddress { get; set; } = null!;
9+
public string ETag { get; set; } = null!;
1110
}

0 commit comments

Comments
 (0)