TanvirArjel.EFCore.QueryRepository 1.4.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package TanvirArjel.EFCore.QueryRepository --version 1.4.0                
NuGet\Install-Package TanvirArjel.EFCore.QueryRepository -Version 1.4.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TanvirArjel.EFCore.QueryRepository" Version="1.4.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TanvirArjel.EFCore.QueryRepository --version 1.4.0                
#r "nuget: TanvirArjel.EFCore.QueryRepository, 1.4.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TanvirArjel.EFCore.QueryRepository as a Cake Addin
#addin nuget:?package=TanvirArjel.EFCore.QueryRepository&version=1.4.0

// Install TanvirArjel.EFCore.QueryRepository as a Cake Tool
#tool nuget:?package=TanvirArjel.EFCore.QueryRepository&version=1.4.0                

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?

For full version (both query and command support):

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)
{
    // For single DbContext
    services.AddGenericRepository<YourDbContext>();
    
    // If multiple DbContext
    services.AddGenericRepository<YourDbContext1>();
    services.AddGenericRepository<YourDbContext1>();
}

For query version only:

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

Package Manager Console:

Install-Package TanvirArjel.EFCore.QueryRepository

.NET CLI:

dotnet add package TanvirArjel.EFCore.QueryRepository

Then in the ConfirugeServices method of the Startup class:

public void ConfigureServices(IServiceCollection services)
{
    // For single DbContext
    services.AddQueryRepository<YourDbContext>();
    
    // For multiple DbContext
    services.AddQueryRepository<YourDbContext1>();
    services.AddQueryRepository<YourDbContext1>();
}

🛠️ Usage: Query

public class EmployeeService
{
    // For query version, please use `IQueryRepository` instead of `IRepository`
    private readonly IRepository _repository; // If single DbContext
    private readonly IRepository<YourDbContext1> _dbConext1Repository; // If multiple DbContext

    public EmployeeService(IRepository repository, IRepository<YourDbContext1> dbConext1Repository)
    {
        _repository = repository;
        _dbConext1Repository = dbConext1Repository;
    }

    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; // If single DbContext
    private readonly IRepository<YourDbContext1> _dbConext1Repository; // If multiple DbContext

    public EmployeeService(IRepository repository, IRepository<YourDbContext1> dbConext1Repository)
    {
        _repository = repository;
        _dbConext1Repository = dbConext1Repository;
    }

    // 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

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on TanvirArjel.EFCore.QueryRepository:

Package Downloads
TanvirArjel.EFCore.GenericRepository

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.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.5.3 6,442 1/13/2024
1.5.2 3,534 11/20/2023
1.5.1 2,592 9/9/2023
1.5.0 11,883 11/9/2022
1.5.0-preview1 151 10/19/2022
1.4.0 6,443 8/21/2022
1.3.0 13,533 6/9/2022
1.2.1 784 6/7/2022
1.1.0 11,765 11/12/2021
1.0.0 679 10/6/2021

1. SQL connection closing isssue has been fixed.