Skip to content

Commit 1591a69

Browse files
author
Anthony Sneed
committed
Upgrade DDD commands, simplify DI registrations.
1 parent 9d646d8 commit 1591a69

File tree

18 files changed

+43
-92
lines changed

18 files changed

+43
-92
lines changed

EventDriven.CQRS.sln

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ EndProject
1111
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{269CD137-4093-4100-B33E-808586D335F6}"
1212
EndProject
1313
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "reference-architecture", "reference-architecture", "{C4FD0AF1-927A-4860-A634-7CE342807692}"
14+
ProjectSection(SolutionItems) = preProject
15+
reference-architecture\dapr\components\pubsub.yaml = reference-architecture\dapr\components\pubsub.yaml
16+
reference-architecture\dapr\components\snssqs-pubsub.yaml = reference-architecture\dapr\components\snssqs-pubsub.yaml
17+
EndProjectSection
1418
EndProject
1519
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EventDriven.CQRS.Tests", "test\EventDriven.CQRS.Tests\EventDriven.CQRS.Tests.csproj", "{9809006C-2F6B-44A1-8AE2-BC449368D209}"
1620
EndProject
@@ -66,8 +70,8 @@ Global
6670
{16A5B2CB-8C46-4F3E-B7A1-97C47D9F66E7} = {C4FD0AF1-927A-4860-A634-7CE342807692}
6771
{B11B21E0-7B89-4285-990A-D98793310B02} = {C4FD0AF1-927A-4860-A634-7CE342807692}
6872
{FC04D111-903D-49FF-84A6-8806C71E2168} = {C4FD0AF1-927A-4860-A634-7CE342807692}
69-
{F0E48E00-7D72-4614-9C13-90A7B015B06F} = {AFFCBFA4-9D64-43AA-AC59-D4CC54BD9C72}
7073
{00BA9501-787E-465C-97D0-F51295D97802} = {F0E48E00-7D72-4614-9C13-90A7B015B06F}
74+
{F0E48E00-7D72-4614-9C13-90A7B015B06F} = {C4FD0AF1-927A-4860-A634-7CE342807692}
7175
EndGlobalSection
7276
GlobalSection(ExtensibilityGlobals) = postSolution
7377
SolutionGuid = {427A0D03-63CA-48AE-AA95-D21800101398}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
using EventDriven.DependencyInjection.URF.Mongo;
2+
13
namespace CustomerService.Configuration
24
{
3-
public class CustomerDatabaseSettings
5+
public class CustomerDatabaseSettings : IMongoDbSettings
46
{
5-
public string CustomersCollectionName { get; set; }
67
public string ConnectionString { get; set; }
78
public string DatabaseName { get; set; }
9+
public string CollectionName { get; set; }
810
}
911
}

reference-architecture/CustomerService/CustomerService.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
<ItemGroup>
88
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
9-
<PackageReference Include="EventDriven.DDD.Abstractions" Version="1.1.0-beta1" />
10-
<PackageReference Include="EventDriven.EventBus.Dapr" Version="1.1.0" />
9+
<PackageReference Include="EventDriven.DDD.Abstractions" Version="1.1.0-beta2" />
10+
<PackageReference Include="EventDriven.DependencyInjection.URF.Mongo" Version="1.1.0-beta3" />
11+
<PackageReference Include="EventDriven.EventBus.Dapr" Version="1.2.0-beta1" />
1112
<PackageReference Include="MongoDB.Driver" Version="2.14.1" />
1213
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
1314
<PackageReference Include="URF.Core.Mongo" Version="3.1.3" />

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ public async Task<CommandResult<Customer>> Handle(CreateCustomer command)
3838
{
3939
// Process command
4040
_logger.LogInformation("Handling command: {CommandName}", nameof(CreateCustomer));
41-
var events = command.Customer.Process(command);
41+
var events = command.Entity.Process(command);
4242

4343
// Apply events
4444
var domainEvent = events.OfType<CustomerCreated>().SingleOrDefault();
4545
if (domainEvent == null) return new CommandResult<Customer>(CommandOutcome.NotHandled);
46-
command.Customer.Apply(domainEvent);
46+
command.Entity.Apply(domainEvent);
4747

4848
// Persist entity
49-
var entity = await _repository.Add(command.Customer);
49+
var entity = await _repository.Add(command.Entity);
5050
if (entity == null) return new CommandResult<Customer>(CommandOutcome.InvalidCommand);
5151
return new CommandResult<Customer>(CommandOutcome.Accepted, entity);
5252
}
@@ -56,12 +56,12 @@ public async Task<CommandResult<Customer>> Handle(UpdateCustomer command)
5656
// Compare shipping addresses
5757
_logger.LogInformation("Handling command: {CommandName}", nameof(UpdateCustomer));
5858
var existing = await _repository.Get(command.EntityId);
59-
var addressChanged = command.Customer.ShippingAddress != existing.ShippingAddress;
59+
var addressChanged = command.Entity.ShippingAddress != existing.ShippingAddress;
6060

