• Stars
    star
    216
  • Rank 183,179 (Top 4 %)
  • Language
    C#
  • License
    MIT License
  • Created almost 7 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

EfCore.GenericBizRunner

NOTE: Version 4.1.0 now requires you to install the NuGet package GenericServices.StatusGeneric in any of your assemblies that needs to interact with the GenericBizRunner's status.

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 4 of GenericBizRunner supports both EF Core >=2.1 and EF Core >=3.0 via NetStandard2.0 and NetStandard2.1 versions.

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.

More Repositories

1

AuthPermissions.AspNetCore

This library provides extra authorization and multi-tenant features to an ASP.NET Core application.
C#
774
star
2

EfCore.GenericServices

A library to help you quickly code CRUD accesses for a web/mobile/desktop application using EF Core.
C#
598
star
3

EfCoreinAction-SecondEdition

Supporting repo to go with book "Entity Framework Core in Action", second edition
C#
397
star
4

EfCore.TestSupport

Tools for helping in unit testing applications that use Entity Framework Core
C#
353
star
5

EfCoreInAction

Supporting code to go with the book "Entity Framework Core in Action"
340
star
6

PermissionAccessControl2

Version 2 of example application to go with articles on feature and data authorization
C#
276
star
7

GenericServices

GenericServices helps with building a service/application layer in a .NET based application using EF6.x
C#
245
star
8

NetCore.AutoRegisterDi

Extension method to find/register classes in an assembly into the Microsoft DI provider
C#
234
star
9

PermissionAccessControl

Example code for Authorization articles
C#
224
star
10

AspNetReactSamples

Template/Sample ASP.NET projects to develop/build/test React.js apps
JavaScript
174
star
11

EfCore.SoftDeleteServices

Services to provide simple soft delete and cascade soft delete in EF Core
C#
114
star
12

EfCore.SchemaCompare

Library to compare EF Core's Model of the database against a database's schema.
C#
106
star
13

Net.DistributedFileStoreCache

NET distributed cache using a json file as the shared resourse with very fast Get
C#
96
star
14

SampleMvcWebApp

A Sample MVC5 web application showing the use of GenericServices for CRUD operations
C#
75
star
15

EfCore.GenericEventRunner

A library to allow developer use events to update their database via Entity Framework Core (EF Core)
C#
67
star
16

EfCoreSqlAndCosmos

Example CQRS application using Cosmos DB with EF Core
C#
66
star
17

BookApp.All

Example of applying an modular monolith approach to building apps. This version contains the whole app in one solution
C#
50
star
18

EfCore.GenericServices.AspNetCore

Converts EFCore.GenericServices and EfCore.GenericBizRunner statuses to ASP.NET Core formats
C#
49
star
19

EfSchemaCompare

EfSchemaCompare.EF6 allows you to compare Entity Framework's database modal with an actual SQL database.
C#
44
star
20

RunStartupMethodsSequentially

A .NET library that runs methods within a locked state on startup. This is useful if you want to migrate or seed a database on an web application that has multiple instances.
C#
32
star
21

EfCore.SoftDeleteServices-Old

Services to provide simple soft delete and cascade soft delete in EF Core
C#
29
star
22

GenericServices.StatusGeneric

Implements the "return a status" pattern - useful for code that can return errors
C#
28
star
23

SampleMvcWebAppComplex

A more complex MVC application showing the use of GenericServices with the AdventureWorksLT2012 database.
C#
26
star
24

Net.LocalizeMessagesAndErrors

This library provides extra code to make it easier to support in different languages in your .NET application
C#
18
star
25

MvcUsingBower

Applying Visual Studio's Bower/Grunt tools to a ASP.NET MVC application. See
JavaScript
16
star
26

PermissionsOnlyApp

C#
15
star
27

DDDExampleCode

Example code to go with my talk and article on DDD
C#
13
star
28

MultiProgPackTool

https://www.thereformedprogrammer.net/evolving-modular-monoliths-2-breaking-up-your-app-into-multiple-solutions/#how-to-create-a-nuget-packages
C#
9
star
29

SimpleMessageBroker

C#
5
star
30

Ef6BookApp

C#
4
star
31

AspNetCore.MultipleHostedService

C#
3
star
32

AuthP.CustomDatabaseExamples

C#
2
star
33

BookApp.Books

Part of the evolving Modular Monalith
C#
2
star
34

TryAspNetCoreMigrate

C#
2
star
35

BookApp.Main

C#
2
star
36

TestSupportSchema

C#
1
star