A:增加数据库配置和使用
This commit is contained in:
1 parent
53d1dd2dbf
commit
f7660c49e8
86 files changed
+7401
-271
No files matched your search
@@ -0,0 +1,272 @@
|
||||
using Sharp.Data;
|
||||
using Sharp.Data.Context;
|
||||
|
||||
namespace Data.Test;
|
||||
|
||||
/// <summary>
|
||||
/// 演示 01: Sharp.Data 基础使用
|
||||
///
|
||||
/// 本测试类演示:
|
||||
/// 1. 如何创建和配置 DataContext
|
||||
/// 2. 最简单的 3 行代码启动方式
|
||||
/// 3. 各种配置选项的使用
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public class _01_BasicUsageTests
|
||||
{
|
||||
/// <summary>
|
||||
/// 演示: 最简单的使用方式 (3 行代码)
|
||||
///
|
||||
/// 这是最基础的用法,适合快速开始
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void Demo_MinimalSetup_ThreeLines()
|
||||
{
|
||||
// 第 1 行: 创建构建器
|
||||
var builder = new DataContextBuilder<TestDbContext>();
|
||||
|
||||
// 第 2 行: 配置数据库 (使用内存数据库用于演示)
|
||||
builder.UseInMemoryDatabase("MinimalDemo");
|
||||
|
||||
// 第 3 行: 构建 DataContext
|
||||
using var db = builder.Build();
|
||||
|
||||
// 完成!现在可以使用了
|
||||
Assert.IsNotNull(db);
|
||||
Assert.IsNotNull(db.Context);
|
||||
|
||||
Console.WriteLine("✓ 3 行代码完成 Data 模块配置");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 演示: 使用 SQLite 数据库
|
||||
///
|
||||
/// SQLite 是最常用的轻量级数据库,适合桌面应用
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void Demo_UsingSQLite()
|
||||
{
|
||||
var builder = new DataContextBuilder<TestDbContext>();
|
||||
|
||||
// 配置 SQLite 数据库
|
||||
builder.UseSqlite("Data Source=test.db");
|
||||
|
||||
using var db = builder.Build();
|
||||
|
||||
Assert.IsNotNull(db);
|
||||
Console.WriteLine("✓ 使用 SQLite 数据库配置成功");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 演示: 启用所有功能的完整配置
|
||||
///
|
||||
/// 演示如何配置审计、软删除、自动迁移等功能
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void Demo_FullConfiguration()
|
||||
{
|
||||
var builder = new DataContextBuilder<TestDbContext>();
|
||||
|
||||
// 链式配置所有功能
|
||||
builder.UseInMemoryDatabase("FullConfigDemo")
|
||||
.UseAudit(true) // 启用审计功能
|
||||
.UseSoftDelete(true) // 启用软删除功能
|
||||
.UseAutoMigration(true) // 启动时自动迁移数据库
|
||||
.UseCurrentUser("admin") // 设置当前用户(用于审计)
|
||||
.EnableDetailedErrors(true) // 启用详细错误信息
|
||||
.EnableSensitiveDataLogging(true); // 启用敏感数据日志(仅开发环境)
|
||||
|
||||
using var db = builder.Build();
|
||||
|
||||
Assert.IsNotNull(db);
|
||||
Console.WriteLine("✓ 完整配置成功");
|
||||
Console.WriteLine(" - 审计功能: 已启用");
|
||||
Console.WriteLine(" - 软删除功能: 已启用");
|
||||
Console.WriteLine(" - 自动迁移: 已启用");
|
||||
Console.WriteLine(" - 当前用户: admin");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 演示: 动态获取当前用户
|
||||
///
|
||||
/// 在真实应用中,用户通常来自认证系统
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void Demo_DynamicCurrentUser()
|
||||
{
|
||||
var builder = new DataContextBuilder<TestDbContext>();
|
||||
|
||||
// 模拟从认证系统获取用户
|
||||
var currentUser = "zhang.san"; // 实际应用中可能来自 HttpContext 或其他认证系统
|
||||
|
||||
builder.UseInMemoryDatabase("DynamicUserDemo")
|
||||
.UseAudit(true)
|
||||
.UseCurrentUserProvider(() =>
|
||||
{
|
||||
// 这里可以放置获取当前用户的逻辑
|
||||
// 例如: HttpContext.Current?.User?.Identity?.Name
|
||||
return currentUser;
|
||||
});
|
||||
|
||||
using var db = builder.Build();
|
||||
|
||||
Assert.IsNotNull(db);
|
||||
Console.WriteLine($"✓ 动态用户配置成功,当前用户: {currentUser}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 演示: 只启用部分功能
|
||||
///
|
||||
/// 所有功能都是可选的,按需启用
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void Demo_SelectiveFeatures()
|
||||
{
|
||||
var builder = new DataContextBuilder<TestDbContext>();
|
||||
|
||||
// 只启用审计,不启用软删除
|
||||
builder.UseInMemoryDatabase("SelectiveDemo")
|
||||
.UseAudit(true) // 启用审计
|
||||
.UseSoftDelete(false); // 不启用软删除
|
||||
|
||||
using var db = builder.Build();
|
||||
|
||||
Assert.IsNotNull(db);
|
||||
Console.WriteLine("✓ 选择性功能配置成功");
|
||||
Console.WriteLine(" - 审计功能: 已启用");
|
||||
Console.WriteLine(" - 软删除功能: 未启用");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 演示: 获取仓储对象
|
||||
///
|
||||
/// DataContext 提供统一的仓储访问入口
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void Demo_GetRepository()
|
||||
{
|
||||
var builder = new DataContextBuilder<TestDbContext>();
|
||||
builder.UseInMemoryDatabase("RepositoryDemo");
|
||||
|
||||
using var db = builder.Build();
|
||||
|
||||
// 获取不同实体的仓储
|
||||
var productRepo = db.Repository<Product>();
|
||||
var categoryRepo = db.Repository<Category>();
|
||||
var orderRepo = db.Repository<Order>();
|
||||
|
||||
Assert.IsNotNull(productRepo);
|
||||
Assert.IsNotNull(categoryRepo);
|
||||
Assert.IsNotNull(orderRepo);
|
||||
|
||||
Console.WriteLine("✓ 仓储获取成功");
|
||||
Console.WriteLine(" - Product 仓储");
|
||||
Console.WriteLine(" - Category 仓储");
|
||||
Console.WriteLine(" - Order 仓储");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 演示: 访问底层 DbContext
|
||||
///
|
||||
/// 可以直接访问 EF Core DbContext 进行高级操作
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void Demo_AccessUnderlyingDbContext()
|
||||
{
|
||||
var builder = new DataContextBuilder<TestDbContext>();
|
||||
builder.UseInMemoryDatabase("DirectAccessDemo");
|
||||
|
||||
using var db = builder.Build();
|
||||
|
||||
// 访问底层 DbContext
|
||||
var context = db.Context;
|
||||
|
||||
Assert.IsNotNull(context);
|
||||
Assert.IsInstanceOfType<TestDbContext>(context);
|
||||
|
||||
Console.WriteLine("✓ 可以访问底层 DbContext");
|
||||
Console.WriteLine(" - 用于执行原生 SQL");
|
||||
Console.WriteLine(" - 用于事务管理");
|
||||
Console.WriteLine(" - 用于高级查询");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 演示: 配置不同的数据库提供程序
|
||||
///
|
||||
/// Sharp.Data 支持多种数据库
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void Demo_DifferentDatabaseProviders()
|
||||
{
|
||||
// 1. SQLite
|
||||
var sqliteBuilder = new DataContextBuilder<TestDbContext>();
|
||||
sqliteBuilder.UseSqlite("Data Source=app.db");
|
||||
Console.WriteLine("✓ SQLite: Data Source=app.db");
|
||||
|
||||
// 2. SQL Server
|
||||
var sqlServerBuilder = new DataContextBuilder<TestDbContext>();
|
||||
sqlServerBuilder.UseSqlServer("Server=.;Database=MyApp;Trusted_Connection=True;");
|
||||
Console.WriteLine("✓ SQL Server: 连接字符串已配置");
|
||||
|
||||
// 3. MySQL
|
||||
var mysqlBuilder = new DataContextBuilder<TestDbContext>();
|
||||
mysqlBuilder.UseMySql("server=localhost;database=myapp;user=root;password=123", "8.0.35");
|
||||
Console.WriteLine("✓ MySQL: 连接字符串已配置");
|
||||
|
||||
// 4. PostgreSQL
|
||||
var npgsqlBuilder = new DataContextBuilder<TestDbContext>();
|
||||
npgsqlBuilder.UseNpgsql("Host=localhost;Database=myapp;Username=postgres;Password=123");
|
||||
Console.WriteLine("✓ PostgreSQL: 连接字符串已配置");
|
||||
|
||||
// 5. InMemory (测试用)
|
||||
var memoryBuilder = new DataContextBuilder<TestDbContext>();
|
||||
memoryBuilder.UseInMemoryDatabase("TestDb");
|
||||
using var db = memoryBuilder.Build();
|
||||
Console.WriteLine("✓ InMemory: 内存数据库用于测试");
|
||||
|
||||
Assert.IsNotNull(db);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 演示: 正确释放资源
|
||||
///
|
||||
/// DataContext 实现了 IDisposable,使用后要释放
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void Demo_ProperResourceDisposal()
|
||||
{
|
||||
// 方式1: 使用 using 语句 (推荐)
|
||||
using (var db = CreateDataContext())
|
||||
{
|
||||
// 使用 db 进行操作
|
||||
var repo = db.Repository<Product>();
|
||||
// ... 操作
|
||||
} // 自动释放
|
||||
|
||||
Console.WriteLine("✓ 方式1: using 语句自动释放");
|
||||
|
||||
// 方式2: 手动释放
|
||||
var db2 = CreateDataContext();
|
||||
try
|
||||
{
|
||||
var repo = db2.Repository<Product>();
|
||||
// ... 操作
|
||||
}
|
||||
finally
|
||||
{
|
||||
db2.Dispose(); // 手动释放
|
||||
}
|
||||
|
||||
Console.WriteLine("✓ 方式2: 手动释放");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 辅助方法: 创建测试用的 DataContext
|
||||
/// </summary>
|
||||
private IDataContext CreateDataContext()
|
||||
{
|
||||
var builder = new DataContextBuilder<TestDbContext>();
|
||||
builder.UseInMemoryDatabase($"TestDb_{Guid.NewGuid()}");
|
||||
return builder.Build();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user