Skip to content

Commit 6eb82f2

Browse files
Merge pull request #595 from johelvisguzman/GH-594
GH-594 Allow ability to disable interceptors
2 parents 70042df + cfbca79 commit 6eb82f2

File tree

3 files changed

+162
-3
lines changed

3 files changed

+162
-3
lines changed

src/DotNetToolkit.Repository/IRepository.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818
/// <seealso cref="IReadOnlyRepository{TEntity, TKey1, TKey2, TKey3}" />
1919
public interface IRepository<TEntity, in TKey1, in TKey2, in TKey3> : IReadOnlyRepository<TEntity, TKey1, TKey2, TKey3> where TEntity : class
2020
{
21+
/// <summary>
22+
/// Gets or sets the value indicating whether all the repository interceptors should be enabled or not.
23+
/// </summary>
24+
bool InterceptorsEnabled { get; set; }
25+
26+
/// <summary>
27+
/// Gets a dictionary for indicating whether the repository interceptor of the specified type should be disabled.
28+
/// </summary>
29+
Dictionary<Type, bool> InterceptorTypesDisabled { get; }
30+
2131
/// <summary>
2232
/// Returns a read-only <see cref="IReadOnlyRepository{TEntity, TKey1, TKey2, TKey3}" /> wrapper for the current repository.
2333
/// </summary>

src/DotNetToolkit.Repository/RepositoryBase.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,16 @@ public abstract class InternalRepositoryBase<TEntity> where TEntity : class
12121212

12131213
#region Properties
12141214

1215+
/// <summary>
1216+
/// Gets or sets the value indicating whether all the repository interceptors should be enabled or not.
1217+
/// </summary>
1218+
public bool InterceptorsEnabled { get; set; }
1219+
1220+
/// <summary>
1221+
/// Gets a dictionary for indicating whether the repository interceptor of the specified type should be disabled.
1222+
/// </summary>
1223+
public Dictionary<Type, bool> InterceptorTypesDisabled { get; }
1224+
12151225
/// <summary>
12161226
/// Gets or sets the value indicating whether caching is enabled or not.
12171227
/// </summary>
@@ -1261,6 +1271,9 @@ internal InternalRepositoryBase([NotNull] IRepositoryOptions options)
12611271

12621272
if (CacheProvider.GetType() != typeof(NullCacheProvider))
12631273
CacheEnabled = true;
1274+
1275+
InterceptorsEnabled = true;
1276+
InterceptorTypesDisabled = new Dictionary<Type, bool>();
12641277
}
12651278

12661279
#endregion
@@ -3046,19 +3059,35 @@ internal void Intercept([NotNull] Action<IRepositoryInterceptor> action)
30463059
{
30473060
Guard.NotNull(action, nameof(action));
30483061

3062+
if (!InterceptorsEnabled)
3063+
return;
3064+
30493065
foreach (var interceptor in GetInterceptors())
30503066
{
3051-
action(interceptor);
3067+
var interceptorType = interceptor.GetType();
3068+
var isEnabled = InterceptorTypesDisabled.ContainsKey(interceptorType)
3069+
? !InterceptorTypesDisabled[interceptorType]
3070+
: true;
3071+
3072+
if (isEnabled) action(interceptor);
30523073
}
30533074
}
30543075

30553076
internal async Task InterceptAsync([NotNull] Func<IRepositoryInterceptor, Task> action)
30563077
{
30573078
Guard.NotNull(action, nameof(action));
30583079

3080+
if (!InterceptorsEnabled)
3081+
return;
3082+
30593083
foreach (var interceptor in GetInterceptors())
30603084
{
3061-
await action(interceptor);
3085+
var interceptorType = interceptor.GetType();
3086+
var isEnabled = InterceptorTypesDisabled.ContainsKey(interceptorType)
3087+
? !InterceptorTypesDisabled[interceptorType]
3088+
: true;
3089+
3090+
if (isEnabled) await action(interceptor);
30623091
}
30633092
}
30643093

test/DotNetToolkit.Repository.Integration.Test/Tests/Interceptor/RepositoryInterceptorTests.cs

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,66 @@ public void CanModifyUserOnInterceptions()
105105
Assert.Equal(user, entity.ModUser);
106106
}
107107

