-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathServiceCollectionExtensions.cs
More file actions
105 lines (87 loc) · 4.44 KB
/
Copy pathServiceCollectionExtensions.cs
File metadata and controls
105 lines (87 loc) · 4.44 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
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using RackPeek.Domain.Persistence;
using RackPeek.Domain.Resources;
using RackPeek.Domain.Resources.Hardware;
using RackPeek.Domain.Resources.Services;
using RackPeek.Domain.Resources.SystemResources;
using RackPeek.Domain.Templates;
using RackPeek.Domain.UseCases;
using RackPeek.Domain.UseCases.Cpus;
using RackPeek.Domain.UseCases.Drives;
using RackPeek.Domain.UseCases.Gpus;
using RackPeek.Domain.UseCases.Nics;
using RackPeek.Domain.UseCases.Labels;
using RackPeek.Domain.UseCases.Ports;
using RackPeek.Domain.UseCases.Tags;
namespace RackPeek.Domain;
public interface IResourceUseCase<T> where T : Resource
{
}
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddResourceUseCases(
this IServiceCollection services,
Assembly assembly)
{
var types = assembly.GetTypes()
.Where(t => !t.IsAbstract && !t.IsInterface);
foreach (var type in types)
{
var resourceUseCaseInterfaces = type.GetInterfaces()
.Where(i =>
i.IsGenericType &&
i.GetInterfaces().Any(parent =>
parent.IsGenericType &&
parent.GetGenericTypeDefinition() == typeof(IResourceUseCase<>)));
foreach (var serviceType in resourceUseCaseInterfaces) services.AddScoped(serviceType, type);
}
return services;
}
public static IServiceCollection AddUseCases(
this IServiceCollection services)
{
services.AddScoped(typeof(IAddResourceUseCase<>), typeof(AddResourceUseCase<>));
services.AddScoped(typeof(IAddResourceFromTemplateUseCase<>), typeof(AddResourceFromTemplateUseCase<>));
services.AddScoped(typeof(IAddLabelUseCase<>), typeof(AddLabelUseCase<>));
services.AddScoped(typeof(IAddTagUseCase<>), typeof(AddTagUseCase<>));
services.AddScoped(typeof(ICloneResourceUseCase<>), typeof(CloneResourceUseCase<>));
services.AddScoped(typeof(IDeleteResourceUseCase<>), typeof(DeleteResourceUseCase<>));
services.AddScoped(typeof(IRemoveLabelUseCase<>), typeof(RemoveLabelUseCase<>));
services.AddScoped(typeof(IRemoveTagUseCase<>), typeof(RemoveTagUseCase<>));
services.AddScoped(typeof(IGetAllResourcesByKindUseCase<>), typeof(GetAllResourcesByKindUseCase<>));
services.AddScoped(typeof(IGetResourceByNameUseCase<>), typeof(GetResourceByNameUseCase<>));
services.AddScoped(typeof(IRenameResourceUseCase<>), typeof(RenameResourceUseCase<>));
services.AddScoped(typeof(IAddCpuUseCase<>), typeof(AddCpuUseCase<>));
services.AddScoped(typeof(IRemoveCpuUseCase<>), typeof(RemoveCpuUseCase<>));
services.AddScoped(typeof(IUpdateCpuUseCase<>), typeof(UpdateCpuUseCase<>));
services.AddScoped(typeof(IAddDriveUseCase<>), typeof(AddDriveUseCase<>));
services.AddScoped(typeof(IRemoveDriveUseCase<>), typeof(RemoveDriveUseCase<>));
services.AddScoped(typeof(IUpdateDriveUseCase<>), typeof(UpdateDriveUseCase<>));
services.AddScoped(typeof(IAddGpuUseCase<>), typeof(AddGpuUseCase<>));
services.AddScoped(typeof(IRemoveGpuUseCase<>), typeof(RemoveGpuUseCase<>));
services.AddScoped(typeof(IUpdateGpuUseCase<>), typeof(UpdateGpuUseCase<>));
services.AddScoped(typeof(IAddPortUseCase<>), typeof(AddPortUseCase<>));
services.AddScoped(typeof(IRemovePortUseCase<>), typeof(RemovePortUseCase<>));
services.AddScoped(typeof(IUpdatePortUseCase<>), typeof(UpdatePortUseCase<>));
services.AddScoped(typeof(IAddNicUseCase<>), typeof(AddNicUseCase<>));
services.AddScoped(typeof(IRemoveNicUseCase<>), typeof(RemoveNicUseCase<>));
services.AddScoped(typeof(IUpdateNicUseCase<>), typeof(UpdateNicUseCase<>));
var usecases = Assembly.GetAssembly(typeof(IUseCase))
?.GetTypes()
.Where(t =>
!t.IsAbstract &&
typeof(IUseCase).IsAssignableFrom(t)
);
foreach (var type in usecases) services.AddScoped(type);
return services;
}
public static IServiceCollection AddYamlRepos(
this IServiceCollection services)
{
services.AddScoped<IHardwareRepository, YamlHardwareRepository>();
services.AddScoped<ISystemRepository, YamlSystemRepository>();
services.AddScoped<IServiceRepository, ServiceRepository>();
return services;
}
}