ServiceCollectionExtensions.cs 3.4 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.Hardware.Desktops;
  7. using RackPeek.Domain.Resources.Models;
  8. using RackPeek.Domain.Resources.Services;
  9. using RackPeek.Domain.Resources.SystemResources;
  10. using RackPeek.Domain.UseCases;
  11. using RackPeek.Domain.UseCases.Cpus;
  12. using RackPeek.Domain.UseCases.Drives;
  13. using RackPeek.Domain.UseCases.Tags;
  14. namespace RackPeek.Domain;
  15. public interface IResourceUseCase<T> where T : Resource
  16. {
  17. }
  18. public static class ServiceCollectionExtensions
  19. {
  20. public static IServiceCollection AddResourceUseCases(
  21. this IServiceCollection services,
  22. Assembly assembly)
  23. {
  24. var types = assembly.GetTypes()
  25. .Where(t => !t.IsAbstract && !t.IsInterface);
  26. foreach (var type in types)
  27. {
  28. var resourceUseCaseInterfaces = type.GetInterfaces()
  29. .Where(i =>
  30. i.IsGenericType &&
  31. i.GetInterfaces().Any(parent =>
  32. parent.IsGenericType &&
  33. parent.GetGenericTypeDefinition() == typeof(IResourceUseCase<>)));
  34. foreach (var serviceType in resourceUseCaseInterfaces)
  35. {
  36. services.AddScoped(serviceType, type);
  37. }
  38. }
  39. return services;
  40. }
  41. public static IServiceCollection AddUseCases(
  42. this IServiceCollection services)
  43. {
  44. services.AddScoped(typeof(IAddResourceUseCase<>), typeof(AddResourceUseCase<>));
  45. services.AddScoped(typeof(IAddTagUseCase<>), typeof(AddTagUseCase<>));
  46. services.AddScoped(typeof(ICloneResourceUseCase<>), typeof(CloneResourceUseCase<>));
  47. services.AddScoped(typeof(IDeleteResourceUseCase<>), typeof(DeleteResourceUseCase<>));
  48. services.AddScoped(typeof(IRemoveTagUseCase<>), typeof(RemoveTagUseCase<>));
  49. services.AddScoped(typeof(IGetAllResourcesByKindUseCase<>), typeof(GetAllResourcesByKindUseCase<>));
  50. services.AddScoped(typeof(IGetResourceByNameUseCase<>), typeof(GetResourceByNameUseCase<>));
  51. services.AddScoped(typeof(IRenameResourceUseCase<>), typeof(RenameResourceUseCase<>));
  52. services.AddScoped(typeof(IAddCpuUseCase<>), typeof(AddCpuUseCase<>));
  53. services.AddScoped(typeof(IRemoveCpuUseCase<>), typeof(RemoveCpuUseCase<>));
  54. services.AddScoped(typeof(IUpdateCpuUseCase<>), typeof(UpdateCpuUseCase<>));
  55. services.AddScoped(typeof(IAddDriveUseCase<>), typeof(AddDriveUseCase<>));
  56. services.AddScoped(typeof(IRemoveDriveUseCase<>), typeof(RemoveDriveUseCase<>));
  57. services.AddScoped(typeof(IUpdateDriveUseCase<>), typeof(UpdateDriveUseCase<>));
  58. var usecases = Assembly.GetAssembly(typeof(IUseCase))
  59. ?.GetTypes()
  60. .Where(t =>
  61. !t.IsAbstract &&
  62. typeof(IUseCase).IsAssignableFrom(t)
  63. );
  64. foreach (var type in usecases) services.AddScoped(type);
  65. return services;
  66. }
  67. public static IServiceCollection AddYamlRepos(
  68. this IServiceCollection services)
  69. {
  70. services.AddScoped<IHardwareRepository, YamlHardwareRepository>();
  71. services.AddScoped<ISystemRepository, YamlSystemRepository>();
  72. services.AddScoped<IServiceRepository, YamlServiceRepository>();
  73. return services;
  74. }
  75. }