ServiceCollectionExtensions.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Reflection;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using RackPeek.Domain.Persistence;
  4. using RackPeek.Domain.Resources;
  5. using RackPeek.Domain.Resources.Connections;
  6. using RackPeek.Domain.Resources.Hardware;
  7. using RackPeek.Domain.Resources.Services;
  8. using RackPeek.Domain.Resources.SystemResources;
  9. using RackPeek.Domain.UseCases;
  10. using RackPeek.Domain.UseCases.Cpus;
  11. using RackPeek.Domain.UseCases.Drives;
  12. using RackPeek.Domain.UseCases.Gpus;
  13. using RackPeek.Domain.UseCases.Labels;
  14. using RackPeek.Domain.UseCases.Ports;
  15. using RackPeek.Domain.UseCases.Tags;
  16. namespace RackPeek.Domain;
  17. public interface IResourceUseCase<T> where T : Resource {
  18. }
  19. public static class ServiceCollectionExtensions {
  20. public static IServiceCollection AddResourceUseCases(
  21. this IServiceCollection services,
  22. Assembly assembly) {
  23. IEnumerable<Type> types = assembly.GetTypes()
  24. .Where(t => !t.IsAbstract && !t.IsInterface);
  25. foreach (Type type in types) {
  26. IEnumerable<Type> resourceUseCaseInterfaces = type.GetInterfaces()
  27. .Where(i =>
  28. i.IsGenericType &&
  29. i.GetInterfaces().Any(parent =>
  30. parent.IsGenericType &&
  31. parent.GetGenericTypeDefinition() == typeof(IResourceUseCase<>)));
  32. foreach (Type serviceType in resourceUseCaseInterfaces) services.AddScoped(serviceType, type);
  33. }
  34. return services;
  35. }
  36. public static IServiceCollection AddUseCases(
  37. this IServiceCollection services) {
  38. services.AddScoped(typeof(IAddResourceUseCase<>), typeof(AddResourceUseCase<>));
  39. services.AddScoped(typeof(IAddLabelUseCase<>), typeof(AddLabelUseCase<>));
  40. services.AddScoped(typeof(IAddTagUseCase<>), typeof(AddTagUseCase<>));
  41. services.AddScoped(typeof(ICloneResourceUseCase<>), typeof(CloneResourceUseCase<>));
  42. services.AddScoped(typeof(IDeleteResourceUseCase<>), typeof(DeleteResourceUseCase<>));
  43. services.AddScoped(typeof(IRemoveLabelUseCase<>), typeof(RemoveLabelUseCase<>));
  44. services.AddScoped(typeof(IRemoveTagUseCase<>), typeof(RemoveTagUseCase<>));
  45. services.AddScoped(typeof(IGetAllResourcesByKindUseCase<>), typeof(GetAllResourcesByKindUseCase<>));
  46. services.AddScoped(typeof(IGetResourceByNameUseCase<>), typeof(GetResourceByNameUseCase<>));
  47. services.AddScoped(typeof(IRenameResourceUseCase<>), typeof(RenameResourceUseCase<>));
  48. services.AddScoped(typeof(IAddCpuUseCase<>), typeof(AddCpuUseCase<>));
  49. services.AddScoped(typeof(IRemoveCpuUseCase<>), typeof(RemoveCpuUseCase<>));
  50. services.AddScoped(typeof(IUpdateCpuUseCase<>), typeof(UpdateCpuUseCase<>));
  51. services.AddScoped(typeof(IAddDriveUseCase<>), typeof(AddDriveUseCase<>));
  52. services.AddScoped(typeof(IRemoveDriveUseCase<>), typeof(RemoveDriveUseCase<>));
  53. services.AddScoped(typeof(IUpdateDriveUseCase<>), typeof(UpdateDriveUseCase<>));
  54. services.AddScoped(typeof(IAddGpuUseCase<>), typeof(AddGpuUseCase<>));
  55. services.AddScoped(typeof(IRemoveGpuUseCase<>), typeof(RemoveGpuUseCase<>));
  56. services.AddScoped(typeof(IUpdateGpuUseCase<>), typeof(UpdateGpuUseCase<>));
  57. services.AddScoped(typeof(IAddPortUseCase<>), typeof(AddPortUseCase<>));
  58. services.AddScoped(typeof(IRemovePortUseCase<>), typeof(RemovePortUseCase<>));
  59. services.AddScoped(typeof(IUpdatePortUseCase<>), typeof(UpdatePortUseCase<>));
  60. services.AddScoped(typeof(IAddConnectionUseCase), typeof(AddConnectionUseCase));
  61. services.AddScoped(typeof(IGetConnectionForPortUseCase), typeof(GetConnectionForPortUseCase));
  62. services.AddScoped(typeof(IGetConnectionsForResourceUseCase), typeof(GetConnectionsForResourceUseCase));
  63. services.AddScoped(typeof(IRemoveConnectionUseCase), typeof(RemoveConnectionUseCase));
  64. IEnumerable<Type>? usecases = Assembly.GetAssembly(typeof(IUseCase))
  65. ?.GetTypes()
  66. .Where(t =>
  67. !t.IsAbstract &&
  68. typeof(IUseCase).IsAssignableFrom(t)
  69. );
  70. if (usecases != null)
  71. foreach (Type type in usecases)
  72. services.AddScoped(type);
  73. return services;
  74. }
  75. public static IServiceCollection AddYamlRepos(
  76. this IServiceCollection services) {
  77. services.AddScoped<IHardwareRepository, YamlHardwareRepository>();
  78. services.AddScoped<ISystemRepository, YamlSystemRepository>();
  79. services.AddScoped<IServiceRepository, ServiceRepository>();
  80. return services;
  81. }
  82. }