Jeff.Repository 0.7.3

dotnet add package Jeff.Repository --version 0.7.3
                    
NuGet\Install-Package Jeff.Repository -Version 0.7.3
                    
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="Jeff.Repository" Version="0.7.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Jeff.Repository" Version="0.7.3" />
                    
Directory.Packages.props
<PackageReference Include="Jeff.Repository" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Jeff.Repository --version 0.7.3
                    
#r "nuget: Jeff.Repository, 0.7.3"
                    
#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.
#:package Jeff.Repository@0.7.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Jeff.Repository&version=0.7.3
                    
Install as a Cake Addin
#tool nuget:?package=Jeff.Repository&version=0.7.3
                    
Install as a Cake Tool

概述

本类库对数据库常见的CRUD方法进行了封装, 简化了数据访问操作, 目前支持 EF Core。通过依赖注入使用。

快速开始

1. 服务注册

builder.Services.AddJeffRepository(cfg =>
{
    //设置主键生成算法,默认为PKAlgo.None。可选。
    cfg.PrimaryKeyAlgo = PKAlgo.SnowFlake;  //设置雪花算法为新增主键的默认方法

    //设置全局软删除、创建时间、更新时间、乐观锁字段,CRUD时自动处理。可选。
    cfg.SoftDeleteProperty = "IsDeleted";   //软删除字段,查询时自动过滤,删除时自动更新
    cfg.CreateTimeProperty = "CreateTime";  //CreateTime字段,新增时自动填充
    cfg.UpdateTimeProperty = "UpdateTime";  //UpdateTime字段,更新时自动填充
    cfg.VersionProperty = "Version";        //乐观锁字段,更新时自动检查
});

 

2. 创建实体类

public class Pet : Entity<long>
{
    public long PetId { get; set; }
    public string PetName { get; set; }
    public int Age { get; set; }
    public string Breed { get; set; }
    public long? StoreId { get; set; }

    public DateTime? CreateTime { get; set; }
    public DateTime? UpdateTime { get; set; }
    public bool? IsDeleted { get; set; }
    public int? Version { get; set; }
}

//数据库上下文
public class PetContext : EFContext
{
    public PetContext(DbContextOptions options) : base(options) {}
    public DbSet<Pet> Pet { get; set; }
}

 

3. 创建仓储类

public class PetRepository : EFRepository<Pet, long, PetContext>
{
    public PetRepository(PetContext dbContext) : base(dbContext) {}
}

 

4. 使用仓储类

[Route("[controller]")]
[ApiController]
public class DemoController : ControllerBase
{
    //通过依赖注入使用仓储类
    [HttpGet]
    public object Get([FromService] PetRepository repo)
    {
        return repo.Find(1)?.PetName;
    }

    //直接注入 EFRepository
    [HttpGet("2")]
    public object Get2([FromService] EFRepository<Pet, long, PetContext> repo)
    {
        return repo.Find(1)?.PetName;
    }
}

 

CRUD 示例

//查询
Pet pet = petRepo.Find(1);
var pets = petRepo.GetAll($"select * from pet").ToList();    //FormattableString
var pets = petRepo.GetAll().Where(pet => pet.Age > 5).ToList();

//排序 + 分页
PagedList<Pet> pets = petRepo.GetAll().OrderBy("age,name", "asc,asc").ToPaged(page, limit);   

//筛选
var pets = petRepo.GetAll().WhereIf(age != null, pet => pet.Age > age).ToList();
var pets = petRepo.GetAll().Filter(
    new {                                                          //定义筛选对象,支持多种类型筛选
        CreateTime = times.Split(',').ConvertToArray<DateTime>(),  //数组筛选
        Age = (minAge, maxAge),                                    //区间筛选
        Breed = $"_{breed}%"                                       //模糊筛选,支持通配
        StoreId = 12345                                            //精确值
    })
.ToList();

//新增
petRepo.Add(new Pet { PetName = "Tom", Age = 10});  //新增记录,不自动生成主键
petRepo.Add(new Pet { PetName = "Tom", Age = 10}, PKAlgo.GUID);  //新增记录,自动生成可排序的GUID作为主键
petRepo.UnitOfWork.SaveChanges();

//部分更新
petRepo.UpdateByDto(new PetUpdateDto());                           //通过 IUpdateDto 更新,推荐使用
petRepo.UpdateByObj(new {PetId = 2, PetName = "Jerry"});           //通过对象更新, 只更新同名非null属性
petRepo.Update(new Pet {PetId = 1, PetName = "Tom"}, ["PetName"]); //通过实体更新,只更新指定属性
petRepo.UnitOfWork.SaveChanges();

//删除
petRepo.Delete(1);                 //主键删除
petRepo.Delete([1,2,3]);           //主键批量删除
petRepo.Delete(p => p.Age > 20);   //条件删除
petRepo.Delete(new Pet {PetId = 1, PetName = "Tom"});   //实体删除
petRepo.UnitOfWork.SaveChanges();

新增/更新/删除 必须调用 repo.UnitOfWork.SaveChanges() 才能生效。 针对ef core,UnitOfWork等同于DbContext。  

IUpdateDto<TEntity>

IUpdateDto<TEntity> 是用于更新实体类的 DTO 接口,通过实现此接口,创建可空类型的同名属性,实现对象的部分更新。默认只更新非空字段。

IUpdateDto<TEntity> 支持几种特性:

  • [UpdateNull]:即使字段为 null 也会更新。
  • [UpdateIgnoreValue]:忽略指定值的更新。
  • [UpdateIgnoreWhiteSpace]:忽略空格和空字符串的更新。
  • [UpdateColumn]:指定映射的实体类属性名,默认映射同名属性。
// This DTO is used for updating the Pet entity, defaulting to updating non-null fields.
public class PetUpdateDto : IUpdateDto<Pet>
{
    public long Id { get; set; }

    [UpdateColumn("PetName")]
    public string? Name { get; set; }

    [UpdateNull]
    public int? Age { get; set; }

    public bool? IsDeleted { get; set; }

    public DateTime? CreateTime { get; set; }

    public DateTime? UpdateTime { get; set; }

    public int? Version { get; set; }
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows 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 Jeff.Repository:

Package Downloads
Jeff.Utility

本框架用于 Asp.Net Core WebApi 项目,核心是开箱即用的通用权限服务。JeffUtility 本身只是一个类库,既适用于单体程序,也可用于多项目方案。使用文档查看自述文件。

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.7.3 245 5/7/2025
0.7.1 253 4/25/2025
0.7.0 191 4/24/2025
0.6.9 246 4/15/2025
0.6.8 203 4/15/2025
0.6.6 150 4/11/2025
0.6.4 111 2/24/2025
0.6.2 119 2/10/2025
0.6.1 120 12/25/2024
0.6.0 115 12/21/2024
0.5.9 122 12/20/2024
0.5.0 113 12/18/2024
0.2.0 190 12/2/2024