Skip to content

Commit f469d05

Browse files
committed
Use schema registry.
1 parent e1601d8 commit f469d05

File tree

12 files changed

+82
-70
lines changed

12 files changed

+82
-70
lines changed

reference-architecture/CustomerService/Configuration/CustomerDatabaseSettings.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
namespace CustomerService.Configuration
22
{
3-
public class CustomerDatabaseSettings : ICustomerDatabaseSettings
4-
{
5-
public string CustomersCollectionName { get; set; }
6-
public string ConnectionString { get; set; }
7-
public string DatabaseName { get; set; }
8-
}
9-
10-
public interface ICustomerDatabaseSettings
3+
public class CustomerDatabaseSettings
114
{
125
public string CustomersCollectionName { get; set; }
136
public string ConnectionString { get; set; }

reference-architecture/CustomerService/Constants.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

reference-architecture/CustomerService/CustomerService.csproj

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

77
<ItemGroup>
88
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
9-
<PackageReference Include="EventDriven.EventBus.Dapr" Version="1.0.0" />
10-
<PackageReference Include="MongoDB.Driver" Version="2.12.2" />
11-
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.3" />
9+
<PackageReference Include="EventDriven.EventBus.Dapr" Version="1.1.0-beta3" />
10+
<PackageReference Include="MongoDB.Driver" Version="2.12.3" />
11+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.4" />
1212
<PackageReference Include="URF.Core.Mongo" Version="3.1.3" />
1313
</ItemGroup>
1414

reference-architecture/CustomerService/Startup.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using CustomerService.Domain.CustomerAggregate;
33
using CustomerService.Domain.CustomerAggregate.CommandHandlers;
44
using CustomerService.Repositories;
5+
using EventDriven.EventBus.Dapr;
56
using Microsoft.AspNetCore.Builder;
67
using Microsoft.AspNetCore.Hosting;
78
using Microsoft.Extensions.Configuration;
@@ -36,29 +37,37 @@ public void ConfigureServices(IServiceCollection services)
3637
// Configuration
3738
services.Configure<CustomerDatabaseSettings>(
3839
Configuration.GetSection(nameof(CustomerDatabaseSettings)));
39-
services.AddSingleton<ICustomerDatabaseSettings>(sp =>
40+
services.AddSingleton(sp =>
4041
sp.GetRequiredService<IOptions<CustomerDatabaseSettings>>().Value);
4142

4243
// Registrations
4344
services.AddAutoMapper(typeof(Startup));
4445
services.AddSingleton<CustomerCommandHandler>();
4546
services.AddSingleton(sp =>
4647
{
47-
var settings = sp.GetRequiredService<ICustomerDatabaseSettings>();
48+
var settings = sp.GetRequiredService<CustomerDatabaseSettings>();
4849
var client = new MongoClient(settings.ConnectionString);
49-
return client.GetDatabase(settings.DatabaseName);
50-
});
51-
services.AddSingleton(sp =>
52-
{
53-
var context = sp.GetRequiredService<IMongoDatabase>();
54-
var settings = sp.GetRequiredService<ICustomerDatabaseSettings>();
55-
return context.GetCollection<Customer>(settings.CustomersCollectionName);
50+
var database = client.GetDatabase(settings.DatabaseName);
51+
return database.GetCollection<Customer>(settings.CustomersCollectionName);
5652
});
5753
services.AddSingleton<IDocumentRepository<Customer>, DocumentRepository<Customer>>();
5854
services.AddSingleton<ICustomerRepository, CustomerRepository>();
5955

60-
// Event Bus
61-
services.AddDaprEventBus(Constants.DaprPubSubName);
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);
61+
62+
// 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+
});
6271
}
6372

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

reference-architecture/CustomerService/appsettings.Development.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,5 @@
55
"Microsoft": "Warning",
66
"Microsoft.Hosting.Lifetime": "Information"
77
}
8-
},
9-
"CustomerDatabaseSettings": {
10-
"ConnectionString": "mongodb://localhost:27017"
118
}
129
}

reference-architecture/CustomerService/appsettings.json

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,22 @@
88
},
99
"AllowedHosts": "*",
1010
"CustomerDatabaseSettings": {
11-
"CustomersCollectionName": "Customers",
12-
"ConnectionString": "",
13-
"DatabaseName": "CustomersDb"
11+
"ConnectionString": "mongodb://localhost:27017",
12+
"DatabaseName": "CustomersDb",
13+
"CustomersCollectionName": "Customers"
14+
},
15+
"DaprEventBusOptions": {
16+
"PubSubName": "pubsub"
17+
},
18+
"DaprEventBusSchemaOptions": {
19+
"UseSchemaRegistry": true,
20+
"SchemaValidatorType": "Json",
21+
"SchemaRegistryType": "Mongo",
22+
"AddSchemaOnPublish": true,
23+
"MongoStateStoreOptions": {
24+
"ConnectionString": "mongodb://localhost:27017",
25+
"DatabaseName": "schema-registry",
26+
"SchemasCollectionName": "schemas"
27+
}
1428
}
1529
}

