TanvirArjel.EFCore.GenericRepository 5.6.0

This library is a Generic Repository implementation for EF Core ORM which will remove developers' pain to write repository layer for each .NET Core and .NET project.

This library includes the following notable features:

     1. This library can be run on any .NET Core or .NET application which has .NET Core 3.1, .NET Standard 2.1 and .NET 5.0 support.

     2. It’s providing the Generic Repository with database transaction support.

     3. It has all the required methods to query your data in whatever way you want without getting IQueryable<T> from the repository.

     4. It also has Specification<T> pattern support so that you can build your query dynamically i.e. differed query building.

     5. It also has database-level projection support for your query.

     6. It also has support to run raw SQL command against your relational database.

     7. It also has support to choose whether you would like to track your query entity/entities or not.

     8. It also has support to reset your EF Core DbContext state whenever you really needed.

     9. Most importantly, it has full Unit Testing support. Most importantly, it facilitates the writing unit test with mocking.

     10. It also has pagination support.

      11. Free raw SQL query support both for complex type and primitive types.

Install-Package TanvirArjel.EFCore.GenericRepository -Version 5.6.0
dotnet add package TanvirArjel.EFCore.GenericRepository --version 5.6.0
<PackageReference Include="TanvirArjel.EFCore.GenericRepository" Version="5.6.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TanvirArjel.EFCore.GenericRepository --version 5.6.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: TanvirArjel.EFCore.GenericRepository, 5.6.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install TanvirArjel.EFCore.GenericRepository as a Cake Addin
#addin nuget:?package=TanvirArjel.EFCore.GenericRepository&version=5.6.0

// Install TanvirArjel.EFCore.GenericRepository as a Cake Tool
#tool nuget:?package=TanvirArjel.EFCore.GenericRepository&version=5.6.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

EF Core Generic Repository

This library is a Generic Repository implementation for EF Core ORM which will remove developers' pain to write repository layer for each .NET Core and .NET project.

⭐ Giving a star

If you find this library useful, please don't forget to encouraging me to do such more stuffs by giving a star to this repository. Thank you.

🔥 What's new

Pagination Support:

PaginationSpecification<Employee> specification = new PaginationSpecification<Employee>();
specification.Conditions.Add(e => e.Name.Contains("Ta"));
specification.PageIndex = 1;
specification.PageSize = 10;

PaginatedList<EmployeeDto> paginatedList = await _repository.GetPaginatedListAsync(specification, e => new EmployeeDto
{
    Id = e.Id
    Name = e.Name,
    DepartmentName = e.DepartmentName
});

Free raw SQL support:

List<string> search = new List<string>() { "Tanvir", "Software" };
string sqlQuery = "Select EmployeeName, DepartmentName from Employee Where EmployeeName LIKE @p0 + '%' and DepartmentName LIKE @p1 + '%'";
List<EmployeeDto> items = await _repository.GetFromRawSqlAsync<EmployeeDto>(sqlQuery, search);

⚙️ This library includes following notable features:

  1. This library can be run on any .NET Core or .NET application which has .NET Core 3.1, .NET Standard 2.1 and .NET 5.0 support.

  2. It’s providing the Generic Repository with database transaction support.

  3. It has all the required methods to query your data in whatever way you want without getting IQueryable<T> from the repository.

  4. It also has Specification<T> pattern support so that you can build your query dynamically i.e. differed query building.

  5. It also has database level projection support for your query.

  6. It also has support to run raw SQL command against your relational database.

  7. It also has support to choose whether you would like to track your query entity/entities or not.

  8. It also has support to reset your EF Core DbContext state whenever you really needed.

  9. Most importantly, it has full Unit Testing support.

  10. Pagination support.

  11. Free raw SQL query support both for complex type and primitive types.

✈️ How do I get started?

First install the latest version of TanvirArjel.EFCore.GenericRepository nuget package into your project as follows:

Package Manager Console:

Install-Package TanvirArjel.EFCore.GenericRepository

.NET CLI:

dotnet add package TanvirArjel.EFCore.GenericRepository

Then in the ConfirugeServices method of the Startup class:

public void ConfigureServices(IServiceCollection services)
{
    services.AddGenericRepository<YourDbContext>();
}

🛠️ Usage: Query

public class EmployeeService
{
    private readonly IRepository _repository;

    public EmployeeService(IRepository repository)
    {
        _repository = repository;
    }

    public async Task<Employee> GetEmployeeAsync(int employeeId)
    {
        Employee employee = await _repository.GetByIdAsync<Employee>(1);
        return employee;
    }
}

🛠️ Usage: Command

public class EmployeeService
{
    private readonly IRepository _repository;

    public EmployeeService(IRepository repository)
    {
        _repository = repository;
    }

    // Single database operation.
    public async Task<int> CreateAsync(Employee employee)
    {
        object[] primaryKeys = await _repository.InsertAsync(employee);
        return (int)primaryKeys[0];
    }

