Skip to content

Commit 357f62b

Browse files
committed
Refactored TriggerConfigurations
1 parent 5a4654c commit 357f62b

5 files changed

Lines changed: 58 additions & 23 deletions

File tree

src/EntityFrameworkCore.Triggered/Extensions/DbContextExtensions.cs

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,43 @@ public static ITriggerSession CreateTriggerSession(this DbContext dbContext, ISe
3232
}
3333

3434
/// <summary>
35-
/// Calls dbContext.SaveChanges without invoking triggers
35+
/// Creates a <c>ITriggerSession</c> that can be used to manually invoke triggers
3636
/// </summary>
37-
public static int SaveChangesWithoutTriggers(this DbContext dbContext, bool acceptAllChangesOnSuccess = true)
37+
public static ITriggerSession CreateTriggerSession(this DbContext dbContext, Func<TriggerConfiguration, TriggerConfiguration> configurator, IServiceProvider? serviceProvider = null)
3838
{
3939
var triggerService = GetTriggerService(dbContext);
40-
var initialConfiguration = triggerService.Configuration;
40+
var configuration = configurator(triggerService.Configuration);
4141

42-
try
43-
{
44-
triggerService.Configuration = initialConfiguration with { Disabled = true };
42+
return triggerService.CreateSession(dbContext, configuration, serviceProvider);
43+
}
4544

46-
return dbContext.SaveChanges(acceptAllChangesOnSuccess);
47-
}
48-
finally
45+
46+
/// <summary>
47+
/// Creates a new <c>ITriggerSession</c> that can be used to manually invoke triggers. Throws if a TriggerSession is already active
48+
/// </summary>
49+
public static ITriggerSession CreateNewTriggerSession(this DbContext dbContext, Func<TriggerConfiguration, TriggerConfiguration>? configurator = null, IServiceProvider? serviceProvider = null)
50+
{
51+
var triggerService = GetTriggerService(dbContext);
52+
if (triggerService.Current is not null)
4953
{
50-
triggerService.Configuration = initialConfiguration;
54+
throw new InvalidOperationException("A triggerSession has already been created");
5155
}
56+
57+
var configuration = configurator?.Invoke(triggerService.Configuration) ?? triggerService.Configuration;
58+
59+
return triggerService.CreateSession(dbContext, configuration, serviceProvider);
60+
}
61+
62+
/// <summary>
63+
/// Calls dbContext.SaveChanges without invoking triggers
64+
/// </summary>
65+
public static int SaveChangesWithoutTriggers(this DbContext dbContext, bool acceptAllChangesOnSuccess = true)
66+
{
67+
CreateNewTriggerSession(dbContext, configuration => configuration with {
68+
Disabled = true
69+
});
70+
71+
return dbContext.SaveChanges(acceptAllChangesOnSuccess);
5272
}
5373

5474
/// <summary>
@@ -62,19 +82,11 @@ public static Task<int> SaveChangesWithoutTriggersAsync(this DbContext dbContext
6282
/// </summary>
6383
public static Task<int> SaveChangesWithoutTriggersAsync(this DbContext dbContext, bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
6484
{
65-
var triggerService = GetTriggerService(dbContext);
66-
var initialConfiguration = triggerService.Configuration;
67-
68-
try
69-
{
70-
triggerService.Configuration = initialConfiguration with { Disabled = true };
85+
CreateNewTriggerSession(dbContext, configuration => configuration with {
86+
Disabled = true
87+
});
7188

72-
return dbContext.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
73-
}
74-
finally
75-
{
76-
triggerService.Configuration = initialConfiguration;
77-
}
89+
return dbContext.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
7890
}
7991
}
8092
}

src/EntityFrameworkCore.Triggered/ITriggerService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public interface ITriggerService
99

1010
ITriggerSession CreateSession(DbContext context, IServiceProvider? serviceProvider = null);
1111

12+
ITriggerSession CreateSession(DbContext context, TriggerConfiguration configuration, IServiceProvider? serviceProvider = null);
13+
1214
ITriggerSession? Current { get; set; }
1315
}
1416
}

src/EntityFrameworkCore.Triggered/TriggerService.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public ITriggerSession? Current
3939
public TriggerConfiguration Configuration { get; set; }
4040

4141
public ITriggerSession CreateSession(DbContext context, IServiceProvider? serviceProvider)
42+
=> CreateSession(context, Configuration, serviceProvider);
43+
44+
public ITriggerSession CreateSession(DbContext context, TriggerConfiguration configuration, IServiceProvider? serviceProvider)
4245
{
4346
if (context is null)
4447
{
@@ -52,7 +55,7 @@ public ITriggerSession CreateSession(DbContext context, IServiceProvider? servic
5255
_triggerDiscoveryService.ServiceProvider = serviceProvider;
5356
}
5457

55-
var triggerSession = new TriggerSession(this, Configuration, _triggerDiscoveryService, triggerContextTracker, _loggerFactory.CreateLogger<TriggerSession>());
58+
var triggerSession = new TriggerSession(this, configuration, _triggerDiscoveryService, triggerContextTracker, _loggerFactory.CreateLogger<TriggerSession>());
5659

5760
_currentTriggerSession = triggerSession;
5861

test/EntityFrameworkCore.Triggered.Tests/Extensions/DbContextExtensionTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,21 @@ public async Task SaveChangesAsyncWithoutTriggers_RestoresConfiguration()
135135
// assert
136136
Assert.Same(expectedConfiguration, context.GetTriggerService().Configuration);
137137
}
138+
139+
140+
[Fact]
141+
public void CreateNewTriggerSession_ExistingSession_Throws()
142+
{
143+
// arrange
144+
using var context = new TestDbContext();
145+
context.TestModels.Add(new TestModel { });
146+
context.CreateNewTriggerSession();
147+
148+
// act
149+
Assert.Throws<InvalidOperationException>(() =>
150+
context.CreateNewTriggerSession());
151+
}
152+
153+
138154
}
139155
}

test/EntityFrameworkCore.Triggered.Tests/Stubs/TriggerServiceStub.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ public ITriggerSession CreateSession(DbContext context, IServiceProvider service
2121
Current = LastSession;
2222
return LastSession;
2323
}
24+
25+
public ITriggerSession CreateSession(DbContext context, TriggerConfiguration configuration, IServiceProvider serviceProvider = null) => throw new NotImplementedException();
2426
}
2527
}

0 commit comments

Comments
 (0)