EfCore.GenericBizRunner 3.0.0

Library to run business logic when using Entity Framework Core for database accesses

Install-Package EfCore.GenericBizRunner -Version 3.0.0
dotnet add package EfCore.GenericBizRunner --version 3.0.0
<PackageReference Include="EfCore.GenericBizRunner" Version="3.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EfCore.GenericBizRunner --version 3.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

EfCore.GenericBizRunner

EfCore.GenericBizRunner (shortened to GenericBizRunner) is a framework to help build and run business logic when you are using Entity Framework Core for database accesses. Its aim is to totally isolate the business logic from other parts of the application, especially the user presentation/UI layers. It provides the following features:

  • A standard pattern for writing business logic, including helper classes.
  • An anti-corruption layer feature that act as a barrier between the business logic and the user presentation/UI layers.
  • The BizRunner handles the call to EF Core's SaveChanges, with optional validation.
  • A service, known as a BizRunner, that runs your business logic.
  • Very good use of Dependency Injection (DI), making calls to business logic very easy.

EfCore.GenericBizRunner is available as a NuGet package, and on the EfCore.GenericBizRunner GitHub repo. It is an open-source project under the MIT license.

NOTE: Version 3 of GenericBizRunner changed the way it handles DTOs to make the library more useful in Web APIs - see upgrade guide.

Example of using GenericBizRunner in ASP.NET Core MVC

Here is some code taken from the ExampleWebApp (which you can run), from the OrdersController
to give you an idea of what it looks like. Note that every business logic call is very similar, just different interfaces and DTO/ViewModel classes.

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult ChangeDelivery(WebChangeDeliveryDto dto,
    [FromServices]IActionService<IChangeDeliverAction> service)
{
    if (!ModelState.IsValid)
    {
        service.ResetDto(dto); //resets any dropdown list etc.
        return View(dto);
    }

    service.RunBizAction(dto);

    if (!service.Status.HasErrors)
    {
        //We copy the message from the business logic to show 
        return RedirectToAction("ConfirmOrder", "Orders", 
            new { dto.OrderId, message = service.Status.Message });
    }

    //Otherwise errors, so I need to redisplay the page to the user
    service.Status.CopyErrorsToModelState(ModelState, dto);
    service.ResetDto(dto); //resets any dropdown list etc.
    return View(dto); //redisplay the page, with the errors
}

Example of using GenericBizRunner in ASP.NET Core Web API

Here is a very simple example of creating a simple TodoItem taken from the EfCore.GenericService.AspNet ExampleWebApi application.

Note: The EfCore.GenericService.AspNetCore NuGet library contains the Response methods to convert the output of the business logic called by GenericBizRunner into a useful json response.

[ProducesResponseType(typeof (TodoItem), 201)] //Tells Swagger that the success status is 201, not 200
[HttpPost]
public ActionResult<TodoItem> Post(CreateTodoDto item, 
    [FromServices]IActionService<ICreateTodoBizLogic> service)
{
    var result = service.RunBizAction<TodoItem>(item);
    return service.Status.Response(this, 
        "GetSingleTodo", new { id = result?.Id }, result);
}

Why did I write this library?

I have built quite a few applications that contain business logic, some of it quite complex - things like optimisers and pricing engines to name but a few.
Business logic can be a real challenge, and over the years I have perfected a architecture pattern that isolates the business logic so its easier to write and manage - see this article and chapter 4 of my book, Entity Framework in Action.

Having perfected my pattern for handling business logic, then the next step was to automate the common parts of the pattern into a library. Which is where the
EfCore.GenericBizRunner library came from.

More information on the business logic pattern and library

The following links start with general descriptions and get deeper towards the end.

  • This article, which describes my business pattern.
  • Chapter 4 of my book, which covers building of business logic using this pattern.
  • This article that describes the EfCore.GenericBizAction library with examples.
  • Project's Wiki, which has a quick start guide and deeper documentation.
  • Clone this repo, and run the ASP.NET Core application in it to see the business logic in action.
  • Read the example code, in this repo.

EfCore.GenericBizRunner