reference-architecture/OrderService/Configuration/OrderDatabaseSettings.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
namespace OrderService.Configuration
22
{
3-
public class OrderDatabaseSettings : IOrderDatabaseSettings
4-
{
5-
public string OrdersCollectionName { get; set; }
6-
public string ConnectionString { get; set; }
7-
public string DatabaseName { get; set; }
8-
}
9-
10-
public interface IOrderDatabaseSettings
3+
public class OrderDatabaseSettings
114
{
125
public string OrdersCollectionName { get; set; }
136
public string ConnectionString { get; set; }

reference-architecture/OrderService/Constants.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

reference-architecture/OrderService/OrderService.csproj

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

77
<ItemGroup>
88
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
9-
<PackageReference Include="EventDriven.EventBus.Dapr" Version="1.0.0" />
10-
<PackageReference Include="MongoDB.Driver" Version="2.12.2" />
11-
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.3" />
9+
<PackageReference Include="EventDriven.EventBus.Dapr" Version="1.1.0-beta3" />
10+
<PackageReference Include="MongoDB.Driver" Version="2.12.3" />
11+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.4" />
1212
<PackageReference Include="URF.Core.Mongo" Version="3.1.3" />
1313
</ItemGroup>
1414

reference-architecture/OrderService/Startup.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using EventDriven.EventBus.Dapr;
12
using Microsoft.AspNetCore.Builder;
23
using Microsoft.AspNetCore.Hosting;
34
using Microsoft.Extensions.Configuration;
@@ -37,30 +38,38 @@ public void ConfigureServices(IServiceCollection services)
3738
// Configuration
3839
services.Configure<OrderDatabaseSettings>(
3940
Configuration.GetSection(nameof(OrderDatabaseSettings)));
40-
services.AddSingleton<IOrderDatabaseSettings>(sp =>
41+
services.AddSingleton(sp =>
4142
sp.GetRequiredService<IOptions<OrderDatabaseSettings>>().Value);
4243

4344
// Registrations
4445
services.AddAutoMapper(typeof(Startup));
4546
services.AddSingleton<OrderCommandHandler>();
4647
services.AddSingleton(sp =>
4748
{
48-
var settings = sp.GetRequiredService<IOrderDatabaseSettings>();
49+
var settings = sp.GetRequiredService<OrderDatabaseSettings>();
4950
var client = new MongoClient(settings.ConnectionString);
50-
return client.GetDatabase(settings.DatabaseName);
51-
});
52-
services.AddSingleton(sp =>
53-
{
54-
var context = sp.GetRequiredService<IMongoDatabase>();
55-
var settings = sp.GetRequiredService<IOrderDatabaseSettings>();
56-
return context.GetCollection<Order>(settings.OrdersCollectionName);
51+
var database = client.GetDatabase(settings.DatabaseName);
52+
return database.GetCollection<Order>(settings.OrdersCollectionName);
5753
});
5854
services.AddSingleton<IDocumentRepository<Order>, DocumentRepository<Order>>();
5955
services.AddSingleton<IOrderRepository, OrderRepository>();
6056
services.AddSingleton<CustomerAddressUpdatedEventHandler>();
6157

62-
// Event Bus
63-
services.AddDaprEventBus(Constants.DaprPubSubName);
58+
// Configuration
59+
var eventBusOptions = new DaprEventBusOptions();
60+
Configuration.GetSection(nameof(DaprEventBusOptions)).Bind(eventBusOptions);
61+
var eventBusSchemaOptions = new DaprEventBusSchemaOptions();
62+
Configuration.GetSection(nameof(DaprEventBusSchemaOptions)).Bind(eventBusSchemaOptions);
63+
64+
// Add Dapr event bus
65+
services.AddDaprEventBus(eventBusOptions.PubSubName, options =>
66+
{
67+
options.UseSchemaRegistry = eventBusSchemaOptions.UseSchemaRegistry;
68+
options.SchemaRegistryType = eventBusSchemaOptions.SchemaRegistryType;
69+
options.MongoStateStoreOptions = eventBusSchemaOptions.MongoStateStoreOptions;
70+
options.SchemaValidatorType = eventBusSchemaOptions.SchemaValidatorType;
71+
options.AddSchemaOnPublish = eventBusSchemaOptions.AddSchemaOnPublish;
72+
});
6473
}
6574

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

0 commit comments

Comments
 (0)