6161
try
6262
{
6363
// Persist entity
64-
var entity = await _repository.Update(command.Customer);
64+
var entity = await _repository.Update(command.Entity);
6565
if (entity == null) return new CommandResult<Customer>(CommandOutcome.NotFound);
6666

6767
// Publish events

reference-architecture/CustomerService/Domain/CustomerAggregate/Commands/CreateCustomer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
namespace CustomerService.Domain.CustomerAggregate.Commands
44
{
5-
public record CreateCustomer(Customer Customer) : Command(Customer.Id);
5+
public record CreateCustomer(Customer Entity) : Command<Customer>(Entity);
66
}

reference-architecture/CustomerService/Domain/CustomerAggregate/Commands/UpdateCustomer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
namespace CustomerService.Domain.CustomerAggregate.Commands
44
{
5-
public record UpdateCustomer(Customer Customer) : Command(Customer.Id);
5+
public record UpdateCustomer(Customer Entity) : Command<Customer>(Entity);
66
}

reference-architecture/CustomerService/Domain/CustomerAggregate/Customer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public IEnumerable<IDomainEvent> Process(CreateCustomer command)
2121
// To process command, return one or more domain events
2222
=> new List<IDomainEvent>
2323
{
24-
new CustomerCreated(command.Customer)
24+
new CustomerCreated(command.Entity)
2525
};
2626

2727
public void Apply(CustomerCreated domainEvent) =>

reference-architecture/CustomerService/Startup.cs

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@
22
using CustomerService.Domain.CustomerAggregate;
33
using CustomerService.Domain.CustomerAggregate.CommandHandlers;
44
using CustomerService.Repositories;
5-
using EventDriven.EventBus.Dapr;
65
using Microsoft.AspNetCore.Builder;
76
using Microsoft.AspNetCore.Hosting;
87
using Microsoft.Extensions.Configuration;
98
using Microsoft.Extensions.DependencyInjection;
109
using Microsoft.Extensions.Hosting;
11-
using Microsoft.Extensions.Options;
1210
using Microsoft.OpenApi.Models;
13-
using MongoDB.Driver;
14-
using URF.Core.Abstractions;
15-
using URF.Core.Mongo;
11+
using EventDriven.DependencyInjection.URF.Mongo;
1612

1713
namespace CustomerService
1814
{
@@ -34,40 +30,14 @@ public void ConfigureServices(IServiceCollection services)
3430
c.SwaggerDoc("v1", new OpenApiInfo { Title = "CustomerService", Version = "v1" });
3531
});
3632

37-
// Configuration
38-
services.Configure<CustomerDatabaseSettings>(
39-
Configuration.GetSection(nameof(CustomerDatabaseSettings)));
40-
services.AddSingleton(sp =>
41-
sp.GetRequiredService<IOptions<CustomerDatabaseSettings>>().Value);
42-
4333
// Registrations
4434
services.AddAutoMapper(typeof(Startup));
4535
services.AddSingleton<CustomerCommandHandler>();
46-
services.AddSingleton(sp =>
47-
{
48-
var settings = sp.GetRequiredService<CustomerDatabaseSettings>();
49-
var client = new MongoClient(settings.ConnectionString);
50-
var database = client.GetDatabase(settings.DatabaseName);
51-
return database.GetCollection<Customer>(settings.CustomersCollectionName);
52-
});
53-
services.AddSingleton<IDocumentRepository<Customer>, DocumentRepository<Customer>>();
5436
services.AddSingleton<ICustomerRepository, CustomerRepository>();
55-
56-
// Configuration
57-
var eventBusOptions = new DaprEventBusOptions();
58-
Configuration.GetSection(nameof(DaprEventBusOptions)).Bind(eventBusOptions);
59-
var eventBusSchemaOptions = new DaprEventBusSchemaOptions();
60-
Configuration.GetSection(nameof(DaprEventBusSchemaOptions)).Bind(eventBusSchemaOptions);
37+
services.AddMongoDbSettings<CustomerDatabaseSettings, Customer>(Configuration);
6138

6239
// Add Dapr event bus
63-
services.AddDaprEventBus(eventBusOptions.PubSubName, options =>
64-
{
65-
options.UseSchemaRegistry = eventBusSchemaOptions.UseSchemaRegistry;
66-
options.SchemaRegistryType = eventBusSchemaOptions.SchemaRegistryType;
67-
options.MongoStateStoreOptions = eventBusSchemaOptions.MongoStateStoreOptions;
68-
options.SchemaValidatorType = eventBusSchemaOptions.SchemaValidatorType;
69-
options.AddSchemaOnPublish = eventBusSchemaOptions.AddSchemaOnPublish;
70-
});
40+
services.AddDaprEventBus(Configuration, true);
7141
}
7242

7343
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

reference-architecture/CustomerService/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"CustomerDatabaseSettings": {
1111
"ConnectionString": "mongodb://localhost:27017",
1212
"DatabaseName": "CustomersDb",
13-
"CustomersCollectionName": "Customers"
13+
"CollectionName": "Customers"
1414
},
1515
"DaprEventBusOptions": {
1616
"PubSubName": "pubsub"
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
using EventDriven.DependencyInjection.URF.Mongo;
2+
13
namespace OrderService.Configuration
24
{
3-
public class OrderDatabaseSettings
5+
public class OrderDatabaseSettings : IMongoDbSettings
46
{
5-
public string OrdersCollectionName { get; set; }
67
public string ConnectionString { get; set; }
78
public string DatabaseName { get; set; }
9+
public string CollectionName { get; set; }
810
}
911
}

0 commit comments

Comments
 (0)