namespace SharedDATA.Api { using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; /// /// Extension methods for setting up unit of work related services in an . /// public static class UnitOfWorkServiceCollectionExtensions { /// /// Registers the unit of work given context as a service in the . /// /// The type of the db context. /// The to add services to. /// The same service collection so that multiple calls can be chained. /// /// This method only support one db context, if been called more than once, will throw exception. /// public static IServiceCollection AddUnitOfWork(this IServiceCollection services) where TContext : DbContext { services.AddScoped>(); // Following has a issue: IUnitOfWork cannot support multiple dbcontext/database, // that means cannot call AddUnitOfWork multiple times. // Solution: check IUnitOfWork whether or null services.AddScoped>(); services.AddScoped, UnitOfWork>(); return services; } /// /// Registers the unit of work given context as a service in the . /// /// The type of the db context. /// The type of the db context. /// The to add services to. /// The same service collection so that multiple calls can be chained. /// /// This method only support one db context, if been called more than once, will throw exception. /// public static IServiceCollection AddUnitOfWork(this IServiceCollection services) where TContext1 : DbContext where TContext2 : DbContext { services.AddScoped, UnitOfWork>(); services.AddScoped, UnitOfWork>(); return services; } /// /// Registers the unit of work given context as a service in the . /// /// The type of the db context. /// The type of the db context. /// The type of the db context. /// The to add services to. /// The same service collection so that multiple calls can be chained. /// /// This method only support one db context, if been called more than once, will throw exception. /// public static IServiceCollection AddUnitOfWork(this IServiceCollection services) where TContext1 : DbContext where TContext2 : DbContext where TContext3 : DbContext { services.AddScoped, UnitOfWork>(); services.AddScoped, UnitOfWork>(); services.AddScoped, UnitOfWork>(); return services; } /// /// Registers the unit of work given context as a service in the . /// /// The type of the db context. /// The type of the db context. /// The type of the db context. /// The type of the db context. /// The to add services to. /// The same service collection so that multiple calls can be chained. /// /// This method only support one db context, if been called more than once, will throw exception. /// public static IServiceCollection AddUnitOfWork(this IServiceCollection services) where TContext1 : DbContext where TContext2 : DbContext where TContext3 : DbContext where TContext4 : DbContext { services.AddScoped, UnitOfWork>(); services.AddScoped, UnitOfWork>(); services.AddScoped, UnitOfWork>(); services.AddScoped, UnitOfWork>(); return services; } /// /// Registers the custom repository as a service in the . /// /// The type of the entity. /// The type of the custom repositry. /// The to add services to. /// The same service collection so that multiple calls can be chained. public static IServiceCollection AddCustomRepository(this IServiceCollection services) where TEntity : class where TRepository : class, IRepository { services.AddScoped, TRepository>(); return services; } } }