Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EntityFrameworkCore.Triggered in the tools & extensions list #2913

Closed
koenbeuk opened this issue Nov 28, 2020 · 3 comments
Closed

Add EntityFrameworkCore.Triggered in the tools & extensions list #2913

koenbeuk opened this issue Nov 28, 2020 · 3 comments

Comments

@koenbeuk
Copy link
Contributor

I've recently published v2 of a project called EntityFrameworkCore.Triggered which lets you code triggers (similar to SQL Server triggers) within an EF domain. It allows you to run arbitrary code in response to changes within your SaveChanges cycle (both before and after SaveChanges happened). Its somewhat similar to Ramses which is already listed in the docs, but on steroids, as it supports full DI integration, Async out of the box, as well as recursion (a BeforeSave trigger could modify the ChangeSet which in return would invoke additional triggers). Given that the plugin is designed to solely add-on to the foundation of EFCore while minimising the number of new concepts introduced, I think its a fair candidate to be noted in the docs. Please consider adding it and driving traffic to the project.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@roji
Copy link
Member

roji commented Nov 29, 2020

Out of curiosity, how would you compare this to the SaveChanges events and interceptors introduced in EF Core 5.0?

@koenbeuk
Copy link
Contributor Author

koenbeuk commented Nov 29, 2020

@roji
In short: SaveChanges interception intercepts calls to SaveChanges whereas triggers Intercepts changes on individual entities.

Triggers are fundamentally higher up the stack. They are individual units of work that are strongly typed and are invoked for specific entities and change types (e.g. intercepting a modified Customer entity). Interceptors are much lower in the stack and a more generalised driver invoked for calls To SaveChanges. In fact, since V2 of this project, Triggers are built on top of SaveChanges interception, before (v1, targeting EFCore 3.1 required overriding SaveChanges/SaveChangesAsync or implementing the TriggeredDbContext).

Some advantages of triggers over SaveChanges interception:

  • There is no easy DI solution for SaveChanges events/interceptor. Interceptors are resolved through EF's internal DI container which has no access to get a hold of your user services.
  • Interceptors are always called, Triggers are called only if there is a type match (IBeforeSaveTrigger< Customer> will only be invoked for added/modified/removed customers). The trigger context then contains information about the current state of the entity, the change type as well as in case of a Modified entity, the state of the entity before that change. To get this to work manually with SaveChangesInterceptors is a bit of a hassle. (Triggers can also listen for base types or interfaces).
  • Interceptors run once for each SaveChanges lifecycle while triggers run recursively (A IBeforeSaveTrigger may change the context further (E.g. add additional entities)), which in turn will recursively raise triggers interested in that change.
  • Triggers can be registered and are invoked in a predictable order so that you can ensure that one trigger runs before another trigger.
  • Triggers go beyond simply hooking into SaveChanges as there are also extensions for Transaction triggers (e.g. I[Before/After]CommitTrigger).

Perhaps a good example for seeing the power of triggers: https://github.com/koenbeuk/EntityFrameworkCore.Triggered/tree/master/samples/v2/3%20-%20StudentManagerAspNetCore

@ajcvickers
Copy link
Member

@koenbeuk Feel free to submit a PR for this. We don't typically add these things ourselves, we just take PRs from the community. Make sure to keep the formatting. etc. consistent with other extensions in the page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants