@@ -8,52 +8,13 @@ namespace Masa.Contrib.Isolation.UoW.EFCore;
8
8
/// </summary>
9
9
/// <typeparam name="TKey">tenant id type</typeparam>
10
10
/// <typeparam name="TDbContext"></typeparam>
11
- public abstract class IsolationDbContext < TDbContext , TKey > : MasaDbContext < TDbContext >
11
+ public abstract class IsolationDbContext < TDbContext , TKey > : IsolationDbContext < TKey >
12
12
where TKey : IComparable
13
13
where TDbContext : MasaDbContext , IMasaDbContext
14
14
{
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 )
26
16
{
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 ;
52
17
}
53
-
54
- protected virtual bool IsEnvironmentFilterEnabled => DataFilter ? . IsEnabled < IMultiEnvironment > ( ) ?? false ;
55
-
56
- protected virtual bool IsTenantFilterEnabled => DataFilter ? . IsEnabled < IMultiTenant < TKey > > ( ) ?? false ;
57
18
}
58
19
59
20
/// <summary>
@@ -76,7 +37,7 @@ public abstract class IsolationDbContext<TKey> : MasaDbContext
76
37
private readonly IMultiEnvironmentContext ? _environmentContext ;
77
38
private readonly IMultiTenantContext ? _tenantContext ;
78
39
79
- public IsolationDbContext ( MasaDbContextOptions options ) : base ( options )
40
+ protected IsolationDbContext ( MasaDbContextOptions options ) : base ( options )
80
41
{
81
42
_environmentContext = options . ServiceProvider ? . GetService < IMultiEnvironmentContext > ( ) ;
82
43
_tenantContext = options . ServiceProvider ? . GetService < IMultiTenantContext > ( ) ;
0 commit comments