-
Notifications
You must be signed in to change notification settings - Fork 2
/
CQRSBooter.cs
89 lines (72 loc) · 3.3 KB
/
CQRSBooter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Threading.Tasks;
using AspNetCoreSample.Denormalizer;
using AspNetCoreSample.Domain;
using BE.CQRS.Data.MongoDb;
using BE.CQRS.Di.AspCore;
using BE.CQRS.Domain.Configuration;
using BE.CQRS.Domain.Denormalization;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Driver;
namespace AspNetCoreSample
{
public static class CQRSBooter
{
public static IServiceCollection AddCqrs(this IServiceCollection serivces, IConfiguration config)
{
Console.WriteLine("Adding CQRS...");
string url = config["CustomerDatabase:MongoDb:Host"];
string db = config["CustomerDatabase:MongoDb:Name"];
Console.WriteLine($"ES DB: {url} - {db}");
IMongoDatabase mongodb = new MongoClient(url).GetDatabase(db);
serivces.AddEventSource(
new EventSourceConfiguration()
.SetDomainObjectAssemblies(typeof(Customer).Assembly)
.SetServiceProviderActivator()
.SetMongoDomainObjectRepository(mongodb)
.SetConventionBasedInMemoryCommandBus());
Console.WriteLine($"CQRS added");
return serivces;
}
public static IApplicationBuilder UseCqrs(this IApplicationBuilder app)
{
app.UseServiceProviderActivator();
return app;
}
public static IServiceCollection AddCustomerDenormalizer(this IServiceCollection collection, IConfiguration config)
{
string url = config["CustomerDatabase:MongoDb:Host"];
string db = config["CustomerDatabase:MongoDb:Name"];
IMongoDatabase eventDb = new MongoClient(url).GetDatabase(db);
IMongoDatabase readDb = GetReadDatabaseMongoDatabase(config, url);
var deconfig = new DenormalizerConfiguration()
.SetDenormalizerAssemblies(typeof(CustomerDenormalizer).Assembly)
.SetMongoEventPositionGateway(readDb)
.SetMongoDbEventSubscriber(eventDb)
.SetServiceProviderDenormalizerActivator();
collection.AddDenormalizers(deconfig);
var ctx = new DenormalizerContext(readDb);
collection.AddSingleton<IDenormalizerContext>(ctx);
return collection;
}
private static IMongoDatabase GetReadDatabaseMongoDatabase(IConfiguration config, string url)
{
string readDburl = config["CustomerDatabase:MongoDb:Host"];
string Readdb = config["CustomerDatabase:MongoDb:Name"];
IMongoDatabase readDb = new MongoClient(url).GetDatabase(Readdb);
return readDb;
}
public static async Task<IApplicationBuilder> UseCustomerDenormalizerAsync(this IApplicationBuilder app)
{
var cfg = app.ApplicationServices.GetRequiredService<DenormalizerConfiguration>();
app.UseServiceProviderActivator();
var foo = cfg.Activator as ServiceCollectionActivator;
foo.UseProvider(app.ApplicationServices);
EventDenormalizer denormalizer = app.UseConvetionBasedDenormalizer();
await denormalizer.StartAsync(TimeSpan.FromMilliseconds(250));
return app;
}
}
}