EfCore.GenericBizRunner (shortened to GenericBizRunner) is a framework to help build and run business logic when you are using Entity Framework Core for database accesses. Its aim is to totally isolate the business logic from other parts of the application, especially the user presentation/UI layers. It provides the following features:

  • A standard pattern for writing business logic, including helper classes.
  • An anti-corruption layer feature that act as a barrier between the business logic and the user presentation/UI layers.
  • The BizRunner handles the call to EF Core's SaveChanges, with optional validation.
  • A service, known as a BizRunner, that runs your business logic.
  • Very good use of Dependency Injection (DI), making calls to business logic very easy.

EfCore.GenericBizRunner is available as a NuGet package, and on the EfCore.GenericBizRunner GitHub repo. It is an open-source project under the MIT license.

NOTE: Version 3 of GenericBizRunner changed the way it handles DTOs to make the library more useful in Web APIs - see upgrade guide.

Example of using GenericBizRunner in ASP.NET Core MVC

Here is some code taken from the ExampleWebApp (which you can run), from the OrdersController
to give you an idea of what it looks like. Note that every business logic call is very similar, just different interfaces and DTO/ViewModel classes.

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult ChangeDelivery(WebChangeDeliveryDto dto,
    [FromServices]IActionService<IChangeDeliverAction> service)
{
    if (!ModelState.IsValid)
    {
        service.ResetDto(dto); //resets any dropdown list etc.
        return View(dto);
    }

    service.RunBizAction(dto);

    if (!service.Status.HasErrors)
    {
        //We copy the message from the business logic to show 
        return RedirectToAction("ConfirmOrder", "Orders", 
            new { dto.OrderId, message = service.Status.Message });
    }

    //Otherwise errors, so I need to redisplay the page to the user
    service.Status.CopyErrorsToModelState(ModelState, dto);
    service.ResetDto(dto); //resets any dropdown list etc.
    return View(dto); //redisplay the page, with the errors
}

Example of using GenericBizRunner in ASP.NET Core Web API

Here is a very simple example of creating a simple TodoItem taken from the EfCore.GenericService.AspNet ExampleWebApi application.

Note: The EfCore.GenericService.AspNetCore NuGet library contains the Response methods to convert the output of the business logic called by GenericBizRunner into a useful json response.

[ProducesResponseType(typeof (TodoItem), 201)] //Tells Swagger that the success status is 201, not 200
[HttpPost]
public ActionResult<TodoItem> Post(CreateTodoDto item, 
    [FromServices]IActionService<ICreateTodoBizLogic> service)
{
    var result = service.RunBizAction<TodoItem>(item);
    return service.Status.Response(this, 
        "GetSingleTodo", new { id = result?.Id }, result);
}

Why did I write this library?

I have built quite a few applications that contain business logic, some of it quite complex - things like optimisers and pricing engines to name but a few.
Business logic can be a real challenge, and over the years I have perfected a architecture pattern that isolates the business logic so its easier to write and manage - see this article and chapter 4 of my book, Entity Framework in Action.

Having perfected my pattern for handling business logic, then the next step was to automate the common parts of the pattern into a library. Which is where the
EfCore.GenericBizRunner library came from.

More information on the business logic pattern and library

The following links start with general descriptions and get deeper towards the end.

  • This article, which describes my business pattern.
  • Chapter 4 of my book, which covers building of business logic using this pattern.
  • This article that describes the EfCore.GenericBizAction library with examples.
  • Project's Wiki, which has a quick start guide and deeper documentation.
  • Clone this repo, and run the ASP.NET Core application in it to see the business logic in action.
  • Read the example code, in this repo.

Release Notes

- BREAKING CHANGE: The way you register GenericBizRunner at startup has changed - see Upgrade Guide on how to do that.
     - BREAKING CHANGE: Changed the way in which AutoMapper mappings are set up. Please read the Upgrade Guide on how this changes integration tests.
     - Breaking change: removed AutoFac DI setup - now only provides Net Core setup (you can still use AutoFac for other DI usages).
     - New Feature: Added `IGenericStatus BeforeSaveChanges(DbContext)` to configuration - allows you to add your own validation, logging etc.
     - Minor improvement: Sql error handler used if present, even if validation isn't on.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
3.0.0 1,439 1/21/2019
2.0.2 985 8/22/2018
2.0.1 3,101 7/29/2018
2.0.0 200 7/14/2018
1.1.0 233 6/7/2018
1.0.1 280 2/10/2018
1.0.0 225 1/19/2018