    // Multiple database operations.
    public async Task<int>> CreateAsync(Employee employee)
    {
       IDbContextTransaction transaction = await _repository.BeginTransactionAsync(IsolationLevel.ReadCommitted);
       try
       {
           object[] primaryKeys = await _repository.InsertAsync(employee);

           long employeeId = (long)primaryKeys[0];
           EmployeeHistory employeeHistory = new EmployeeHistory()
           {
               EmployeeId = employeeId,
               DepartmentId = employee.DepartmentId,
               EmployeeName = employee.EmployeeName
           };

           await _repository.InsertAsync(employeeHistory);

           await transaction.CommitAsync();

           return employeeId;
       }
       catch (Exception)
       {
           await transaction.RollbackAsync();
           throw;
       }
    }
}

For more detail documentaion, please visit Documentation Wiki

EF Core Generic Repository

This library is a Generic Repository implementation for EF Core ORM which will remove developers' pain to write repository layer for each .NET Core and .NET project.

⭐ Giving a star

If you find this library useful, please don't forget to encouraging me to do such more stuffs by giving a star to this repository. Thank you.

🔥 What's new

Pagination Support:

PaginationSpecification<Employee> specification = new PaginationSpecification<Employee>();
specification.Conditions.Add(e => e.Name.Contains("Ta"));
specification.PageIndex = 1;
specification.PageSize = 10;

PaginatedList<EmployeeDto> paginatedList = await _repository.GetPaginatedListAsync(specification, e => new EmployeeDto
{
    Id = e.Id
    Name = e.Name,
    DepartmentName = e.DepartmentName
});

Free raw SQL support:

List<string> search = new List<string>() { "Tanvir", "Software" };
string sqlQuery = "Select EmployeeName, DepartmentName from Employee Where EmployeeName LIKE @p0 + '%' and DepartmentName LIKE @p1 + '%'";
List<EmployeeDto> items = await _repository.GetFromRawSqlAsync<EmployeeDto>(sqlQuery, search);

⚙️ This library includes following notable features:

  1. This library can be run on any .NET Core or .NET application which has .NET Core 3.1, .NET Standard 2.1 and .NET 5.0 support.

  2. It’s providing the Generic Repository with database transaction support.

  3. It has all the required methods to query your data in whatever way you want without getting IQueryable<T> from the repository.

  4. It also has Specification<T> pattern support so that you can build your query dynamically i.e. differed query building.

  5. It also has database level projection support for your query.

  6. It also has support to run raw SQL command against your relational database.

  7. It also has support to choose whether you would like to track your query entity/entities or not.

  8. It also has support to reset your EF Core DbContext state whenever you really needed.

  9. Most importantly, it has full Unit Testing support.

  10. Pagination support.

  11. Free raw SQL query support both for complex type and primitive types.

✈️ How do I get started?

First install the latest version of TanvirArjel.EFCore.GenericRepository nuget package into your project as follows:

Package Manager Console:

Install-Package TanvirArjel.EFCore.GenericRepository

.NET CLI:

dotnet add package TanvirArjel.EFCore.GenericRepository

Then in the ConfirugeServices method of the Startup class:

public void ConfigureServices(IServiceCollection services)
{
    services.AddGenericRepository<YourDbContext>();
}

🛠️ Usage: Query

public class EmployeeService
{
    private readonly IRepository _repository;

    public EmployeeService(IRepository repository)
    {
        _repository = repository;
    }

    public async Task<Employee> GetEmployeeAsync(int employeeId)
    {
        Employee employee = await _repository.GetByIdAsync<Employee>(1);
        return employee;
    }
}

🛠️ Usage: Command

public class EmployeeService
{
    private readonly IRepository _repository;

    public EmployeeService(IRepository repository)
    {
        _repository = repository;
    }

    // Single database operation.
    public async Task<int> CreateAsync(Employee employee)
    {
        object[] primaryKeys = await _repository.InsertAsync(employee);
        return (int)primaryKeys[0];
    }

    // Multiple database operations.
    public async Task<int>> CreateAsync(Employee employee)
    {
       IDbContextTransaction transaction = await _repository.BeginTransactionAsync(IsolationLevel.ReadCommitted);
       try
       {
           object[] primaryKeys = await _repository.InsertAsync(employee);

           long employeeId = (long)primaryKeys[0];
           EmployeeHistory employeeHistory = new EmployeeHistory()
           {
               EmployeeId = employeeId,
               DepartmentId = employee.DepartmentId,
               EmployeeName = employee.EmployeeName
           };

           await _repository.InsertAsync(employeeHistory);

           await transaction.CommitAsync();

           return employeeId;
       }
       catch (Exception)
       {
           await transaction.RollbackAsync();
           throw;
       }
    }
}

For more detail documentaion, please visit Documentation Wiki

Release Notes

1. EF Core 6.0 support has been added.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
5.6.0 128 8/13/2021
5.5.0 1,727 4/7/2021
5.4.0 100 3/30/2021
5.3.0 218 3/27/2021
5.2.2 369 2/12/2021
5.2.1 98 2/8/2021
5.2.0 66 2/7/2021
5.1.0 164 1/14/2021
5.0.0 304 11/12/2020
3.1.2 617 5/20/2020
3.1.1 212 4/25/2020
3.1.0 187 4/23/2020
3.0.2 211 5/20/2020
3.0.1 196 4/25/2020
3.0.0 193 4/23/2020
2.0.2 200 5/20/2020
2.0.1 185 4/25/2020
2.0.0 176 4/23/2020