11
11
12
12
namespace Microsoft . Extensions . DependencyInjection ;
13
13
14
+ #pragma warning disable S3267
15
+
14
16
/// <summary>
15
17
/// Extensions methods for <see cref="IServiceCollection"/>.
16
18
/// </summary>
@@ -63,6 +65,40 @@ public static IUnitOfWorkBuilder<TDbContext> AddWorkContext<TDbContext>(
63
65
return new UnitOfWorkBuilder < TDbContext > ( services , lifetime ) ;
64
66
}
65
67
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
+
66
102
/// <summary>
67
103
/// Adds a work context related to a <see cref="DbContext"/>.
68
104
/// </summary>
@@ -88,32 +124,32 @@ public static IUnitOfWorkBuilder<TDbContext> AddWorkContext<TWorkContext, TDbWor
88
124
89
125
services . Add ( ServiceDescriptor . Describe (
90
126
typeof ( IWorkContext < TDbContext > ) ,
91
- sp => sp . GetService < TDbWorkContext > ( ) ! ,
127
+ sp => sp . GetService < TWorkContext > ( ) ! ,
92
128
lifetime ) ) ;
93
129
94
130
services . Add ( ServiceDescriptor . Describe (
95
131
typeof ( IWorkContext ) ,
96
- sp => sp . GetService < TDbWorkContext > ( ) ! ,
132
+ sp => sp . GetService < TWorkContext > ( ) ! ,
97
133
lifetime ) ) ;
98
134
99
135
services . Add ( ServiceDescriptor . Describe (
100
136
typeof ( IEntityManager ) ,
101
- sp => sp . GetService < TDbWorkContext > ( ) ! ,
137
+ sp => sp . GetService < TWorkContext > ( ) ! ,
102
138
lifetime ) ) ;
103
139
104
140
services . Add ( ServiceDescriptor . Describe (
105
141
typeof ( ISearchManager ) ,
106
- sp => sp . GetService < TDbWorkContext > ( ) ! ,
142
+ sp => sp . GetService < TWorkContext > ( ) ! ,
107
143
lifetime ) ) ;
108
144
109
145
services . TryAdd ( ServiceDescriptor . Describe (
110
146
typeof ( IUnitOfWork < TDbContext > ) ,
111
- sp => sp . GetService < TDbWorkContext > ( ) ! ,
147
+ sp => sp . GetService < TWorkContext > ( ) ! ,
112
148
lifetime ) ) ;
113
149
114
150
services . TryAdd ( ServiceDescriptor . Describe (
115
151
typeof ( IUnitOfWork ) ,
116
- sp => sp . GetService < TDbWorkContext > ( ) ! ,
152
+ sp => sp . GetService < TWorkContext > ( ) ! ,
117
153
lifetime ) ) ;
118
154
119
155
return new UnitOfWorkBuilder < TDbContext > ( services , lifetime ) ;
0 commit comments