EfCore.GenericServices 9.0.0

dotnet add package EfCore.GenericServices --version 9.0.0                
NuGet\Install-Package EfCore.GenericServices -Version 9.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EfCore.GenericServices" Version="9.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EfCore.GenericServices --version 9.0.0                
#r "nuget: EfCore.GenericServices, 9.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EfCore.GenericServices as a Cake Addin
#addin nuget:?package=EfCore.GenericServices&version=9.0.0

// Install EfCore.GenericServices as a Cake Tool
#tool nuget:?package=EfCore.GenericServices&version=9.0.0                

EfCore.GenericServices

This library helps you quickly code Create, Read, Update and Delete (CRUD) accesses for a web/mobile/desktop application. It acts as a adapter and command pattern between a database accessed by Entity Framework Core (EF Core) and the needs of the front-end system.

The EfCore.GenericServices library is available on NuGet as EfCore.GenericServices and is an open-source library under the MIT licence. See ReleaseNotes for details of changes and information on each version of EfCore.GenericServices.

List of versions and which .NET framework they support

  • Version 8.0.0: Supports NET 9 only (simpler to update to next NET release)
  • Version 8.0.0: Supports NET 8 only (simpler to update to next NET release)
  • Version 6.?.?: Supports NET 6, 7 and 8
  • Version 5.2.?: Supports NET 5, 6 and 7

There are older versions of the EfCore.GenericServices library, but .NET lower than .NET 5 are not supported by Microsoft.

Documentation and useful articles

The documentation can be found in the GitHub wiki. but the rest of this README file provides a good overview of what the library can do, but here are some articles that give you a detailed description of what the libraray does.

What the library does

This library takes advantage of the fact that each of the four CRUD database accesses differ in what they do, but they have a common set of data part they all use, which are:

  1. What database class/table do you want to access?
  2. What properties in that class/table do you want to access or change?

This library uses DTOs (data transfer objects, also known as ViewModels) plus a special interface to define the class/table and the properties to access. That allows the library to implement a generic solution for each of the four CRUD accesses, where the only thing that changes is the DTO you use.

Typical web applications have hundreds of CRUD pages - display this, edit that, delete the other - and each CRUD access has to adapt the data in the database to show the user, and then apply the changes to the database. So, you create one set of update code for your specific application and then cut/paste + change one line (the DTO name) for all the other versions.

I personally work with ASP.NET Core, so my examples are from that, but it will work with any NET Core type of application (I do know one person have used this libary with WPF).

Limitations with EF Core 5 and beyond

  • The ILinkToEntity<TEntity> interface can't handle an entity class mapped to a multple tables.

Code examples of using EfCore.GenericServices

I personally work with ASP.NET Core, so my examples are all around ASP.NET Core, but EfCore.GenericServices will work with any NET Core type of application (I do know one person have used this libary with WPF).

ASP.NET Core MVC - Controllers with actions

The classic way to produce HTML pages in ASP.NET is using the MVC approach, with razor pages. Here a simple example to show you the basic way to inject and then call the ICrudServices, in this case a simple List.

public class BookController

    private ICrudServices _service;

    public BookController(ICrudServices service)
    {
        _service = service;
    }

    public ActionResult Index()
    {
        var dataToDisplay = _service.ReadManyNoTracked<BookListDto>().ToList()
        return View(dataToDisplay);
    }
    //... etc.

ASP.NET Core - Razor Pages

Here is the code from the example Razor Page application contained in this repo for adding a review to a Book (the example site is a tiny Amazon-like site). This example shows an more complex example where I am updating the Book class that uses a Domain-Driven Design (DDD) approach to add a new review to a book. The code shown is the complete code in the AddReview.cshtml.cs class.

public class AddReviewModel : PageModel
{
    private readonly ICrudServices _service;

    public AddReviewModel(ICrudServices service)
    {
        _service = service;
    }

    [BindProperty]
    public AddReviewDto Data { get; set; }

    public void OnGet(int id)
    {
        Data = _service.ReadSingle<AddReviewDto>(id);
        if (!_service.IsValid)
        {
            _service.CopyErrorsToModelState(ModelState, Data, nameof(Data));
        }
    }

    public IActionResult OnPost()
    {
        if (!ModelState.IsValid)
        {
            return Page();
        }
        _service.UpdateAndSave(Data);
        if (_service.IsValid)
            return RedirectToPage("BookUpdated", new { message = _service.Message});

        //Error state
        _service.CopyErrorsToModelState(ModelState, Data, nameof(Data));
        return Page();
    }
}

