Skip to content

Setup: App startup

Jon P Smith edited this page Nov 10, 2020 · 2 revisions

What to run when your application starts

When your application starts EFCore.GenericEventRunner needs to find and register your event handlers and its EventRunner class with NET Core dependency injection (DI) provider. You tell the method which assembles in your application and it then scans each assembles for event handlers and registers then with the DI provider.

There are two options, one without extra configuration and one with configuration.

1. Register without configuration

You provider a series of assemblies to scan. The easier way to do this is use Assembly.GetAssembly(<Type>). e.g.

var logs = services.RegisterGenericEventRunner(  //"var logs = " is optional - see note below.
    Assembly.GetAssembly(typeof(ReviewAddedHandler)),
    Assembly.GetAssembly(typeof(CosmasUpdaterHandler))
    );

NOTES

  • If you don't provide any assemblies, then it scans the assembly that called the RegisterGenericEventRunner extension method.
  • The logs are optional, but the provide a list of what it found and registered. That's useful for debugging issues.

2. Register with configuration

This is very similar to the non-config version, but the first parameter should now be of the type IGenericEventRunnerConfig. Here is an example of using the GenericEventRunnerConfig class to register a exception handler for a class called BookDbContext.

var eventConfig = new GenericEventRunnerConfig();
eventConfig.RegisterSaveChangesExceptionHandler<BookDbContext>(
    BookWithEventsConcurrencyHandler.HandleCacheValuesConcurrency);

var logs = services.RegisterGenericEventRunner(  //"var logs = " is optional - see note above.
    eventConfig,                //Adds configuration to GenericEventRunner
    Assembly.GetAssembly(typeof(ReviewAddedHandler)),
    Assembly.GetAssembly(typeof(CosmasUpdaterHandler))
    );

Clone this wiki locally