Skip to content
Jon P Smith edited this page Jan 21, 2019 · 2 revisions

GenericBizRunner is best used with Dependency Injection (DI), but can used in non-DI (see end of this page)
In this section I deal with.

  • Registering GenericBizRunner with ASP.NET DI
  • Registering your business logic with DI

Using DI

QUICK START - ASP.NET Core code examples

See ConfigureService method in Startup class. This shows registering the GenericBizRunner library and registering your business logic.

Registering GenericBizRunner with ASP.NET DI

  1. The default registering extension method is RegisterBizRunnerWithDtoScans<TDefaultDbContext> method which works if you have one application DbContext.
  2. The other method RegisterBizRunnerMultiDbContextWithDtoScans is for cases where you have multiple application DbContexts. In that case you will use the IActionService<YourContext, IYourBizLogic>. That allows you to choose which DbContext should be used by the BizRunner.

In both cases you need to provide a set of Assemblies to scan for DTOs that inherit from the GenericActionToBizDto/Async or GenericActionFromBizDto\Async class. Typically you will only have one assembly (I use the ServiceLayer), but the methods take in multiple assemblies if you need that. Here is code taken from the ExampleWebApp in the EfCore.GenericBizRunner repo.

services.RegisterBizRunnerWithDtoScans<EfCoreContext>(Assembly.GetAssembly(typeof(WebChangeDeliveryDto)));

Registering your business logic with DI

Your business logic should have a interface (see Your Business Logic), and you need to register that interface with the DI. You can write code to do this but I recommend my NetCore.AutoRegisterDi library which can scan assemblies for all public classes and registers any that have a public interface. See that project for documentation on how it works.

Non-DI approach

You can use GenericBizRunner without DI. What you need to do is use the NonDiBizSetup class. There are a number of methods to help you and below is one example:

Setup stage

var config = new GenericBizRunnerConfig { ... }; //Optional config. Leave out is not required
var nonDiData = NonDiBizSetup.SetupDtoMapping<ServiceLayerBizInDto>(config);
nonDiData .AddDtoMapping<ServiceLayerBizOutDto>();
//... add all the DTOs you are going to use

Run stage

var bizInstance = new YourBizAction(yourDbContext);
var runner = new ActionService<IYourBizAction>(
    yourDbContext, bizInstance, nonDiData.WrappedConfig);

var input = new ServiceLayerBizInDto{ num = 123};
var result= runner.RunBizAction<ServiceLayerBizOutDto>(input);
//... your code after 

I suggest looking at the NonDiSetup class for the list of methods you can use, and any of these unit tests in this directory for examples of the different options for setup.

Clone this wiki locally