-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathApplicationExtensions.cs
76 lines (63 loc) · 3.28 KB
/
ApplicationExtensions.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
using Digdir.Domain.Dialogporten.Application.Common;
using Digdir.Domain.Dialogporten.Application.Common.Behaviours;
using Digdir.Domain.Dialogporten.Application.Common.Extensions;
using Digdir.Domain.Dialogporten.Application.Common.Extensions.OptionExtensions;
using FluentValidation;
using MediatR;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Reflection;
namespace Digdir.Domain.Dialogporten.Application;
public static class ApplicationExtensions
{
public static IServiceCollection AddApplication(this IServiceCollection services, IConfiguration configuration, IHostEnvironment environment)
{
ArgumentNullException.ThrowIfNull(services);
ArgumentNullException.ThrowIfNull(configuration);
var thisAssembly = Assembly.GetExecutingAssembly();
services.AddOptions<ApplicationSettings>()
.Bind(configuration.GetSection(ApplicationSettings.ConfigurationSectionName))
.ValidateFluently()
.ValidateOnStart();
services
// Framework
.AddAutoMapper(thisAssembly)
.AddMediatR(x => x.RegisterServicesFromAssembly(thisAssembly))
.AddValidatorsFromAssembly(thisAssembly, ServiceLifetime.Transient, includeInternalTypes: true)
// Singleton
.AddSingleton<ICompactJwsGenerator, Ed25519Generator>()
// Scoped
.AddScoped<IDomainContext, DomainContext>()
.AddScoped<ITransactionTime, TransactionTime>()
.AddScoped<IDialogTokenGenerator, DialogTokenGenerator>()
// Transient
.AddTransient<IStringHasher, PersistantRandomSaltStringHasher>()
.AddTransient<IUserOrganizationRegistry, UserOrganizationRegistry>()
.AddTransient<IUserResourceRegistry, UserResourceRegistry>()
.AddTransient<IUserNameRegistry, UserNameRegistry>()
.AddTransient<IClock, Clock>()
.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>))
.AddTransient(typeof(IPipelineBehavior<,>), typeof(DomainContextBehaviour<,>));
if (!environment.IsDevelopment())
{
return services;
}
var localDeveloperSettings = configuration.GetLocalDevelopmentSettings();
services.Decorate<IUserResourceRegistry, LocalDevelopmentUserResourceRegistryDecorator>(
predicate:
localDeveloperSettings.UseLocalDevelopmentUser ||
localDeveloperSettings.UseLocalDevelopmentResourceRegister);
services.Decorate<IUserOrganizationRegistry, LocalDevelopmentUserOrganizationRegistryDecorator>(
predicate:
localDeveloperSettings.UseLocalDevelopmentUser ||
localDeveloperSettings.UseLocalDevelopmentOrganizationRegister);
services.Decorate<IUserNameRegistry, LocalDevelopmentUserNameRegistryDecorator>(
predicate:
localDeveloperSettings.UseLocalDevelopmentUser ||
localDeveloperSettings.UseLocalDevelopmentNameRegister);
services.Decorate<ICompactJwsGenerator, LocalDevelopmentCompactJwsGeneratorDecorator>(
predicate: localDeveloperSettings.UseLocalDevelopmentCompactJwsGenerator);
return services;
}
}