-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathDependencyInjectionExtensions.cs
More file actions
38 lines (31 loc) · 1.61 KB
/
DependencyInjectionExtensions.cs
File metadata and controls
38 lines (31 loc) · 1.61 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
// Copyright (c) TotalSoft.
// This source code is licensed under the MIT license.
using System.Reflection;
using MediatR;
using NBB.ProjectR;
// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection
{
public static class DependencyInjectionExtensions
{
public static IServiceCollection AddProjectR(this IServiceCollection services, params Assembly[] assemblies)
{
var metadata = ProjectorMetadataService.ScanProjectorsMetadata(assemblies);
foreach (var m in metadata)
{
services.AddSingleton(typeof(IProjector<,,>).MakeGenericType(m.ModelType, m.MessageType, m.IdentityType), m.ProjectorType);
foreach (var eventType in m.SubscriptionTypes)
{
var serviceType = typeof(INotificationHandler<>).MakeGenericType(eventType);
var implementationType =
typeof(ProjectorNotificationHandler<,,,>).MakeGenericType(eventType, m.ModelType, m.MessageType, m.IdentityType);
services.AddScoped(serviceType, implementationType);
}
services.AddScoped(typeof(IReadModelStore<>).MakeGenericType(m.ModelType), typeof(ProjectionStore<,,>).MakeGenericType(m.ModelType, m.MessageType, m.IdentityType));
}
services.AddScoped(typeof(IProjectionStore<,,>), typeof(ProjectionStore<,,>));
services.AddSingleton(typeof(ProjectorMetadataAccessor), _ => new ProjectorMetadataAccessor(metadata));
return services;
}
}
}