using System; using Kyoo.Controllers; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace Kyoo { /// /// A static class with helper functions to setup external modules /// public static class Module { /// /// Register a new task to the container. /// /// The container /// The type of the task /// The initial container. public static IServiceCollection AddTask(this IServiceCollection services) where T : class, ITask { services.AddSingleton(); return services; } /// /// Register a new repository to the container. /// /// The container /// The lifetime of the repository. The default is scoped. /// The type of the repository. /// /// If your repository implements a special interface, please use /// /// The initial container. public static IServiceCollection AddRepository(this IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Scoped) where T : IBaseRepository { Type repository = Utility.GetGenericDefinition(typeof(T), typeof(IRepository<>)); if (repository != null) services.Add(ServiceDescriptor.Describe(repository, typeof(T), lifetime)); services.Add(ServiceDescriptor.Describe(typeof(IBaseRepository), typeof(T), lifetime)); return services; } /// /// Register a new repository with a custom mapping to the container. /// /// /// The lifetime of the repository. The default is scoped. /// The custom mapping you have for your repository. /// The type of the repository. /// /// If your repository does not implements a special interface, please use /// /// The initial container. public static IServiceCollection AddRepository(this IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Scoped) where T2 : IBaseRepository, T { services.Add(ServiceDescriptor.Describe(typeof(T), typeof(T2), lifetime)); return services.AddRepository(lifetime); } public static IServiceCollection AddConfiguration(this IServiceCollection services, IConfiguration config, string path) { services.Configure(config.GetSection(path)); services.AddSingleton(new ConfigReference(path, typeof(T))); return services; } } }