Skip to content

Commit 9310db1

Browse files
(GH-594) Introduced new InterceptorsEnabled property to disable all interceptors
1 parent 70042df commit 9310db1

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

src/DotNetToolkit.Repository/IRepository.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
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+
2126
/// <summary>
2227
/// Returns a read-only <see cref="IReadOnlyRepository{TEntity, TKey1, TKey2, TKey3}" /> wrapper for the current repository.
2328
/// </summary>

src/DotNetToolkit.Repository/RepositoryBase.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,11 @@ 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+
12151220
/// <summary>
12161221
/// Gets or sets the value indicating whether caching is enabled or not.
12171222
/// </summary>
@@ -1261,6 +1266,8 @@ internal InternalRepositoryBase([NotNull] IRepositoryOptions options)
12611266

12621267
if (CacheProvider.GetType() != typeof(NullCacheProvider))
12631268
CacheEnabled = true;
1269+
1270+
InterceptorsEnabled = true;
12641271
}
12651272

12661273
#endregion
@@ -3046,6 +3053,9 @@ internal void Intercept([NotNull] Action<IRepositoryInterceptor> action)
30463053
{
30473054
Guard.NotNull(action, nameof(action));
30483055

3056+
if (!InterceptorsEnabled)
3057+
return;
3058+
30493059
foreach (var interceptor in GetInterceptors())
30503060
{
30513061
action(interceptor);
@@ -3056,6 +3066,9 @@ internal async Task InterceptAsync([NotNull] Func<IRepositoryInterceptor, Task>
30563066
{
30573067
Guard.NotNull(action, nameof(action));
30583068

3069+
if (!InterceptorsEnabled)
3070+
return;
3071+
30593072
foreach (var interceptor in GetInterceptors())
30603073
{
30613074
await action(interceptor);

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

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,35 @@ 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+
108137
[Fact]
109138
public async Task AddAsync()
110139
{
@@ -194,5 +223,34 @@ public async Task CanModifyUserOnInterceptionsAsync()
194223
Assert.Equal(user, entity.CreateUser);
195224
Assert.Equal(user, entity.ModUser);
196225
}
226+
227+
[Fact]
228+
public async Task CantModifyUserOnInterceptionsWhenDisabledAsync()
229+
{
230+
const string user = "Random User";
231+
232+
var entity = new CustomerWithTimeStamp();
233+
var options = new RepositoryOptionsBuilder()
234+
.UseInMemoryDatabase()
235+
.UseInterceptor(new TestRepositoryTimeStampInterceptor(user))
236+
.Options;
237+
238+
var repo = new Repository<CustomerWithTimeStamp>(options)
239+
{
240+
InterceptorsEnabled = false
241+
};
242+
243+
Assert.Null(entity.CreateTime);
244+
Assert.Null(entity.ModTime);
245+
Assert.Null(entity.CreateUser);
246+
Assert.Null(entity.ModUser);
247+
248+
await repo.AddAsync(entity);
249+
250+
Assert.Null(entity.CreateTime);
251+
Assert.Null(entity.ModTime);
252+
Assert.Null(entity.CreateUser);
253+
Assert.Null(entity.ModUser);
254+
}
197255
}
198-
}
256+
}

0 commit comments

Comments
 (0)