Giter Club home page Giter Club logo

efpostgresengagement's Introduction

Entity Framework & Postgres Engagement

This library provides the engagement between Entity Framework Core and PosgreSQL. In other hand, it gives some services for CRUD such as IUnitOfWork, IRepository,.. Just do a litte code and let's start

Installation

This library requires .NET Core 3.x

Install via Package Manager
$ Install-Package EFPostgresEngagement
Install via .NET CLI
$ dotnet add package EFPostgresEngagement

How to use?

  • Create a new environment variable for the Postgres connection
CONNECTION_STRING: '....'
  • Create an entity base and let's in inherits DefaultEntityBase
    public class EntityBase : DefaultEntityBase
    {
    }
  • Create a new DbContext and let's it inherits PostgresDbContextBase
    public class MyDataDbContext : PostgresDbContextBase<MyDataDbContext>
    {
        public MyDataDbContext(DbContextOptions<MyDataDbContext> options, IDbTracker dbTracker) : base(options,
            dbTracker)
        {
        }
    }
  • Create a DbContext factory for migration --optional
public class MyDbContextFactory : IDesignTimeDbContextFactory<MyDbContext>
    {
        public MyDbContext CreateDbContext(string[] args)
        {
            var connection = Environment.GetEnvironmentVariable("CONNECTION_STRING");
            
            var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>();
            optionsBuilder.UseNpgsql(connection ?? <connection-string>");

            return new MyDbContext(optionsBuilder.Options, null);
        }
    }
  • Create class Repository and let's it inherits RepostoryBase
    public class Repository<TEntity> : RepositoryBase<TEntity, MyDataDbContext>
        where TEntity : class, new()
    {
        public Repository(MyDataDbContext dbContext) : base(dbContext)
        {
        }
    }
  • Create class UnitOfWork and let's it inherits UnitOfWorkBase
    public class UnitOfWork : UnitOfWorkBase<MyDataDbContext>
    {
        public UnitOfWork(MyDataDbContext dbContext) : base(dbContext)
        {
        }

        public override IRepository<T> GetRepository<T>()
        {
            return new Repository<T>(this.DbContext);
        }
    }
  • Finally, registering all services in StartUp file
    services
        .UsePostgresSql<MyDataDbContext>(this.Configuration)
        .AddTransient<IUnitOfWork, UnitOfWork>();
    services.AddScoped<IDbTracker>(provider => new ApplicationUserProvider());
  • Ok, just inject IUnitOfWork wherever you want to interact with database.

Note: IDbTracker helps to track the owner of data modification. In the near future, it will be optional. Temporarily for now, please implement a class to pass this requirement

An example project is implementing. Please watch for new update.

efpostgresengagement's People

Contributors

hieutran3010 avatar

Watchers

 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.