1111
1212namespace Microsoft . Extensions . DependencyInjection ;
1313
14+ #pragma warning disable S3267
15+
1416/// <summary>
1517/// Extensions methods for <see cref="IServiceCollection"/>.
1618/// </summary>
@@ -63,6 +65,40 @@ public static IUnitOfWorkBuilder<TDbContext> AddWorkContext<TDbContext>(
6365 return new UnitOfWorkBuilder < TDbContext > ( services , lifetime ) ;
6466 }
6567
68+ /// <summary>
69+ /// Adds a work context related to a <see cref="DbContext"/> and register all implemented interfaces
70+ /// of the <typeparamref name="TDbWorkContext"/> that is assignable to <see cref="IUnitOfWork"/>.
71+ /// </summary>
72+ /// <typeparam name="TDbWorkContext">The type of the work context.</typeparam>
73+ /// <typeparam name="TDbContext">The type of the DbContext used in the work context.</typeparam>
74+ /// <param name="services">The service collection.</param>
75+ /// <param name="lifetime">The services lifetime, by default is scoped.</param>
76+ /// <returns>
77+ /// A unit of work builder to configure the <see cref="DbContext"/> and services like repositories and searches.
78+ /// </returns>
79+ public static IUnitOfWorkBuilder < TDbContext > AddWorkContext < TDbWorkContext , TDbContext > (
80+ this IServiceCollection services ,
81+ ServiceLifetime lifetime = ServiceLifetime . Scoped )
82+ where TDbWorkContext : class , IWorkContext < TDbContext >
83+ where TDbContext : DbContext
84+ {
85+ services . Add ( ServiceDescriptor . Describe (
86+ typeof ( TDbWorkContext ) ,
87+ typeof ( TDbWorkContext ) ,
88+ lifetime ) ) ;
89+
90+ foreach ( var implements in typeof ( TDbWorkContext ) . GetInterfaces ( ) )
91+ {
92+ if ( typeof ( IUnitOfWork ) . IsAssignableFrom ( implements ) )
93+ services . Add ( ServiceDescriptor . Describe (
94+ implements ,
95+ sp => sp . GetService < TDbWorkContext > ( ) ! ,
96+ lifetime ) ) ;
97+ }
98+
99+ return new UnitOfWorkBuilder < TDbContext > ( services , lifetime ) ;
100+ }
101+
66102 /// <summary>
67103 /// Adds a work context related to a <see cref="DbContext"/>.
68104 /// </summary>
@@ -88,32 +124,32 @@ public static IUnitOfWorkBuilder<TDbContext> AddWorkContext<TWorkContext, TDbWor
88124
89125 services . Add ( ServiceDescriptor . Describe (
90126 typeof ( IWorkContext < TDbContext > ) ,
91- sp => sp . GetService < TDbWorkContext > ( ) ! ,
127+ sp => sp . GetService < TWorkContext > ( ) ! ,
92128 lifetime ) ) ;
93129
94130 services . Add ( ServiceDescriptor . Describe (
95131 typeof ( IWorkContext ) ,
96- sp => sp . GetService < TDbWorkContext > ( ) ! ,
132+ sp => sp . GetService < TWorkContext > ( ) ! ,
97133 lifetime ) ) ;
98134
99135 services . Add ( ServiceDescriptor . Describe (
100136 typeof ( IEntityManager ) ,
101- sp => sp . GetService < TDbWorkContext > ( ) ! ,
137+ sp => sp . GetService < TWorkContext > ( ) ! ,
102138 lifetime ) ) ;
103139
104140 services . Add ( ServiceDescriptor . Describe (
105141 typeof ( ISearchManager ) ,
106- sp => sp . GetService < TDbWorkContext > ( ) ! ,
142+ sp => sp . GetService < TWorkContext > ( ) ! ,
107143 lifetime ) ) ;
108144
109145 services . TryAdd ( ServiceDescriptor . Describe (
110146 typeof ( IUnitOfWork < TDbContext > ) ,
111- sp => sp . GetService < TDbWorkContext > ( ) ! ,
147+ sp => sp . GetService < TWorkContext > ( ) ! ,
112148 lifetime ) ) ;
113149
114150 services . TryAdd ( ServiceDescriptor . Describe (
115151 typeof ( IUnitOfWork ) ,
116- sp => sp . GetService < TDbWorkContext > ( ) ! ,
152+ sp => sp . GetService < TWorkContext > ( ) ! ,
117153 lifetime ) ) ;
118154
119155 return new UnitOfWorkBuilder < TDbContext > ( services , lifetime ) ;
0 commit comments