@@ -8,52 +8,13 @@ namespace Masa.Contrib.Isolation.UoW.EFCore;
88/// </summary>
99/// <typeparam name="TKey">tenant id type</typeparam>
1010/// <typeparam name="TDbContext"></typeparam>
11- public abstract class IsolationDbContext < TDbContext , TKey > : MasaDbContext < TDbContext >
11+ public abstract class IsolationDbContext < TDbContext , TKey > : IsolationDbContext < TKey >
1212 where TKey : IComparable
1313 where TDbContext : MasaDbContext , IMasaDbContext
1414{
15- private readonly IMultiEnvironmentContext ? _environmentContext ;
16- private readonly IMultiTenantContext ? _tenantContext ;
17-
18- public IsolationDbContext ( MasaDbContextOptions < TDbContext > options ) : base ( options )
19- {
20- _environmentContext = options . ServiceProvider ? . GetService < IMultiEnvironmentContext > ( ) ;
21- _tenantContext = options . ServiceProvider ? . GetService < IMultiTenantContext > ( ) ;
22- }
23-
24- protected override Expression < Func < TEntity , bool > > ? CreateFilterExpression < TEntity > ( )
25- where TEntity : class
15+ protected IsolationDbContext ( MasaDbContextOptions < TDbContext > options ) : base ( options )
2616 {
27- Expression < Func < TEntity , bool > > ? expression = null ;
28-
29- if ( typeof ( IMultiTenant < > ) . IsGenericInterfaceAssignableFrom ( typeof ( TEntity ) ) && _tenantContext != null )
30- {
31- string defaultTenantId = default ( TKey ) ? . ToString ( ) ?? string . Empty ;
32- Expression < Func < TEntity , bool > > tenantFilter = entity => ! IsTenantFilterEnabled ||
33- ( EF . Property < TKey > ( entity , nameof ( IMultiTenant < TKey > . TenantId ) ) . ToString ( ) ?? string . Empty )
34- . Equals ( _tenantContext . CurrentTenant != null ? _tenantContext . CurrentTenant . Id : defaultTenantId ) ;
35-
36- expression = tenantFilter . And ( expression != null , expression ) ;
37- }
38-
39- if ( typeof ( IMultiEnvironment ) . IsAssignableFrom ( typeof ( TEntity ) ) && _environmentContext != null )
40- {
41- Expression < Func < TEntity , bool > > envFilter = entity => ! IsEnvironmentFilterEnabled ||
42- EF . Property < string > ( entity , nameof ( IMultiEnvironment . Environment ) )
43- . Equals ( _environmentContext != null ? _environmentContext . CurrentEnvironment : default ) ;
44- expression = envFilter . And ( expression != null , expression ) ;
45- }
46-
47- var secondExpression = base . CreateFilterExpression < TEntity > ( ) ;
48- if ( secondExpression != null )
49- expression = secondExpression . And ( expression != null , expression ) ;
50-
51- return expression ;
5217 }
53-
54- protected virtual bool IsEnvironmentFilterEnabled => DataFilter ? . IsEnabled < IMultiEnvironment > ( ) ?? false ;
55-
56- protected virtual bool IsTenantFilterEnabled => DataFilter ? . IsEnabled < IMultiTenant < TKey > > ( ) ?? false ;
5718}
5819
5920/// <summary>
@@ -76,7 +37,7 @@ public abstract class IsolationDbContext<TKey> : MasaDbContext
7637 private readonly IMultiEnvironmentContext ? _environmentContext ;
7738 private readonly IMultiTenantContext ? _tenantContext ;
7839
79- public IsolationDbContext ( MasaDbContextOptions options ) : base ( options )
40+ protected IsolationDbContext ( MasaDbContextOptions options ) : base ( options )
8041 {
8142 _environmentContext = options . ServiceProvider ? . GetService < IMultiEnvironmentContext > ( ) ;
8243 _tenantContext = options . ServiceProvider ? . GetService < IMultiTenantContext > ( ) ;
0 commit comments