-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathServiceCollectionExtensions.cs
More file actions
169 lines (148 loc) · 6.33 KB
/
ServiceCollectionExtensions.cs
File metadata and controls
169 lines (148 loc) · 6.33 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using Duber.Domain.Trip.Commands;
using Duber.Trip.API.Application.DomainEventHandlers;
using Duber.Trip.API.Infrastructure.Repository;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using AutoMapper;
using Duber.Infrastructure.EventBus.Idempotency;
using Duber.Infrastructure.EventBus.RabbitMQ.IoC;
using Duber.Infrastructure.EventBus.ServiceBus.IoC;
using OpenCqrs;
using OpenCqrs.Commands;
using OpenCqrs.Configuration;
using OpenCqrs.Domain;
using OpenCqrs.Events;
using OpenCqrs.Extensions;
using OpenCqrs.Queries;
using OpenCqrs.Store.Cosmos.Mongo.Configuration;
using Microsoft.OpenApi.Models;
using OpenCqrs.Store.Cosmos.Mongo.Extensions;
using MongoDB.Bson.Serialization;
using Microsoft.Extensions.Diagnostics.HealthChecks;
namespace Duber.Trip.API.Extensions
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddCQRS(this IServiceCollection services, IConfiguration configuration)
{
// Kledex only needs a type per assembly, it automatically registers the rest of the commands, events, etc.
services.Configure<MongoOptions>(configuration.GetSection("EventStoreConfiguration"));
services.AddCustomKledex(options =>
{
options.PublishEvents = true;
options.SaveCommandData = true;
}, typeof(CreateTripCommand), typeof(TripCreatedDomainEventHandlerAsync))
.AddCosmosMongoStore(__ => configuration.Get<MongoOptions>());
services.AddTransient<IEventStoreRepository, EventStoreRepository>();
return services;
}
public static IServiceCollection AddCustomSwagger(this IServiceCollection services)
{
// swagger configuration
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "Duber.Trip HTTP API",
Version = "v1",
Description = "The Duber Trip Service HTTP API"
});
// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetEntryAssembly()?.GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath);
});
return services;
}
/// <summary>
/// I had to override this method since I was using automapper to map out some commands and events and Kledex internally overrides those mappings.
/// </summary>
/// <param name="services"></param>
/// <param name="setupAction"></param>
/// <param name="types"></param>
/// <returns></returns>
public static IOpenCqrsServiceBuilder AddCustomKledex(this IServiceCollection services, Action<MainOptions> setupAction, params Type[] types)
{
var typeList = types.ToList();
typeList.Add(typeof(IDispatcher));
services.Scan(s => s
.FromAssembliesOf(typeList)
.AddClasses()
.AsImplementedInterfaces());
services.AddTransient(typeof(IRepository<>), typeof(Repository<>));
services.AddCustomAutoMapper(typeList);
services.Configure(setupAction);
return new OpenCqrsServiceBuilder(services);
}
private static IServiceCollection AddCustomAutoMapper(this IServiceCollection services, List<Type> types)
{
var autoMapperConfig = new MapperConfiguration(cfg =>
{
foreach (var type in types)
{
var typesToMap = type.Assembly.GetTypes()
.Where(t => t.GetTypeInfo().IsClass && !t.GetTypeInfo().IsAbstract && (
typeof(ICommand).IsAssignableFrom(t) ||
typeof(IEvent).IsAssignableFrom(t) ||
typeof(IQuery<>).IsAssignableFrom(t)))
.ToList();
foreach (var typeToMap in typesToMap)
{
cfg.CreateMap(typeToMap, typeToMap);
cfg.AddMaps(types);
}
}
});
services.AddSingleton(sp => autoMapperConfig.CreateMapper());
return services;
}
public static IServiceCollection AddServiceBroker(this IServiceCollection services, IConfiguration configuration)
{
if (configuration.GetValue<bool>("AzureServiceBusEnabled"))
{
services.AddServiceBus(configuration);
}
else
{
services.AddRabbitMQ(configuration);
}
return services;
}
public static IServiceCollection AddIdempotency(this IServiceCollection services)
{
services.AddTransient<IIdempotencyStoreProvider, IdempotencyStoreProvider>();
services.RegisterIdempotentHandlers(typeof(TripUpdatedIdempotentEventHandler));
BsonClassMap.RegisterClassMap<IdempotentMessage>(cm =>
{
cm.AutoMap();
cm.SetIgnoreExtraElements(true);
});
return services;
}
public static IServiceCollection AddHealthChecks(this IServiceCollection services, IConfiguration configuration)
{
var hcBuilder = services.AddHealthChecks();
hcBuilder.AddCheck("self", () => HealthCheckResult.Healthy());
hcBuilder
.AddMongoDb(
configuration["EventStoreConfiguration:ConnectionString"],
mongoDatabaseName: string.Empty,
name: "TripDB-check",
tags: new string[] { "tripdb" });
if (configuration.GetValue<bool>("AzureServiceBusEnabled"))
{
hcBuilder.AddAzureServiceBusTopic(configuration, "trip-az-servicebus-check");
}
else
{
hcBuilder.AddRabbitMQ(configuration, "trip-rabbitmqbus-check");
}
return services;
}
}
}