forked from ardalis/CleanArchitecture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfrastructureServiceExtensions.cs
More file actions
91 lines (77 loc) · 3.64 KB
/
InfrastructureServiceExtensions.cs
File metadata and controls
91 lines (77 loc) · 3.64 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
using Microsoft.Extensions.Configuration;
using NimblePros.Metronome;
using NimblePros.SampleToDo.Core.Interfaces;
using NimblePros.SampleToDo.Infrastructure.Data;
using NimblePros.SampleToDo.Infrastructure.Data.Queries;
using NimblePros.SampleToDo.Infrastructure.Email;
using NimblePros.SampleToDo.UseCases.Contributors.Queries.List;
using NimblePros.SampleToDo.UseCases.Projects.Queries.ListIncompleteItems;
using NimblePros.SampleToDo.UseCases.Projects.Queries.ListShallow;
using NimblePros.SharedKernel;
namespace NimblePros.SampleToDo.Infrastructure;
public static class InfrastructureServiceExtensions
{
public static IServiceCollection AddInfrastructureServices(
this IServiceCollection services,
IConfiguration configuration,
ILogger logger,
string environmentName)
{
if (environmentName == "Development")
{
RegisterDevelopmentOnlyDependencies(services, configuration);
}
else if (environmentName == "Testing")
{
RegisterTestingOnlyDependencies(services);
}
else
{
RegisterProductionOnlyDependencies(services, configuration);
}
RegisterEFRepositories(services);
logger.LogInformation("{Project} services registered", "Infrastructure");
return services;
}
private static void AddDbContextWithSqlite(IServiceCollection services, IConfiguration configuration)
{
services.AddScoped<EventDispatchInterceptor>();
services.AddScoped<IDomainEventDispatcher, MediatorDomainEventDispatcher>();
var connectionString = configuration.GetConnectionString("SqliteConnection");
services.AddDbContext<AppDbContext>((provider, options) =>
{
options.UseSqlite(connectionString)
.AddMetronomeDbTracking(provider)
.AddInterceptors(provider.GetRequiredService<EventDispatchInterceptor>());
});
}
private static void RegisterDevelopmentOnlyDependencies(IServiceCollection services, IConfiguration configuration)
{
AddDbContextWithSqlite(services, configuration);
services.AddScoped<IEmailSender, MimeKitEmailSender>(); // to demo with a localhost test email server like Papercut
services.AddScoped<IListContributorsQueryService, ListContributorsQueryService>();
services.AddScoped<IListIncompleteItemsQueryService, ListIncompleteItemsQueryService>();
services.AddScoped<IListProjectsShallowQueryService, ListProjectsShallowQueryService>();
}
private static void RegisterTestingOnlyDependencies(IServiceCollection services)
{
// do not configure a DbContext here for testing - it's configured in CustomWebApplicationFactory
services.AddScoped<IEmailSender, FakeEmailSender>();
services.AddScoped<IListContributorsQueryService, FakeListContributorsQueryService>();
services.AddScoped<IListIncompleteItemsQueryService, FakeListIncompleteItemsQueryService>();
services.AddScoped<IListProjectsShallowQueryService, FakeListProjectsShallowQueryService>();
}
private static void RegisterProductionOnlyDependencies(IServiceCollection services, IConfiguration configuration)
{
AddDbContextWithSqlite(services, configuration);
services.AddScoped<IEmailSender, MimeKitEmailSender>();
services.AddScoped<IListContributorsQueryService, ListContributorsQueryService>();
services.AddScoped<IListIncompleteItemsQueryService, ListIncompleteItemsQueryService>();
services.AddScoped<IListProjectsShallowQueryService, ListProjectsShallowQueryService>();
}
private static void RegisterEFRepositories(IServiceCollection services)
{
services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));
services.AddScoped(typeof(IReadRepository<>), typeof(EfRepository<>));
}
}