question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Transient triggers should share state between its life-cycle implementations

See original GitHub issue

Currently 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:closed
  • Created 3 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
koenbeukcommented, Jul 21, 2021

@DavidErben Yes my bad, the default Lifetime for AddTrigger is Transient, not Scoped. I’ve updated my reply above. Thanks for pointing that out!

1reaction
koenbeukcommented, Jul 21, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found