Skip to content

Commit aa7c24a

Browse files
committed
2 parents e099b13 + 29b2302 commit aa7c24a

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

README.md

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Triggers for EF Core. Respond to changes in your DbContext before and after they
1414
## Getting started
1515
1. Install the package from [NuGet](https://www.nuget.org/packages/EntityFrameworkCore.Triggered)
1616
2. Implement Triggers by implementing `IBeforeSaveTrigger<TEntity>` and `IAfterSaveTrigger<TEntity>`
17-
3. Register your triggers with Dependency Injection (Or [read on](when-you-dont-want-to-use-dependeny-injection) if you don't want to use DI)
17+
3. Register your triggers with your DbContext
1818
4. View our [samples](https://github.com/koenbeuk/EntityFrameworkCore.Triggered/tree/master/samples)
1919
5. Check out our [wiki](https://github.com/koenbeuk/EntityFrameworkCore.Triggered/wiki) for tips and tricks on getting started and being succesfull.
2020

@@ -82,15 +82,40 @@ public class Startup
8282
{
8383
services.AddSingleton<EmailService>();
8484
services
85-
.AddTriggeredDbContext<ApplicationContext>()
86-
.AddScoped<IBeforeSaveTrigger<Course>, BeforeSaveStudentTrigger>()
85+
.AddDbContext<ApplicationContext>(options => {
86+
options.UseTriggers(triggerOptions => {
87+
triggerOptions.AddTrigger<StudentSignupTrigger>();
88+
triggerOptions.AddTrigger<SendEmailTrigger>();
89+
});
90+
})
91+
.AddTransient<IEmailService, MyEmailService>();
8792
}
8893

8994
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
9095
{ ... }
9196
}
9297
```
9398

99+
### Trigger discovery
100+
In the given example, we register triggers directly with our DbContext. This is the recommended approach starting from version 2.3 and 1.4 respectively. If you're on an older version then its recommend to register triggers with your applications DI container instead:
101+
102+
```csharp
103+
services
104+
.AddDbContext<ApplicationContext>(options => options.UseTriggers())
105+
.AddTransient<IBeforeSaveTrigger<User>, MyBeforeSaveTrigger<User>>();
106+
```
107+
108+
Doing so will make sure that your triggers can use other services registered in your DI container.
109+
110+
You can also use functionality in [EntityFrameworkCore.Triggered.Extensions](https://www.nuget.org/packages/EntityFrameworkCore.Triggered.Extensions/) which allows you to discover triggers that are part of an assembly:
111+
112+
```csharp
113+
services.AddDbContext<ApplicationContext>(options => options.UseTriggers(triggerOptions => triggerOptions.AddAssemblyTriggers()));
114+
// or alternatively
115+
services.AddAssemblyTriggers();
116+
```
117+
118+
94119
### Cascading changes (previously called Recursion)
95120
`BeforeSaveTrigger<TEntity>` supports cascading triggers. This is useful since it allows your triggers to subsequently modify the same DbContext entity graph and have it raise additional triggers. By default this behavior is turned on and protected from infinite loops by limiting the number of cascading cycles. If you don't like this behavior or want to change it, you can do so by:
96121
```csharp
@@ -169,23 +194,6 @@ In this example we were not able to inherit from TriggeredDbContext since we wan
169194
### Custom trigger types
170195
By default we offer 3 trigger types: `IBeforeSaveTrigger`, `IAfterSaveTrigger` and `IAfterSaveFailedTrigger`. These will cover most cases. In addition we offer `IRaiseBeforeCommitTrigger` and `IRaiseAfterCommitTrigger` as an extension to further enhance your control of when triggers should run. We also offer support for custom triggers. Lets say we want to react to specific events happening in your context. We can do so by creating a new interface: IThisThingJustHappenedTrigger and implementing an extension method for ITriggerSession to invoke triggers of that type. Please take a look at how [Transactional triggers](https://github.com/koenbeuk/EntityFrameworkCore.Triggered/tree/master/src/EntityFrameworkCore.Triggered.Transactions) are implemented as an example.
171196
172-
### When you don't want to use dependency injection
173-
```csharp
174-
175-
public class ApplicationDbContext : DbContext {
176-
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
177-
{
178-
optionsBuilder
179-
// Enable triggers
180-
.UseTriggers(triggerOptions => {
181-
triggerOptions.AddTrigger<BeforeSaveStudentTrigger>(); // Register your triggers
182-
});
183-
184-
base.OnConfiguring(optionsBuilder);
185-
}
186-
}
187-
```
188-
189197
### Similar products
190198
- [Ramses](https://github.com/JValck/Ramses): Lifecycle hooks for EFCore. A simple yet effective way of reacting to changes. Great for situations where you simply want to make sure that a property is set before saving to the database. Limited though in features as there is no dependency injection, no async support, no extensibility model and lifecycle hooks need to be implemented on the entity type itself.
191199
- [EntityFramework.Triggers](https://github.com/NickStrupat/EntityFramework.Triggers). Add triggers to your entities with insert, update, and delete events. There are three events for each: before, after, and upon failure. A fine alternative to EntityFrameworkCore.Triggered. It has been around for some time and has support for EF6 and boast a decent community. There are plenty of trigger types to opt into including the option to cancel SaveChanges from within a trigger. A big drawback however is that it does not support cascading triggers so that triggers can never be relied on to enforce a domain constraint.

0 commit comments

Comments
 (0)