Files
sharp-basic/test/Data.Test/TestEntities.cs
T
2025-12-30 09:17:25 +08:00

159 lines
4.4 KiB
C#

using Sharp.Data.Entities;
namespace Data.Test;
// ============================================================
// 演示各种实体定义方式
// ============================================================
#region 方式1: 使用 AuditableEntity 基类 (最常用,包含审计和软删除)
/// <summary>
/// 演示: 使用 AuditableEntity 基类
/// 优点: 自动包含 Id、审计字段、软删除字段
/// 适用场景: 需要审计和软删除功能的业务实体
/// </summary>
public class Product : AuditableEntity
{
public string Name { get; set; } = string.Empty;
public decimal Price { get; set; }
public string? Description { get; set; }
public int Stock { get; set; }
}
#endregion
#region 方式2: 使用 Entity 基类 (只需要 Id,不需要审计)
/// <summary>
/// 演示: 使用 Entity 基类 (Guid 主键)
/// 优点: 只包含 Id,更简洁
/// 适用场景: 简单实体,不需要审计功能
/// </summary>
public class Category : Entity
{
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
}
/// <summary>
/// 演示: 使用 EntityInt 基类 (Int 主键)
/// 适用场景: 需要自增 Int 主键的实体
/// </summary>
public class Tag : EntityInt
{
public string Name { get; set; } = string.Empty;
public string Color { get; set; } = "#000000";
}
#endregion
#region 方式3: 完全自定义实体 (不使用任何基类)
/// <summary>
/// 演示: 完全自定义实体,不使用任何基类
/// 优点: 完全自主控制,不依赖框架
/// 适用场景:
/// - 已有实体模型,不想改动
/// - 需要特殊的主键类型
/// - 不需要框架提供的任何功能
/// </summary>
public class CustomProduct
{
public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
public decimal Price { get; set; }
public DateTime CreatedDate { get; set; }
// 可以有自己的业务逻辑
public bool IsExpensive => Price > 1000;
}
/// <summary>
/// 演示: 使用字符串作为主键的自定义实体
/// </summary>
public class Region
{
public string Code { get; set; } = string.Empty; // 使用字符串作为主键,如 "CN", "US"
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
}
#endregion
#region 方式4: 实现接口但不使用基类 (灵活控制)
/// <summary>
/// 演示: 实现审计接口但不使用基类
/// 优点: 可以自己控制字段名称和类型,同时享受审计功能
/// 适用场景: 需要审计功能,但想自定义实现
/// </summary>
public class Order : IAuditableEntity, ISoftDeletable
{
public Guid Id { get; set; }
public string OrderNumber { get; set; } = string.Empty;
public decimal TotalAmount { get; set; }
// 实现审计接口
public DateTime CreatedAt { get; set; }
public string? CreatedBy { get; set; }
public DateTime? UpdatedAt { get; set; }
public string? UpdatedBy { get; set; }
// 实现软删除接口
public bool IsDeleted { get; set; }
public DateTime? DeletedAt { get; set; }
public string? DeletedBy { get; set; }
}
#endregion
#region 方式5: 只实现软删除接口
/// <summary>
/// 演示: 只实现软删除接口,不要审计功能
/// 适用场景: 只需要软删除,不需要记录创建/修改信息
/// </summary>
public class Notification : ISoftDeletable
{
public Guid Id { get; set; }
public string Title { get; set; } = string.Empty;
public string Content { get; set; } = string.Empty;
public DateTime CreatedAt { get; set; }
// 只实现软删除接口
public bool IsDeleted { get; set; }
public DateTime? DeletedAt { get; set; }
public string? DeletedBy { get; set; }
}
#endregion
#region 方式6: 值对象和复杂实体
/// <summary>
/// 演示: 包含值对象的实体
/// </summary>
public class Customer : AuditableEntity
{
public string Name { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
// 值对象
public Address? ShippingAddress { get; set; }
public Address? BillingAddress { get; set; }
}
/// <summary>
/// 值对象: 地址
/// </summary>
public class Address
{
public string Street { get; set; } = string.Empty;
public string City { get; set; } = string.Empty;
public string State { get; set; } = string.Empty;
public string ZipCode { get; set; } = string.Empty;
public string Country { get; set; } = string.Empty;
}
#endregion