1+ using Microsoft . EntityFrameworkCore ;
2+ using Microsoft . Extensions . DependencyInjection ;
3+ using Microsoft . Extensions . DependencyInjection . Extensions ;
4+ using System ;
5+
6+ namespace Calabonga . UnitOfWork ;
7+
8+ /// <summary>
9+ /// Extension methods for setting up unit of work related services in an <see cref="IServiceCollection"/>.
10+ /// </summary>
11+ public static class UnitOfWorkFactoryServiceCollectionExtensions
12+ {
13+ /// <summary>
14+ /// Registers the unit of work given context as a service in the <see cref="IServiceCollection"/>.
15+ /// </summary>
16+ /// <typeparam name="TContext">The type of the db context.</typeparam>
17+ /// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param>
18+ /// <param name="lifetime"></param>
19+ /// <returns>The same service collection so that multiple calls can be chained.</returns>
20+ /// <remarks>
21+ /// This method only support one db context, if been called more than once, will throw exception.
22+ /// </remarks>
23+ public static IServiceCollection AddUnitOfWorkFactory < TContext > ( this IServiceCollection services , ServiceLifetime lifetime = ServiceLifetime . Scoped )
24+ where TContext : DbContext
25+ {
26+ switch ( lifetime )
27+ {
28+ case ServiceLifetime . Singleton :
29+ services . TryAddSingleton < IRepositoryFactory , UnitOfWork < TContext > > ( ) ;
30+ // Following has an issue: IUnitOfWork cannot support multiple dbContext/database,
31+ // that means cannot call AddUnitOfWork<TContext> multiple times.
32+ // Solution: check IUnitOfWork whether or null
33+ services . TryAddSingleton < IUnitOfWork , UnitOfWork < TContext > > ( ) ;
34+ services . TryAddSingleton < IUnitOfWork < TContext > , UnitOfWork < TContext > > ( ) ;
35+ services . TryAddSingleton < IUnitOfWorkFactory , UnitOfWorkFactory < TContext > > ( ) ;
36+ services . TryAddSingleton < IUnitOfWorkFactory < TContext > , UnitOfWorkFactory < TContext > > ( ) ;
37+ break ;
38+ case ServiceLifetime . Scoped :
39+ services . TryAddScoped < IRepositoryFactory , UnitOfWork < TContext > > ( ) ;
40+ // Following has an issue: IUnitOfWork cannot support multiple dbContext/database,
41+ // that means cannot call AddUnitOfWork<TContext> multiple times.
42+ // Solution: check IUnitOfWork whether or null
43+ services . TryAddScoped < IUnitOfWork , UnitOfWork < TContext > > ( ) ;
44+ services . TryAddScoped < IUnitOfWork < TContext > , UnitOfWork < TContext > > ( ) ;
45+ services . TryAddScoped < IUnitOfWorkFactory , UnitOfWorkFactory < TContext > > ( ) ;
46+ services . TryAddScoped < IUnitOfWorkFactory < TContext > , UnitOfWorkFactory < TContext > > ( ) ;
47+ break ;
48+ case ServiceLifetime . Transient :
49+ services . TryAddTransient < IRepositoryFactory , UnitOfWork < TContext > > ( ) ;
50+ // Following has an issue: IUnitOfWork cannot support multiple dbContext/database,
51+ // that means cannot call AddUnitOfWork<TContext> multiple times.
52+ // Solution: check IUnitOfWork whether or null
53+ services . TryAddTransient < IUnitOfWork , UnitOfWork < TContext > > ( ) ;
54+ services . TryAddTransient < IUnitOfWork < TContext > , UnitOfWork < TContext > > ( ) ;
55+ services . TryAddTransient < IUnitOfWorkFactory , UnitOfWorkFactory < TContext > > ( ) ;
56+ services . TryAddTransient < IUnitOfWorkFactory < TContext > , UnitOfWorkFactory < TContext > > ( ) ;
57+ break ;
58+ default :
59+ throw new ArgumentOutOfRangeException ( nameof ( lifetime ) , lifetime , null ) ;
60+ }
61+
62+ services . AddScoped < IRepositoryFactory , UnitOfWork < TContext > > ( ) ;
63+ // Following has an issue: IUnitOfWork cannot support multiple dbContext/database,
64+ // that means cannot call AddUnitOfWork<TContext> multiple times.
65+ // Solution: check IUnitOfWork whether or null
66+ services . AddScoped < IUnitOfWork , UnitOfWork < TContext > > ( ) ;
67+ services . AddScoped < IUnitOfWork < TContext > , UnitOfWork < TContext > > ( ) ;
68+
69+ return services ;
70+ }
71+
72+ }
0 commit comments