BugFree.XCode.Extensions 1.2.2026.806-beta1159

This is a prerelease version of BugFree.XCode.Extensions.
There is a newer version of this package available.
See the version list below for details.
dotnet add package BugFree.XCode.Extensions --version 1.2.2026.806-beta1159
                    
NuGet\Install-Package BugFree.XCode.Extensions -Version 1.2.2026.806-beta1159
                    
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="BugFree.XCode.Extensions" Version="1.2.2026.806-beta1159" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BugFree.XCode.Extensions" Version="1.2.2026.806-beta1159" />
                    
Directory.Packages.props
<PackageReference Include="BugFree.XCode.Extensions" />
                    
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 BugFree.XCode.Extensions --version 1.2.2026.806-beta1159
                    
#r "nuget: BugFree.XCode.Extensions, 1.2.2026.806-beta1159"
                    
#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 BugFree.XCode.Extensions@1.2.2026.806-beta1159
                    
#: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=BugFree.XCode.Extensions&version=1.2.2026.806-beta1159&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=BugFree.XCode.Extensions&version=1.2.2026.806-beta1159&prerelease
                    
Install as a Cake Tool

BugFree.XCode.Extensions

NewLife.XCode 企业级扩展库,提供实体基类增强、多数据源初始化、通用查询条件和代码生成等能力。

项目结构

BugFree.XCode.Extensions/          # 核心类库
├── BugFreeEntity.cs               # 实体基类(字段过滤、敏感字段自动排除)
├── BugFreeEntity.Linq.cs          # Linq 表达式查询扩展
├── BugFreeEntity.QueryCondition.cs # 前端通用查询条件扩展
├── DbOptions.cs                   # 数据库连接及字段过滤配置
├── ExpressionExtensions.cs        # XCode 表达式辅助扩展
├── ServiceCollectionExtensions.cs # XCode DI 初始化扩展
├── Expressions/
│   └── QueryConditionExpressionProvider.cs # QueryCondition→WhereExpression
├── ShardPolicy/
│   ├── FieldValueShardPolicy.cs           # 单字段按值分库/分表策略
│   └── DualFieldValueShardPolicy.cs       # 双字段按值/时间组合分库分表策略
└── Model/
    └── QueryCondition.cs          # 通用查询条件模型

DbEntityTool/                      # 代码生成工具
└── Builder/
    ├── BugFreeEntityBuilder.cs    # 自定义实体生成器
    ├── BugFreeInterfaceBuilder.cs # 自定义接口生成器
    └── BugFreeModelBuilder.cs    # 自定义模型生成器

BugFree.XCode.Extensions.TestProject/ # 单元测试项目
├── DbOptionsTests.cs                 # 敏感字段/删除字段配置测试
├── EntityInitTests.cs                # 实体初始化测试
├── XCodeLinqQueryTests.cs             # XCode LINQ Provider 集成测试
├── QueryConditionWhereExpressionTests.cs # QueryCondition→WhereExpression 测试
└── Fixtures/
    ├── TestUser.cs                   # 表达式测试实体
    └── LinqTestUser.cs               # SQLite LINQ 集成测试实体

核心功能

1. 实体基类增强 (BugFreeEntity<TEntity>)

继承自 XCode Entity<T>,提供:

  • 列表查询字段:自动过滤敏感字段(密码、令牌、密钥等)和超长文本字段,优化列表查询性能
  • 实体查询字段:自动过滤敏感字段,适用于详情页展示
  • 字段缓存热更新:配置变更后调用 InvalidateFieldCache() 使缓存失效

2. 多数据源初始化 (ServiceCollectionExtensions)

通过 ASP.NET Core DI 一键初始化:

builder.Services.InitConnection();

支持多数据源配置(default、report、archive 等),自动注册连接字符串、检查数据库和表结构、初始化实体工厂。

3. 数据库配置 (DbOptions)

支持从配置文件加载:

  • 多数据源连接字符串(加密存储)
  • 敏感字段正则匹配规则
  • 逻辑删除字段识别规则
  • 字段显示长度阈值
  • 拷贝时排除字段列表

4. 通用查询条件

QueryCondition 方式(前端通用模型)
var conditions = new List<QueryCondition>
{
    new() { Field = "Name", Operator = QueryOperator.Contains, Value = "admin" },
    new() { Field = "Enable", Operator = QueryOperator.Equal, Value = true, Logical = QueryLogical.And }
};
var user = BugFreeEntity<User>.Find(conditions);

