Galosys.Foundation.SqlSugarCore
26.7.31.1
dotnet add package Galosys.Foundation.SqlSugarCore --version 26.7.31.1
NuGet\Install-Package Galosys.Foundation.SqlSugarCore -Version 26.7.31.1
<PackageReference Include="Galosys.Foundation.SqlSugarCore" Version="26.7.31.1" />
<PackageVersion Include="Galosys.Foundation.SqlSugarCore" Version="26.7.31.1" />
<PackageReference Include="Galosys.Foundation.SqlSugarCore" />
paket add Galosys.Foundation.SqlSugarCore --version 26.7.31.1
#r "nuget: Galosys.Foundation.SqlSugarCore, 26.7.31.1"
#:package Galosys.Foundation.SqlSugarCore@26.7.31.1
#addin nuget:?package=Galosys.Foundation.SqlSugarCore&version=26.7.31.1
#tool nuget:?package=Galosys.Foundation.SqlSugarCore&version=26.7.31.1
Galosys.Foundation.SqlSugarCore
成熟度: 🟡 可用 — 功能完整,测试或文档可能不完善
SqlSugar ORM 框架集成模块,为 Galosys.Foundation 提供基于 SqlSugar 的数据访问层实现。
简介
Galosys.Foundation.SqlSugar 是 SqlSugar ORM 框架在 Galosys.Foundation 框架中的集成模块。它提供了自动化的数据库连接管理、实体映射、审计字段填充、多租户支持、单元工作模式等企业级功能。
特性
- 多数据库支持:自动识别并支持 SqlServer、MySql、PostgreSQL、Oracle、SQLite
- 自动审计字段:自动填充创建时间、修改时间、创建人、修改人等审计字段
- 多租户支持:基于
IMultiTenancy接口实现数据隔离 - 多应用支持:基于
IMultiApplication接口实现应用级数据隔离 - 软删除过滤:自动过滤已删除数据(
IDeletable接口) - 多种ID生成:支持 Snowflake、ULID、NanoID、UUID 等分布式ID生成策略
- SQL 日志:自动记录执行的 SQL 语句
- Snake Case 命名:自动将 PascalCase 属性名转换为 snake_case 列名
- 仓储模式:提供
SqlSugarRepository<T>泛型仓储 - 单元工作模式:提供
IUnitOfWork接口实现事务管理
安装
<ItemGroup>
<PackageReference Include="Galosys.Foundation.SqlSugarCore" Version="x.x.x" />
</ItemGroup>
配置
appsettings.json
{
"ConnectionStrings": {
"Default": {
"ConnectionString": "Server=localhost;Database=mydb;User=root;Password=password;",
"Provider": "MySqlConnector"
}
}
}
支持的数据库 Provider
| Provider | 数据库类型 |
|---|---|
system.data.sqlClient / microsoft.data.sqlClient |
SqlServer |
MySqlConnector |
MySql |
Npgsql |
PostgreSQL |
Oracle.ManagedDataAccess.Client |
Oracle |
Microsoft.Data.Sqlite |
SQLite |
使用示例
基本 CRUD 操作
public class ProductService
{
private readonly SqlSugarRepository<Product> _repository;
private readonly IUnitOfWork _unitOfWork;
public ProductService(
SqlSugarRepository<Product> repository,
IUnitOfWork unitOfWork)
{
_repository = repository;
_unitOfWork = unitOfWork;
}
// 查询
public async Task<List<Product>> GetProductsAsync()
{
return await _repository.AsQueryable()
.Where(p => !p.Deleted) // 软删除过滤
.ToListAsync();
}
// 新增
public async Task AddProductAsync(Product product)
{
await _repository.InsertAsync(product);
}
// 更新
public async Task UpdateProductAsync(Product product)
{
await _repository.UpdateAsync(product);
}
// 删除(软删除)
public async Task DeleteProductAsync(long id)
{
var product = await _repository.GetByIdAsync(id);
if (product != null)
{
product.Deleted = true;
await _repository.UpdateAsync(product);
}
}
}
事务管理
public class OrderService
{
private readonly SqlSugarRepository<Order> _orderRepo;
private readonly SqlSugarRepository<OrderItem> _itemRepo;
private readonly IUnitOfWork _unitOfWork;
public async Task CreateOrderAsync(Order order, List<OrderItem> items)
{
await _unitOfWork.BeginTransactionAsync();
try
{
await _orderRepo.InsertAsync(order);
foreach (var item in items)
{
item.OrderId = order.Id;
await _itemRepo.InsertAsync(item);
}
await _unitOfWork.CommitAsync();
}
catch
{
await _unitOfWork.RollbackAsync();
throw;
}
}
}
实体定义
[Table("products")]
public class Product : FullEntity<long>
{
[SnowflakeId]
public new long Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
[CreatedAt]
public DateTime CreatedAt { get; set; }
[CreatorId]
public long CreatorId { get; set; }
[LastModifiedAt]
public DateTime? LastModifiedAt { get; set; }
}
Outbox 支持
SqlSugarOutboxStore 实现了 Outbox 模式的存储层,支持在 SqlSugar ORM 环境下使用消息出库模式。
注册方式
services.AddMessageBus();
services.AddSqlSugarOutboxStore(); // 替代 AddEfCoreOutboxStore
SqlSugarOutboxStore 随模块自动注册(SqlSugarModule.ConfigureServices 自带 AddSqlSugarOutboxStore() 调用)。如需手动控制,在注册 MessageBus 后再调用一次即可。
工作原理
PublishAsync → PendingMsgCol.Add(msg)
业务操作 → await _uow.CommitAsync()
├── PendingMsgCol.Drain() ← 自动排出
├── SqlSugarOutboxStore.SaveAsync() ← 事务内写入
└── ISqlSugarClient.Ado.CommitTranAsync()
在 SqlSugarUnitOfWork.CommitAsync() 中自动排出 PendingMsgCol,与业务数据在同一事务内提交,避免幽灵消息。
数据库支持
| 特性 | PostgreSQL | 其他数据库 |
|---|---|---|
| TryAcquireBatchAsync | SELECT ... FOR UPDATE SKIP LOCKED |
两步法 SELECT + UPDATE |
| 批量状态更新 | SetColumns + WHERE id IN (...) |
同上 |
核心类
| 类名 | 描述 |
|---|---|
SqlSugarModule |
模块入口,实现 IModule 接口 |
SqlSugarServiceCollectionExtensions |
DI 扩展方法,注册 SqlSugar 服务 |
SqlSugarRepository<T> |
泛型仓储,继承自 SimpleClient<T> |
SqlSugarUnitOfWork |
单元工作实现,提供事务管理 |
SqlSugarOutboxStore |
IOutboxStore 实现,支持批量和 SKIP LOCKED |
SqlSugarOutboxServiceCollectionExtensions |
AddSqlSugarOutboxStore() DI 扩展 |
依赖
- Galosys.Foundation.Data:基础数据模块,提供
IRepository、IUnitOfWork等接口 - Galosys.Foundation.Core:核心模块,提供审计特性、多租户接口、ID生成器、Outbox 模式基础设施等
- SqlSugarCore:SqlSugar ORM 核心库
自动审计与实体映射
审计字段:模块会自动填充 [SnowflakeId]、[Ulid]、[Nanoid]、[Uuid] 等ID字段,以及 [CreatedAt]、[CreatorId]、[CreatorName]、[LastModifiedAt]、[LastModifierId]、[LastModifierName] 等审计字段。
实体映射:支持 [Table]、[Column]、[Key]、[DatabaseGenerated(Identity)]、[NotMapped]、[Description]、[DefaultValue]、[Required] 等标准特性进行实体映射配置。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. |
-
net10.0
- Galosys.Foundation.Data (>= 26.7.31.1)
- sqlsugarcore (>= 5.1.4.158)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.