Giter Site home page Giter Site logo

msx752 / sampledotnet.repositoryfactory Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 64 KB

EFCore DbContext RepositoryFactory Pattern managed by DbContextFactory

License: MIT License

C# 100.00%
dbcontext dbcontextfactory efcore entity-framework-core repository-pattern

sampledotnet.repositoryfactory's Introduction

Nuget CodeQL MIT

EFCore DbContext RepositoryFactory Pattern managed by DbContextFactory

EntityFrameworkCore doesn't support multiple parallel operations, when we need parallel actions in different threads such as adding or deleting on the same DbContext, It throws an exception when calling SaveChanges source.

NOTE: DbContext service scope set as Transient which managed by IServiceScopeFactory

How to Use

using SampleDotnet.RepositoryFactory;

ServiceCollection Definition

services.AddDbContextFactoryWithUnitOfWork<UserDbContext>(opt =>
    {
        opt.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
    });

then we call transient scoped DbContext

    public class UserController : Controller
    {
        private readonly IUnitOfWork _unitOfWork;

        public UserController(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        [HttpDelete("{id}")]
        public ActionResult Delete(Guid id)
        {
            using (var repository = _unitOfWork.CreateRepository<UserDbContext>())
            {
                var personal = repository.FirstOrDefault<UserEntity>(f => f.Id == id);

                //some operations goes here....

                repository.Delete(personal);

                //some operations goes here....
            }

            _unitOfWork.SaveChanges();
            return Ok();
        }
    }

Additional Feature

  • If IHasDateTimeOffset interfece used on Entity object then value of the the CreatedAt and UpdatedAt properties will be updated automatically.
        public class TestUserEntity : IHasDateTimeOffset
        {
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public Guid Id { get; set; }
            public string Name { get; set; }
            public string Surname { get; set; }

            public DateTimeOffset? CreatedAt { get; set; }
            public DateTimeOffset? UpdatedAt { get; set; }
        }

sampledotnet.repositoryfactory's People

Contributors

msx752 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.