QueryCondition 直接转换为 XCode WhereExpression,不经过 LINQ 表达式树,继续支持 Equal、NotEqual、GreaterThan、GreaterThanOrEqual、LessThan、LessThanOrEqual、Contains、StartsWith、EndsWith、In、NotIn、Between、IsNull、IsNotNull。 无效字段、敏感字段、空集合及类型转换失败会直接抛出异常,不会静默丢弃条件。

Linq 表达式方式
var users = BugFreeEntity<User>.FindAll(x => x.Name.Contains("张") && x.Age >= 18);
var total = BugFreeEntity<User>.FindCount(x => x.Age >= 18);

LINQ 查询统一复用 XCode Entity<TEntity>.QueryEntityQueryProvider,支持字段位于比较运算符左侧的 &&/||==/!=>/>=/</<=,以及字符串 Contains/StartsWith/EndsWith

FindDataFindMinFindMaxDelete 继续使用 XCode 原生数据库端操作,过滤条件由同一 Provider 解析。 具体表达式支持范围跟随所引用的 XCode 版本;当 BuildWhere 无法生成任何条件时会抛出 NotSupportedException

5. 分库分表策略 (ShardPolicy)

提供 XCode 分片策略扩展:

  • FieldValueShardPolicy:单字段按值分库、分表或分库分表。
  • DualFieldValueShardPolicy:分库字段和分表字段独立配置,每个维度可按字段值或时间字段路由。

6. 代码生成工具 (DbEntityTool)

基于 NewLife.XCode 代码生成框架,提供自定义的实体类、接口和模型类的生成器,支持:

  • 自定义类名模板
  • 接口继承
  • 中/英文文件名
  • 批量生成

单元测试

项目使用 NUnit 框架进行单元测试,覆盖核心逻辑:

  • DbOptions 测试:验证敏感字段匹配、逻辑删除字段识别、边界条件(空值/大小写/部分匹配)
  • 实体初始化测试:验证实体字段注册
  • XCode LINQ 测试:验证官方 Provider、比较/逻辑/字符串表达式、闭包变量、Query 组合查询,以及 Find/FindAll/FindCount/FindMin/FindMax/FindData/Delete 的 SQLite 端到端行为
  • QueryCondition 测试:验证 WhereExpression、逻辑组合、IN/Between/Null 和 JSON 数组转换
  • ExpressionExtensions 测试:验证字段匹配、大小写规则和嵌套 WhereExpression 查找
  • ShardPolicy 测试:验证单字段值分片、双字段值/时间组合分片、缺失条件和异常边界

运行测试

dotnet test BugFree.XCode.Extensions.TestProject

环境要求

  • .NET 8.0 / .NET 10.0
  • NewLife.XCode (>= 12.1.2026.801)
  • BugFree.Configuration

安装

通过 NuGet 安装:

dotnet add package BugFree.XCode.Extensions

快速开始

  1. 配置 Db 配置节(支持多数据源):
{
  "Db": {
    "DbConns": [
      {
        "Name": "default",
        "Type": "MySql",
        "ConnectionString": "Server=localhost;Database=mydb;..."
      }
    ]
  }
}
  1. Program.cs 中初始化:
var builder = WebApplication.CreateBuilder(args);
builder.Services.InitConnection();
  1. 实体类继承 BugFreeEntity<TEntity> 即可使用扩展功能。
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 is compatible.  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 (2)

Showing the top 2 NuGet packages that depend on BugFree.XCode.Extensions:

Package Downloads
BugFree.Controllers.Core

BugFree 控制器核心框架,提供统一的 API/MVC 控制器基类和实体 CRUD 控制器。

BugFree.Controllers.Api

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.20260630 95 8/6/2026
1.2.2026.806-beta1159 122 8/6/2026
1.2.2026.806-beta1125 96 8/6/2026
1.2.2026.616-beta0958 147 6/16/2026
1.2.2026.615-beta1101 116 6/15/2026
1.2.2026.614-beta1942 121 6/14/2026
1.2.2026.613-beta1605 118 6/13/2026
1.1.2026.121-beta1110 147 1/21/2026
1.1.2026.121-beta1048 137 1/21/2026
1.1.2026.116-beta1150 121 1/16/2026
1.0.2026.106-beta1143 134 1/6/2026
1.0.2025.1230-beta1037 138 12/30/2025

BugFreeEntity 实体基类;IModel 数据模型接口;数据库连接与 BugFree.Configuration 集成;XCode LINQ Provider 查询集成;按字段值/时间组合分库分表策略。