Skip to content

Commit 58378cd

Browse files
author
Anthony Sneed
committed
Refactor repositories.
1 parent 79be63c commit 58378cd

File tree

13 files changed

+86
-96
lines changed

13 files changed

+86
-96
lines changed

reference-architecture/CustomerService/Controllers/CustomerQueryController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public CustomerQueryController(ICustomerRepository repository, IMapper mapper)
2525
[HttpGet]
2626
public async Task<IActionResult> GetCustomers()
2727
{
28-
var customers = await _repository.Get();
28+
var customers = await _repository.GetAsync();
2929
var result = _mapper.Map<IEnumerable<CustomerView>>(customers);
3030
return Ok(result);
3131
}
@@ -35,7 +35,7 @@ public async Task<IActionResult> GetCustomers()
3535
[Route("{id}")]
3636
public async Task<IActionResult> GetCustomer([FromRoute] Guid id)
3737
{
38-
var customer = await _repository.Get(id);
38+
var customer = await _repository.GetAsync(id);
3939
if (customer == null) return NotFound();
4040
var result = _mapper.Map<CustomerView>(customer);
4141
return Ok(result);

reference-architecture/CustomerService/Domain/CustomerAggregate/CommandHandlers/CustomerCommandHandler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public async Task<CommandResult<Customer>> Handle(CreateCustomer command)
4242
command.Entity.Apply(domainEvent);
4343

4444
// Persist entity
45-
var entity = await _repository.Add(command.Entity);
45+
var entity = await _repository.AddAsync(command.Entity);
4646
if (entity == null) return new CommandResult<Customer>(CommandOutcome.InvalidCommand);
4747
return new CommandResult<Customer>(CommandOutcome.Accepted, entity);
4848
}
@@ -51,14 +51,14 @@ public async Task<CommandResult<Customer>> Handle(UpdateCustomer command)
5151
{
5252
// Compare shipping addresses
5353
_logger.LogInformation("Handling command: {CommandName}", nameof(UpdateCustomer));
54-
var existing = await _repository.Get(command.EntityId);
54+
var existing = await _repository.GetAsync(command.EntityId);
5555
if (existing == null) return new CommandResult<Customer>(CommandOutcome.NotHandled);
5656
var addressChanged = command.Entity.ShippingAddress != existing.ShippingAddress;
5757

5858
try
5959
{
6060
// Persist entity
61-
var entity = await _repository.Update(command.Entity);
61+
var entity = await _repository.UpdateAsync(command.Entity);
6262
if (entity == null) return new CommandResult<Customer>(CommandOutcome.NotFound);
6363

6464
// Publish events
@@ -82,7 +82,7 @@ public async Task<CommandResult<Customer>> Handle(RemoveCustomer command)
8282
{
8383
// Persist entity
8484
_logger.LogInformation("Handling command: {CommandName}", nameof(RemoveCustomer));
85-
await _repository.Remove(command.EntityId);
85+
await _repository.RemoveAsync(command.EntityId);
8686
return new CommandResult<Customer>(CommandOutcome.Accepted);
8787
}
8888
}

reference-architecture/CustomerService/Repositories/CustomerRepository.cs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,45 @@
33
using System.Threading.Tasks;
44
using CustomerService.Domain.CustomerAggregate;
55
using Microsoft.Extensions.Logging;
6+
using MongoDB.Driver;
67
using URF.Core.Abstractions;
8+
using URF.Core.Mongo;
79

810
namespace CustomerService.Repositories
911
{
10-
public class CustomerRepository : ICustomerRepository
12+
public class CustomerRepository : DocumentRepository<Customer>, ICustomerRepository
1113
{
12-
private readonly ILogger<CustomerRepository> _logger;
13-
private readonly IDocumentRepository<Customer> _documentRepository;
14-
15-
public CustomerRepository(
16-
IDocumentRepository<Customer> documentRepository,
17-
ILogger<CustomerRepository> logger)
14+
public CustomerRepository(IMongoCollection<Customer> collection) : base(collection)
1815
{
19-
_documentRepository = documentRepository;
20-
_logger = logger;
2116
}
2217

23-
public async Task<IEnumerable<Customer>> Get() =>
24-
await _documentRepository.FindManyAsync();
18+
public async Task<IEnumerable<Customer>> GetAsync() =>
19+
await FindManyAsync();
2520

26-
public async Task<Customer> Get(Guid id) =>
27-
await _documentRepository.FindOneAsync(e => e.Id == id);
21+
public async Task<Customer> GetAsync(Guid id) =>
22+
await FindOneAsync(e => e.Id == id);
2823

29-
public async Task<Customer> Add(Customer entity)
24+
public async Task<Customer> AddAsync(Customer entity)
3025
{
31-
var existing = await _documentRepository.FindOneAsync(e => e.Id == entity.Id);
26+
var existing = await FindOneAsync(e => e.Id == entity.Id);
3227
if (existing != null) return null;
3328
entity.SequenceNumber = 1;
3429
entity.ETag = Guid.NewGuid().ToString();
35-
return await _documentRepository.InsertOneAsync(entity);
30+
return await InsertOneAsync(entity);
3631
}
3732

38-
public async Task<Customer> Update(Customer entity)
33+
public async Task<Customer> UpdateAsync(Customer entity)
3934
{
40-
var existing = await Get(entity.Id);
35+
var existing = await GetAsync(entity.Id);
4136
if (existing == null) return null;
4237
if (string.Compare(entity.ETag, existing.ETag, StringComparison.OrdinalIgnoreCase) != 0 )
4338
throw new ConcurrencyException();
4439
entity.SequenceNumber = existing.SequenceNumber + 1;
4540
entity.ETag = Guid.NewGuid().ToString();
46-
return await _documentRepository.FindOneAndReplaceAsync(e => e.Id == entity.Id, entity);
41+
return await FindOneAndReplaceAsync(e => e.Id == entity.Id, entity);
4742
}
4843

49-
public async Task<int> Remove(Guid id) =>
50-
await _documentRepository.DeleteOneAsync(e => e.Id == id);
44+
public async Task<int> RemoveAsync(Guid id) =>
45+
await DeleteOneAsync(e => e.Id == id);
5146
}
5247
}

reference-architecture/CustomerService/Repositories/ICustomerRepository.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ namespace CustomerService.Repositories
77
{
88
public interface ICustomerRepository
99
{
10-
Task<IEnumerable<Customer>> Get();
11-
Task<Customer> Get(Guid id);
12-
Task<Customer> Add(Customer entity);
13-
Task<Customer> Update(Customer entity);
14-
Task<int> Remove(Guid id);
10+
Task<IEnumerable<Customer>> GetAsync();
11+
Task<Customer> GetAsync(Guid id);
12+
Task<Customer> AddAsync(Customer entity);
13+
Task<Customer> UpdateAsync(Customer entity);
14+
Task<int> RemoveAsync(Guid id);
1515
}
1616
}

reference-architecture/OrderService/Controllers/OrderQueryController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public OrderQueryController(IOrderRepository repository)
2525
[HttpGet]
2626
public async Task<IActionResult> GetOrders()
2727
{
28-
var orders = await _repository.GetOrders();
28+
var orders = await _repository.GetAsync();
2929
var result = GetOrderViews(orders);
3030
return Ok(result);
3131
}
@@ -35,7 +35,7 @@ public async Task<IActionResult> GetOrders()
3535
[Route("customer/{id}")]
3636
public async Task<IActionResult> GetOrders([FromRoute] Guid id)
3737
{
38-
var orders = await _repository.GetCustomerOrders(id);
38+
var orders = await _repository.GetByCustomerAsync(id);
3939
var result = GetOrderViews(orders);
4040
return Ok(result);
4141
}
@@ -45,7 +45,7 @@ public async Task<IActionResult> GetOrders([FromRoute] Guid id)
4545
[Route("{id}")]
4646
public async Task<IActionResult> GetOrder([FromRoute] Guid id)
4747
{
48-
var order = await _repository.GetOrder(id);
48+
var order = await _repository.GetAsync(id);
4949
var result = GetOrderViews
5050
(Enumerable.Repeat(order, 1)).Single();
5151
return Ok(result);

reference-architecture/OrderService/Domain/OrderAggregate/CommandHandlers/OrderCommandHandler.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public async Task<CommandResult<Order>> Handle(CreateOrder command)
3434
command.Entity.Apply(domainEvent);
3535

3636
// Persist entity
37-
var entity = await _repository.AddOrder(command.Entity);
37+
var entity = await _repository.AddAsync(command.Entity);
3838
if (entity == null) return new CommandResult<Order>(CommandOutcome.InvalidCommand);
3939
return new CommandResult<Order>(CommandOutcome.Accepted, entity);
4040
}
@@ -46,7 +46,7 @@ public async Task<CommandResult<Order>> Handle(UpdateOrder command)
4646
try
4747
{
4848
// Persist entity
49-
var entity = await _repository.UpdateOrder(command.Entity);
49+
var entity = await _repository.UpdateAsync(command.Entity);
5050
if (entity == null) return new CommandResult<Order>(CommandOutcome.NotFound);
5151
return new CommandResult<Order>(CommandOutcome.Accepted, entity);
5252
}
@@ -60,15 +60,15 @@ public async Task<CommandResult<Order>> Handle(RemoveOrder command)
6060
{
6161
// Persist entity
6262
_logger.LogInformation("Handling command: {CommandName}", nameof(RemoveOrder));
63-
await _repository.RemoveOrder(command.EntityId);
63+
await _repository.RemoveAsync(command.EntityId);
6464
return new CommandResult<Order>(CommandOutcome.Accepted);
6565
}
6666

6767
public async Task<CommandResult<Order>> Handle(ShipOrder command)
6868
{
6969
// Process command
7070
_logger.LogInformation("Handling command: {CommandName}", nameof(ShipOrder));
71-
var entity = await _repository.GetOrder(command.EntityId);
71+
var entity = await _repository.GetAsync(command.EntityId);
7272
if (entity == null) return new CommandResult<Order>(CommandOutcome.NotFound);
7373
var domainEvent = entity.Process(command);
7474

@@ -78,7 +78,7 @@ public async Task<CommandResult<Order>> Handle(ShipOrder command)
7878
try
7979
{
8080
// Persist entity
81-
var order = await _repository.UpdateOrderState(entity, OrderState.Shipped);
81+
var order = await _repository.UpdateOrderStateAsync(entity, OrderState.Shipped);
8282
if (order == null) return new CommandResult<Order>(CommandOutcome.NotFound);
8383
return new CommandResult<Order>(CommandOutcome.Accepted, order);
8484
}
@@ -92,7 +92,7 @@ public async Task<CommandResult<Order>> Handle(CancelOrder command)
9292
{
9393
// Process command
9494
_logger.LogInformation("Handling command: {CommandName}", nameof(CancelOrder));
95-
var entity = await _repository.GetOrder(command.EntityId);
95+
var entity = await _repository.GetAsync(command.EntityId);
9696
if (entity == null) return new CommandResult<Order>(CommandOutcome.NotFound);
9797
var domainEvent = entity.Process(command);
9898

@@ -102,7 +102,7 @@ public async Task<CommandResult<Order>> Handle(CancelOrder command)
102102
try
103103
{
104104
// Persist entity
105-
var order = await _repository.UpdateOrderState(entity, OrderState.Cancelled);
105+
var order = await _repository.UpdateOrderStateAsync(entity, OrderState.Cancelled);
106106
if (order == null) return new CommandResult<Order>(CommandOutcome.NotFound);
107107
return new CommandResult<Order>(CommandOutcome.Accepted, order);
108108
}

reference-architecture/OrderService/Integration/EventHandlers/CustomerAddressUpdatedEventHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public CustomerAddressUpdatedEventHandler(IOrderRepository orderRepository,
2626
public override async Task HandleAsync(CustomerAddressUpdated @event)
2727
{
2828
_logger.LogInformation("Handling CustomerAddressUpdated event");
29-
var orders = await _orderRepository.GetCustomerOrders(@event.CustomerId);
29+
var orders = await _orderRepository.GetByCustomerAsync(@event.CustomerId);
3030
foreach (var order in orders)
3131
{
3232
var shippingAddress = _mapper.Map<Address>(@event.ShippingAddress);
33-
await _orderRepository.UpdateOrderAddress(order.Id, shippingAddress);
33+
await _orderRepository.UpdateAddressAsync(order.Id, shippingAddress);
3434
}
3535
}
3636
}

reference-architecture/OrderService/Repositories/IOrderRepository.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ namespace OrderService.Repositories
77
{
88
public interface IOrderRepository
99
{
10-
Task<IEnumerable<Order>> GetOrders();
11-
Task<IEnumerable<Order>> GetCustomerOrders(Guid customerId);
12-
Task<Order> GetOrder(Guid id);
13-
Task<Order> AddOrder(Order entity);
14-
Task<Order> UpdateOrder(Order entity);
15-
Task<Order> UpdateOrderAddress(Guid orderId, Address address);
16-
Task<int> RemoveOrder(Guid id);
17-
Task<Order> UpdateOrderState(Order entity, OrderState orderState);
10+
Task<IEnumerable<Order>> GetAsync();
11+
Task<IEnumerable<Order>> GetByCustomerAsync(Guid customerId);
12+
Task<Order> GetAsync(Guid id);
13+
Task<Order> AddAsync(Order entity);
14+
Task<Order> UpdateAsync(Order entity);
15+
Task<Order> UpdateAddressAsync(Guid orderId, Address address);
16+
Task<int> RemoveAsync(Guid id);
17+
Task<Order> UpdateOrderStateAsync(Order entity, OrderState orderState);
1818
}
1919
}

reference-architecture/OrderService/Repositories/OrderRepository.cs

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,73 +2,68 @@
22
using System.Collections.Generic;
33
using System.Threading.Tasks;
44
using Microsoft.Extensions.Logging;
5+
using MongoDB.Driver;
56
using OrderService.Domain.OrderAggregate;
67
using URF.Core.Abstractions;
8+
using URF.Core.Mongo;
79

810
namespace OrderService.Repositories
911
{
10-
public class OrderRepository : IOrderRepository
12+
public class OrderRepository : DocumentRepository<Order>, IOrderRepository
1113
{
12-
private readonly IDocumentRepository<Order> _documentRepository;
13-
private readonly ILogger<OrderRepository> _logger;
14-
15-
public OrderRepository(
16-
IDocumentRepository<Order> documentRepository,
17-
ILogger<OrderRepository> logger)
14+
public OrderRepository(IMongoCollection<Order> collection) : base(collection)
1815
{
19-
_documentRepository = documentRepository;
20-
_logger = logger;
2116
}
22-
public async Task<IEnumerable<Order>> GetOrders() =>
23-
await _documentRepository.FindManyAsync();
17+
public async Task<IEnumerable<Order>> GetAsync() =>
18+
await FindManyAsync();
2419

25-
public async Task<IEnumerable<Order>> GetCustomerOrders(Guid customerId) =>
26-
await _documentRepository.FindManyAsync(e => e.CustomerId == customerId);
20+
public async Task<IEnumerable<Order>> GetByCustomerAsync(Guid customerId) =>
21+
await FindManyAsync(e => e.CustomerId == customerId);
2722

28-
public async Task<Order> GetOrder(Guid id) =>
29-
await _documentRepository.FindOneAsync(e => e.Id == id);
23+
public async Task<Order> GetAsync(Guid id) =>
24+
await FindOneAsync(e => e.Id == id);
3025

31-
public async Task<Order> AddOrder(Order entity)
26+
public async Task<Order> AddAsync(Order entity)
3227
{
33-
var existing = await _documentRepository.FindOneAsync(e => e.Id == entity.Id);
28+
var existing = await FindOneAsync(e => e.Id == entity.Id);
3429
if (existing != null) return null;
3530
entity.SequenceNumber = 1;
3631
entity.ETag = Guid.NewGuid().ToString();
37-
return await _documentRepository.InsertOneAsync(entity);
32+
return await InsertOneAsync(entity);
3833
}
3934

40-
public async Task<Order> UpdateOrder(Order entity)
35+
public async Task<Order> UpdateAsync(Order entity)
4136
{
42-
var existing = await GetOrder(entity.Id);
37+
var existing = await GetAsync(entity.Id);
4338
if (existing == null) return null;
4439
if (string.Compare(entity.ETag, existing.ETag, StringComparison.OrdinalIgnoreCase) != 0 )
4540
throw new ConcurrencyException();
4641
entity.SequenceNumber = existing.SequenceNumber + 1;
4742
entity.ETag = Guid.NewGuid().ToString();
48-
return await _documentRepository.FindOneAndReplaceAsync(e => e.Id == entity.Id, entity);
43+
return await FindOneAndReplaceAsync(e => e.Id == entity.Id, entity);
4944
}
5045

51-
public async Task<Order> UpdateOrderAddress(Guid orderId, Address address)
46+
public async Task<Order> UpdateAddressAsync(Guid orderId, Address address)
5247
{
53-
var existing = await GetOrder(orderId);
48+
var existing = await GetAsync(orderId);
5449
if (existing == null) return null;
5550
existing.ShippingAddress = address;
56-
return await _documentRepository.FindOneAndReplaceAsync(e => e.Id == orderId, existing);
51+
return await FindOneAndReplaceAsync(e => e.Id == orderId, existing);
5752
}
5853

59-
public async Task<int> RemoveOrder(Guid id) =>
60-
await _documentRepository.DeleteOneAsync(e => e.Id == id);
54+
public async Task<int> RemoveAsync(Guid id) =>
55+
await DeleteOneAsync(e => e.Id == id);
6156

62-
public async Task<Order> UpdateOrderState(Order entity, OrderState orderState)
57+
public async Task<Order> UpdateOrderStateAsync(Order entity, OrderState orderState)
6358
{
64-
var existing = await GetOrder(entity.Id);
59+
var existing = await GetAsync(entity.Id);
6560
if (existing == null) return null;
6661
if (string.Compare(entity.ETag, existing.ETag, StringComparison.OrdinalIgnoreCase) != 0 )
6762
throw new ConcurrencyException();
6863
entity.SequenceNumber++;
6964
entity.ETag = Guid.NewGuid().ToString();
7065
entity.OrderState = orderState;
71-
return await _documentRepository.FindOneAndReplaceAsync(e => e.Id == entity.Id, entity);
66+
return await FindOneAndReplaceAsync(e => e.Id == entity.Id, entity);
7267
}
7368
}
7469
}

test/EventDriven.CQRS.Tests/CustomerCommandControllerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public async Task Remove_Removes_Customer()
7878

7979
// Assert
8080
Assert.IsType<NoContentResult>(actionResult);
81-
var value = await repository.Get(customer.Id);
81+
var value = await repository.GetAsync(customer.Id);
8282
Assert.Null(value);
8383
}
8484
}

0 commit comments

Comments
 (0)