Skip to content

QUICK START guide

Jon P Smith edited this page Jan 21, 2019 · 10 revisions

Quick start on GenericBizRunner

This details all the steps you need to do to use the GenericBizRunner library.

NOTE: There is a much longer article called A library to run your business logic when using Entity Framework Core which gives a more detailed account of how to use the GenericBizRunner library, with examples.

Assumed architecture

You can use any architecture you like, but in this quick start page I will refer to project/assembly by the names shown in this diagram Layered Architecture

1. Install the following NuGet packages

  1. EfCore.GenericBizRunner in the following projects/assemblies:
    1. Bizlogic: The project/assembly(s) that hold your business logic.
    2. Presentation: The ASP.NET Core project/assembly (for setup).
    3. ServiceLayer: The project/assembly(s) where you are going to create presentation-focused DTOs (see Anti-corruption feature).
  2. Presentation: You most likely will be using dependency injection to link the projects/assemblies. You need to register GenericBizRunner.

2. Build your business logic using GenericBizRunner pattern (BizLogic Layer)

Your business logic has to:

  1. Implement the IBizActionStatus interface, which you can do by inheriting the BizActionStatus abstract class
  2. It should have an interface for DI, and that interface should inherit from one of the 12 GenericBizRunner's IGenericAction??? interface (see What your interface looks like).

See Writing your Business Logic for more.

3. Register everything into the Dependency Injection (DI) container

Here are the steps for registering GenericBizRunner with .NET Core's DI provider. Typically this is done at startup - in an ASP.NET Core applications your code would go in the ConfigureServices method in the Startup class (see Startup class in the ExampleWebApp for example).

Registering with .NET Core DI container

  1. Register GenericBizRunner, which will also scan the assemblies that contain your GenericBizRunner DTOs. There are a few options, but here is a typical format.
services.RegisterBizRunnerWithDtoScans<YourDbContext>(
    Assembly.GetAssembly(typeof(OneOfYourBizDTO)));
  1. Register your business logic. I recommend using the library NetCore.AutoRegisterDi (see link to example code above).

See the Setup Wiki page for more. NOTE: GenericBizRunner can be used in a non-DI situation too.

4. Create a BizRunner with your business logic (Presentation layer)

In places where you can inject via DI then if you wanted to run an synchronous business logic who's interface was IMyBizLogic, then you would use this signature: IActionService<IMyBizLogic>.
For instance, if you were using an ASP.NET Core Action method inside a controller you could inject the BizRunner service using the [FromService] in the actions' parameter, e.g.

public IActionResult PlaceOrder(PlaceOrderInDto dto, 
    [FromServices]IActionService<IMyBizLogic> service)

5. Running the business logic (Presentation layer)

Assuming you have a variable called service with an instance of the BizRunner service, as setup by the last step, then you can run it by calling.

var order = service.RunBizAction<MyOutputClass>(myInputDtoInstance);

Or for async

var order = await service.RunBizActionAsync<MyOutputClass>(myInputDtoInstance);

Note: there are different versions of the RunBizAction depending on whether the business logic has an input and/or an output. There are also async versions of the BizRunner service, e.g. IActionServiceAsync<IMyBizLogicAsync>.

You can find out if the business logic ran successfully by testing the service.Status.HasErrors boolean property. You can access any errors via the service.Status.Errors property. if the business logic ran successfully the service.Status.Message will contain a sensible success message.

See Running your business logic for more.