108+
[Fact]
109+
public void CantModifyUserOnInterceptionsWhenDisabled()
110+
{
111+
const string user = "Random User";
112+
113+
var entity = new CustomerWithTimeStamp();
114+
var options = new RepositoryOptionsBuilder()
115+
.UseInMemoryDatabase()
116+
.UseInterceptor(new TestRepositoryTimeStampInterceptor(user))
117+
.Options;
118+
119+
var repo = new Repository<CustomerWithTimeStamp>(options)
120+
{
121+
InterceptorsEnabled = false
122+
};
123+
124+
Assert.Null(entity.CreateTime);
125+
Assert.Null(entity.ModTime);
126+
Assert.Null(entity.CreateUser);
127+
Assert.Null(entity.ModUser);
128+
129+
repo.Add(entity);
130+
131+
Assert.Null(entity.CreateTime);
132+
Assert.Null(entity.ModTime);
133+
Assert.Null(entity.CreateUser);
134+
Assert.Null(entity.ModUser);
135+
}
136+
137+
[Fact]
138+
public void CantModifyUserOnInterceptionsWhenDisabledByType()
139+
{
140+
const string user = "Random User";
141+
142+
var entity = new CustomerWithTimeStamp();
143+
var options = new RepositoryOptionsBuilder()
144+
.UseInMemoryDatabase()
145+
.UseInterceptor(new TestRepositoryTimeStampInterceptor(user))
146+
.Options;
147+
148+
var repo = new Repository<CustomerWithTimeStamp>(options)
149+
{
150+
InterceptorsEnabled = true
151+
};
152+
153+
repo.InterceptorTypesDisabled.Add(typeof(TestRepositoryTimeStampInterceptor), true);
154+
155+
Assert.Null(entity.CreateTime);
156+
Assert.Null(entity.ModTime);
157+
Assert.Null(entity.CreateUser);
158+
Assert.Null(entity.ModUser);
159+
160+
repo.Add(entity);
161+
162+
Assert.Null(entity.CreateTime);
163+
Assert.Null(entity.ModTime);
164+
Assert.Null(entity.CreateUser);
165+
Assert.Null(entity.ModUser);
166+
}
167+
108168
[Fact]
109169
public async Task AddAsync()
110170
{
@@ -194,5 +254,65 @@ public async Task CanModifyUserOnInterceptionsAsync()
194254
Assert.Equal(user, entity.CreateUser);
195255
Assert.Equal(user, entity.ModUser);
196256
}
257+
258+
[Fact]
259+
public async Task CantModifyUserOnInterceptionsWhenDisabledAsync()
260+
{
261+
const string user = "Random User";
262+
263+
var entity = new CustomerWithTimeStamp();
264+
var options = new RepositoryOptionsBuilder()
265+
.UseInMemoryDatabase()
266+
.UseInterceptor(new TestRepositoryTimeStampInterceptor(user))
267+
.Options;
268+
269+
var repo = new Repository<CustomerWithTimeStamp>(options)
270+
{
271+
InterceptorsEnabled = false
272+
};
273+
274+
Assert.Null(entity.CreateTime);
275+
Assert.Null(entity.ModTime);
276+
Assert.Null(entity.CreateUser);
277+
Assert.Null(entity.ModUser);
278+
279+
await repo.AddAsync(entity);
280+
281+
Assert.Null(entity.CreateTime);
282+
Assert.Null(entity.ModTime);
283+
Assert.Null(entity.CreateUser);
284+
Assert.Null(entity.ModUser);
285+
}
286+
287+
[Fact]
288+
public async Task CantModifyUserOnInterceptionsWhenDisabledByTypeAsync()
289+
{
290+
const string user = "Random User";
291+
292+
var entity = new CustomerWithTimeStamp();
293+
var options = new RepositoryOptionsBuilder()
294+
.UseInMemoryDatabase()
295+
.UseInterceptor(new TestRepositoryTimeStampInterceptor(user))
296+
.Options;
297+
298+
var repo = new Repository<CustomerWithTimeStamp>(options)
299+
{
300+
InterceptorsEnabled = true
301+
};
302+
303+
repo.InterceptorTypesDisabled.Add(typeof(TestRepositoryTimeStampInterceptor), true);
304+
305+
Assert.Null(entity.CreateTime);
306+
Assert.Null(entity.ModTime);
307+
Assert.Null(entity.CreateUser);
308+
Assert.Null(entity.ModUser);
309+
310+
await repo.AddAsync(entity);
311+
312+
Assert.Null(entity.CreateTime);
313+
Assert.Null(entity.ModTime);
314+
Assert.Null(entity.CreateUser);
315+
Assert.Null(entity.ModUser);
316+
}
197317
}
198-
}
318+
}

0 commit comments

Comments
 (0)