Transient triggers should share state between its life-cycle implementations
See original GitHub issueCurrently transient triggers do not share state between the different lifecycles, e.g;
class MyTrigger : IBeforeSaveTrigger<object>, IAfterSaveTrigger<object> {
int _counter;
public Task BeforeSave(ITriggerContext<object> context, CancellationToken _) {
_counter += 1;
return Task.CompletedTask;
}
public Task AfterSave(ITriggerContext<object> context, CancellationToken _) {
Console.WriteLine($"Counter: {_counter}");
return Task.CompletedTask;
}
}
Will always print 0 when registered as a transient trigger since state is not shared between the 2 lifecycles. If we were to register the trigger as a Scoped trigger then the counter may be > 1 if there are multiple entities within the changeset.
It makes sense to have transient triggers still share the same state so that a Trigger is only transient per unique Entity/Trigger, not per unique Entity/Lifecyle as its currently.
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (7 by maintainers)
Top Results From Across the Web
When to use following Transient, scoped and singleton
A transient lifetime services are created each time they're requested from the service container.
Read more >Activity state changes
Different events, some user-triggered and some system-triggered, can cause an Activity to transition from one state to another.
Read more >Dependency injection guidelines - .NET
When designing services for dependency injection: Avoid stateful, static classes and members. Avoid creating global state by designing apps ...
Read more >1 Year of Event Sourcing and CQRS | by Teiva Harsanyi
I have been working on implementing an application based on CQRS and Event Sourcing principles for about one year. This post is a...
Read more >Use State Machines! - Richard Clayton - Silvrback
The lifecycle is defined by an enumerated set of states known at the time of implementation (this is where the term "finite" comes...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@DavidErben Yes my bad, the default Lifetime for AddTrigger is Transient, not Scoped. I’ve updated my reply above. Thanks for pointing that out!
I have a hypothetical fix for this issue implemented in #115
This exposes a new EntityBag property on the TriggerContext which in turn can be used to associate additional items with the entity for the duration of the Trigger session.
A sample usage is given here: https://github.com/koenbeuk/EntityFrameworkCore.Triggered/blob/trigger-entity-state-management/test/EntityFrameworkCore.Triggered.IntegrationTests/EntityBags/EntityBagsTestScenario.cs
Feedback is welcome