NOTE: If you compare the code above with the AddPromotion or ChangePubDate updates in the same example then you will see they are identical apart from the type of the Data property.

And the ViewModel/DTO isn't anything special (see the AddReviewDto). They just need to be marked with an empty ILinkToEntity<TEntity> interface, which tells GenericServices which EF Core entity class to map to. For more security you can also mark any read-only properties with the [ReadOnly(true)] attribute - GenericServices will never try to update the database with any read-only marked property.

ASP.NET Core Web API

When using ASP.NET Web API then another companion library called EfCore.GenericServices.AspNetCore provides extension methods to help return the data in the correct form (plus other methods to allow unit testing of Web API actions using EfCore.GenericServices).

The code below comes from the example ToDoController example in the EfCore.GenericServices.AspNetCore GitHub repo.

public class ToDoController : ControllerBase
{

    [HttpGet]
    public async Task<ActionResult<WebApiMessageAndResult<List<TodoItem>>>> GetManyAsync([FromServices]ICrudServices service)
    {
        return service.Response(await service.ReadManyNoTracked<TodoItem>().ToListAsync());
    }

    [Route("name")]
    [HttpPatch()]
    public ActionResult<WebApiMessageOnly> Name(ChangeNameDto dto, [FromServices]ICrudServices service)
    {
        service.UpdateAndSave(dto);
        return service.Response();

    //... other action methods removed 
}

Technical features

The EfCore.GenericServices (NuGet, EfCore.GenericServices), is an open-source (MIT licence) netcoreapp2.0 library that assumes you use EF Core for your database accesses. It has good documentation in the repo's Wiki.

It is designed to work with both standard-styled entity classes (e.g. public setters on the properties and a public, paremeterless constructor), or with a Domain-Driven Design (DDD) styled entity classes (e.g. where all updates are done through named methods in the the entity class) - see this article for more on the difference between standard-styled entity classes and DDD styled entity classes.

It also works well with with dependancy injection (DI), such as ASP.NET Core's DI service. But does also contain a simplified, non-DI based configuration system suitable for unit testing and/or serverless applications.

NOTE: I created a similar library for EF6.x back in 2014, which has saved my many months of (boring) coding - on one project alone I think it saved 2 months out of 12. This new version contains the learning from that library, and the new DDD-enabling feature of EF Core to reimagine that library, but in a very different (hopefully simpler) way.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on EfCore.GenericServices:

Package Downloads
EfCore.GenericServices.AspNetCore

A support library to the EfCore.GenericServices and EfCore.GenericBizRunner libraries. It converts IStatusGeneric into ASP.NET Core's ModelState or Web API response

EntityServices

Package Description

UZB.ELYORBEK.EF

CRUD operations works.

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on EfCore.GenericServices:

Repository Stars
JonPSmith/AuthPermissions.AspNetCore
This library provides extra authorization and multi-tenant features to an ASP.NET Core application.
JonPSmith/PermissionAccessControl2
Version 2 of example application to go with articles on feature and data authorization
Version Downloads Last updated
9.0.0 47 11/21/2024
8.1.0 5,160 5/4/2024
8.0.0 490 4/10/2024
6.0.0 6,611 11/22/2023
5.2.1 20,949 1/3/2023
5.2.0 3,803 11/15/2022
5.2.0-preview001 683 12/8/2022
5.1.1 17,904 2/2/2022
5.1.0 25,443 11/10/2021
5.1.0-preview001 167 11/4/2021
5.0.1 16,525 7/8/2021
5.0.0 19,379 1/6/2021
3.3.0 1,380 6/21/2021
3.2.2 17,790 5/22/2020
3.2.1 3,514 4/11/2020
3.2.0 1,183 4/10/2020
3.1.0 45,666 1/27/2020
3.0.0 5,188 10/12/2019
2.1.0-preview001 1,072 10/4/2019
2.0.3 3,675 7/29/2019
2.0.2 11,211 4/4/2019
2.0.1 5,017 3/10/2019
2.0.0 7,476 1/2/2019
2.0.0-preview001 2,753 12/24/2018
1.3.3 5,705 10/27/2018
1.3.2 1,905 10/22/2018
1.3.1 2,026 9/26/2018
1.3.0 1,974 9/18/2018
1.2.6 10,457 8/18/2018
1.2.4 2,991 7/29/2018
1.2.3 2,193 7/24/2018
1.2.2 1,455 7/16/2018
1.2.1 1,488 7/14/2018
1.1.0 2,153 6/7/2018
1.0.0 2,353 4/12/2018

- .NET 9 version