-
Notifications
You must be signed in to change notification settings - Fork 12
Configuration options
EfCore.GenericEventRunner has a EfCore.GenericEventRunnerConfig class with lots of useful parts to it. I'm not going to describe every one, as the comments in the class does that, but here is a summary of the things you can change.
You can register a method which will be called if there is a exception returned by SaveChanges/SaveChangesAsync. See article EF Core – validating data and catching SQL errors on how to use this. The code below registers a method called CatchAndFixConcurrencyException to the DbContext type ExampleDbContext.
var config = new GenericEventRunnerConfig();
config.RegisterSaveChangesExceptionHandler<ExampleDbContext>(CatchAndFixConcurrencyException);
var context = options.CreateAndSeedDbWithDiForHandlers<OrderCreatedHandler>( config: config);
//...Often you want to add code to detect changes and do something to an entity, e.g. adding a LastUpdated DateTime to every entity class that has been changed. You can register your method here and it will be called after DetectChanges is called but before SaveChanges/SaveChangesAsync have been called. The code below registers a static method called BookDetectChangesExtensions.ChangeChecker to the DbContext type BookDbContext.
var eventConfig = new GenericEventRunnerConfig();
eventConfig.AddActionToRunAfterDetectChanges<BookDbContext>(BookDetectChangesExtensions.ChangeChecker);
var logs = services.RegisterGenericEventRunner(eventConfig,
Assembly.GetAssembly(typeof(ReviewAddedHandler)), //SQL cached values event handlers
Assembly.GetAssembly(typeof(BookChangeHandlerAsync)) //Cosmos Db event handlers
);By default the first Before event handler that returns a status with errors immediately return that error, e.g. if calling SaveChanges/SaveChangesAsync it will throw an GenericEventRunnerStatusException. You can set a property to false and it will run ALL the Before event handlers before returning the error. Useful if you want to get all the errors to return them to the user. Example below:
var eventConfig = new GenericEventRunnerConfig
{
StopOnFirstBeforeHandlerThatHasAnError = false //The default is true
};You can turn off During or After events/handler if you don't what them - just set the properties to true. But these two properties will be set to true if it doesn't find any During or After event handlers. That makes the code quicker.
If you manually want to turn them off (maybe to unit test??) you can
var eventConfig = new GenericEventRunnerConfig
{
NotUsingDuringSaveHandlers = true, //If not set, then set to true if no During event handlers found
NotUsingAfterSaveHandlers = //...
};Before events can create new Before events. That can be useful, but you could create a circular system where it won't stop. The library counts the number of this it has been around and it it exceeds the MaxTimesToLookForBeforeEvents property (default is 6) it will throw an exception.