GithubHelp home page GithubHelp logo

jonnynovikov / slant.entity Goto Github PK

View Code? Open in Web Editor NEW
12.0 2.0 2.0 135 KB

Better Entity Framework development

License: MIT License

C# 100.00%
c-sharp entity-framework ef6 entity linq dbcontext-instances nuget slant

slant.entity's Introduction

Slant.Entity

Library for managing DbContext the right way with Entity Framework Core.

NuGet version (Slant.Entity) NuGet Build Status GitHub license

Overview

This package is based on the original DbContextScope repository by Mehdi El Gueddari with the following changes:

  • projects were updated to .NET 6+ and Entity Framework Core;
  • usages of CallContext were replaced with AsyncLocal;
  • added fix for RefreshEntitiesInParentScope method so that it works correctly for entities with composite primary keys;
  • added fix for DbContextCollection's Commit and CommitAsync methods so that SaveChanges can be called more than once if there is a DbUpdateConcurrencyException (see this unmerged pull request in the original DbContextScope repository);
  • added the RegisteredDbContextFactory class as a concrete implementation of the IDbContextFactory interface, which allows users to easily register factory functions for one or more DbContext type(s) during startup; and
  • added unit tests.

Description

Library provides simple and flexible way to manage your Entity Framework Core DbContext instances.

DbContextScope was created out of the need for a better way to manage DbContext instances in Entity Framework-based applications.

The commonly advocated method of injecting DbContext instances works fine for single-threaded web applications where each web request implements exactly one business transaction. But it breaks down quite badly when console apps, Windows Services, parallelism and requests that need to implement multiple independent business transactions make their appearance.

The alternative of manually instantiating DbContext instances and manually passing them around as method parameters is (speaking from experience) more than cumbersome.

DbContextScope implements the ambient context pattern for DbContext instances. It doesn't force any particular design pattern or application architecture to be used. It works beautifully with dependency injection and works like a charm without any IoC container.

And most importantly, DbContextScope has been battle-tested in a large-scale applications for a long time.

Mehdi El Gueddari's original article describing the thinking behind the DbContextScope library can be found here.

In summary, the library addresses the problem that injecting DbContext instances as a scoped dependency (which ordinarily results in one instance per web request) offers insufficient control over the lifetime of DbContext instances in more complex scenarios.

The DbContextScope library allows users to create scopes which control the lifetime of ambient DbContext instances, as well giving control over the exact time at which changes are saved.

For general usage instructions, see article referred to above and the original GitHub repository readme file. Copy of original README with improved formatting and fixing broken links is included in this repository here.

Please note the Mehdime.Entity namespace has been renamed to Slant.Entity due to naming conventions in Slant packages.

The new RegisteredDbContextFactory class can be used as follows:

  • In Startup.cs, register a RegisteredDbContextFactory instance as a singleton and register one or more DbContext factory functions on that instance, e.g.:
using Slant.Entity;
...
public void ConfigureServices(IServiceCollection services)
{
    ...
    // Create an instance of the RegisteredDbContextFactory
    var dbContextFactory = new RegisteredDbContextFactory();

    // Register factory functions for each of the required DbContext types
    dbContextFactory.RegisterDbContextType<DbContextOne>(() =>
        new DbContextOne(Configuration.GetConnectionString("DatabaseOne")));
    dbContextFactory.RegisterDbContextType<DbContextTwo>(() =>
        new DbContextTwo(Configuration.GetConnectionString("DatabaseTwo")));

    // Register the RegisteredDbContextFactory instance as a singleton
    // with the dependency injection container.
    services.AddSingleton<IDbContextFactory>(dbContextFactory);
    ...
}

See also the unit tests for RegisteredDbContextFactory here.

Dependencies

  • .NET 8+
  • Entity Framework Core 8+

Installation

dotnet add package Slant.Entity

Acknowledgments

Many thanks to Mehdi El Gueddari for creating the original DbContextScope library.

slant.entity's People

Contributors

dependabot[bot] avatar gitter-badger avatar jonny-novikov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.