1 Star 0 Fork 0

jobily / EFCore.GenericRepository

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

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 encourage 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.GetListAsync(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 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 ConfigureServices method of the Startup class:

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

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 ConfigureServices method of the Startup class:

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

🛠️ 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> _dbContext1Repository; // If multiple DbContext

    public EmployeeService(IRepository repository, IRepository<YourDbContext1> dbContext1Repository)
    {
        _repository = repository;
        _dbContext1Repository = dbContext1Repository;
    }

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

🛠️ Usage: Command

public class EmployeeService
{
    private readonly IRepository _repository; // If single DbContext
    private readonly IRepository<YourDbContext1> _dbContext1Repository; // If multiple DbContext

    public EmployeeService(IRepository repository, IRepository<YourDbContext1> dbContext1Repository)
    {
        _repository = repository;
        _dbContext1Repository = dbContext1Repository;
    }

    public async Task<int> CreateAsync(Employee employee)
    {
        await _repository.AddAsync(employee);
        await _repository.SaveChangesAsync();

        return employee.Id;
    }
}

For more detail documentation, please visit Documentation Wiki

MIT License Copyright (c) 2020 Tanvir Ahmad Arjel Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

该库是EF Core ORM的通用仓储库实现,旨在简化开发人员为每个.NET Core和.NET项目编写仓储层的工作。通过使用这个库,开发人员可以更轻松地管理数据访问层,提高开发效率。 展开 收起
C#
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C#
1
https://gitee.com/hubo/EFCore.GenericRepository.git
git@gitee.com:hubo/EFCore.GenericRepository.git
hubo
EFCore.GenericRepository
EFCore.GenericRepository
master

搜索帮助