Skip to content
Eltjo edited this page Mar 7, 2021 · 4 revisions

Description of the tool

DBAccess is a tool that provides default utilities for managing the Database infrastructure layer such as:

  • UnitOfWork
  • Synchronous repositories
  • Asynchronous repositories
  • Flexibility for any type of query
  • Ease of configuration

Dependencies and compatibilities:

  • .NET Core 2.1:

    • Microsoft.EntityFrameworkCore [2.1.14]
  • .NET Core 2.2:

    • Microsoft.EntityFrameworkCore [2.2.6]
  • .NET Core 3.1:

    • Microsoft.EntityFrameworkCore [3.1.7]
  • .NET Framework

    • EntityFramework [6.4.4]
    • Unity [5.11.6]
  • All:

    • ChustaSoft.Common [1.3.2]: Plain library with no dependencies as a complement for the SDK

Configuring DBAccess.EntityFramework

  1. Install package: Install-Package ChustaSoft.Tools.DBAccess.EntityFramework

Dependencies installed in any version are described at the beginning.

Additionally, the Abstractions package of the project will be installed as well. This package is also useful for a better architectural design in your application, allowing low-level layers to remain unaware of the infrastructure used by high-level libraries.

.NET Core (any version)

  1. At startup level on ConfigureServices, if the project entities uses Guid as default Id:

services.RegisterDbAccess<[ApplicationContext]>();

[ApplicationContext] should be replaced by the context that the application will use

If the application uses multiple contexts, it will be necessary to add as many records as the one above for each of the contexts used:

services.RegisterDbAccess<[ApplicationContext1]>().RegisterDbAccess<[ApplicationContext2]>();

If another key type is used in the context, then you can register the specific Id used in the project, for example int below:

services.RegisterDbAccess<[ApplicationContext], int>();

This library forces the project to use an aligned Primary Key strategy on all the entities.

.NET Framework (Using Unity Container)

  1. During the registration of the container, it will be required to add the following:

container.RegisterDbAccess<[ApplicationContext]>();

[ApplicationContext] should be replaced by the context that the application will use

If the application uses multiple contexts, it will be necessary to add as many records as the one above for each of the contexts used:

container.RegisterDbAccess<[ApplicationContext1]>().RegisterDbAccess<[ApplicationContext2]>();

If another key type is used in the context, then you can register the specific Id used in the project, for example int below:

container.RegisterDbAccess<[ApplicationContext], int>();

This library forces the project to use an aligned Primary Key strategy on all the entities.

Usage

  1. In the required service, inject the IUnitOfWork in the constructor
  2. Then, get the required repository from the IUnitOfWork .

With this approach never will be required to inject directly the repository, otherwise the sense of the library and the UoW pattern will have no sense

  1. IUnitOfWork can provide of sync and async repositories, the methods required are only the type of entity required, that should be registered on the context, and the Key of the object,
  • GetAsyncRepository<TEntity, TKey>
  • GetRepository<TEntity, TKey>

by default Tkey is configured as a Guid if nothing is provided

Example of a Service using it

Examples are provided here in the repo

Using the MongoDb implementation

Transactions are only supported for replica sets. Perform the following steps to convert a standalone Mongo server to a replica set:

  • Edit the file mongod.cfg (for windows installations typically located in C:\Program Files\MongoDB\Server\4.2\bin) and make sure it contains the following lines:
    #replication:
    replication:
      replSetName: rs0
  • Restart the MongoDb Server process
  • Execute the following command in a Mongo shell: rs.initiate().