ServiceCollectionExtensions.cs 4.1 KB

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