jQuery最核心的基础设施之一——数据缓存模块进化史

📅 发布时间:2026/7/5 17:29:55 👁️ 浏览次数:
jQuery最核心的基础设施之一——数据缓存模块进化史
合灯酥尘为什么需要类似Lombok的工具在现代软件开发中我们经常需要编写大量重复的样板代码例如依赖注入为服务类编写构造函数注入代码数据传输对象(DTO)为实体类创建对应的DTO类Builder模式为复杂对象创建Builder构建器属性访问器为私有字段生成公共属性映射方法在不同对象之间进行属性映射这些样板代码不仅占用大量时间编写还容易出错且难以维护。通过代码生成工具我们可以自动化这些重复性工作让开发者专注于业务逻辑的实现。Mud代码生成器Mud代码生成器是一套基于Roslyn的源代码生成器专门针对.NET平台设计提供了类似Lombok的功能甚至更加丰富。它包含两个主要组件Mud.EntityCodeGenerator实体代码生成器用于根据实体类自动生成各种相关代码Mud.ServiceCodeGenerator服务代码生成器用于自动生成服务层相关代码这套工具通过在代码中添加特定的特性(Attribute)标记然后在编译时自动生成相应的代码大大减少了开发者需要手动编写的代码量。核心功能1. 生成构造函数注入代码在.NET的依赖注入系统中构造函数注入是最推荐的依赖注入方式。然而手动编写构造函数注入代码可能会很繁琐特别是当一个类需要注入多个服务时。Mud.ServiceCodeGenerator提供了多种注入特性可以自动生成构造函数注入代码ConstructorInjectAttribute 字段注入使用[ConstructorInject]特性可以将类中已存在的私有只读字段通过构造函数注入初始化[ConstructorInject]public partial class UserService{private readonly IUserRepository _userRepository;private readonly IRoleRepository _roleRepository;// 生成的代码将包含:// public UserService(IUserRepository userRepository, IRoleRepository roleRepository)// {// _userRepository userRepository;// _roleRepository roleRepository;// }}LoggerInjectAttribute 日志注入使用[LoggerInject]特性可以为类注入ILogger类型的日志记录器[LoggerInject]public partial class UserService{// 生成的代码将包含:// private readonly ILogger _logger;//// public UserService(ILoggerFactory loggerFactory)// {// _logger loggerFactory.CreateLogger();// }}CacheInjectAttribute 缓存管理器注入使用[CacheInject]特性可以注入缓存管理器实例[CacheInject]public partial class UserService{// 生成的代码将包含:// private readonly ICacheManager _cacheManager;//// public UserService(ICacheManager cacheManager)// {// _cacheManager cacheManager;// }}UserInjectAttribute 用户管理器注入使用[UserInject]特性可以注入用户管理器实例[UserInject]public partial class UserService{// 生成的代码将包含:// private readonly IUserManager _userManager;//// public UserService(IUserManager userManager)// {// _userManager userManager;// }}OptionsInjectAttribute 配置项注入使用[OptionsInject]特性可以根据指定的配置项类型注入配置实例[OptionsInject(OptionType TenantOptions)]public partial class UserService{// 生成的代码将包含:// private readonly TenantOptions _tenantOptions;//// public UserService(IOptions tenantOptions)// {// _tenantOptions tenantOptions.Value;// }}CustomInjectAttribute 自定义注入使用[CustomInject]特性可以注入任意类型的依赖项[CustomInject(VarType IRepository, VarName _userRepository)][CustomInject(VarType INotificationService, VarName _notificationService)]public partial class UserService{// 生成的代码将包含:// private readonly IRepository _userRepository;// private readonly INotificationService _notificationService;//// public UserService(IRepository userRepository, INotificationService notificationService)// {// _userRepository userRepository;// _notificationService notificationService;// }}组合注入示例多种注入特性可以组合使用生成器会自动合并所有注入需求[ConstructorInject][LoggerInject][CacheInject][UserInject][OptionsInject(OptionType TenantOptions)][CustomInject(VarType IRepository, VarName _userRepository)]public partial class UserService{private readonly IRoleRepository _roleRepository;private readonly IPermissionRepository _permissionRepository;// 生成的代码将包含所有注入项:// private readonly ILogger _logger;// private readonly ICacheManager _cacheManager;// private readonly IUserManager _userManager;// private readonly TenantOptions _tenantOptions;// private readonly IRepository _userRepository;// private readonly IRoleRepository _roleRepository;// private readonly IPermissionRepository _permissionRepository;//// public UserService(// ILoggerFactory loggerFactory,// ICacheManager cacheManager,// IUserManager userManager,// IOptions tenantOptions,// IRepository userRepository,// IRoleRepository roleRepository,// IPermissionRepository permissionRepository)// {// _logger loggerFactory.CreateLogger();// _cacheManager cacheManager;// _userManager userManager;// _tenantOptions tenantOptions.Value;// _userRepository userRepository;// _roleRepository roleRepository;// _permissionRepository permissionRepository;// }}2. Builder模式代码生成Builder模式是一种创建型设计模式能够分步骤创建复杂对象。使用Builder模式可以创建不同表现的对象同时避免构造函数参数过多的问题。Mud.EntityCodeGenerator支持通过[Builder]特性自动生成Builder构建器模式代码////// 客户端信息实体类///[DtoGenerator][Builder][Table(Name sys_client),SuppressSniffer]public partial class SysClientEntity{////// id///[property: Column(Name id, IsPrimary true, Position 1)][property: Required(ErrorMessage id不能为空)]private long? _id;////// 客户端key///[property: Column(Name client_key, Position 3)][property: Required(ErrorMessage 客户端key不能为空)]private string _clientKey;////// 删除标志0代表存在 2代表删除///[property: Column(Name del_flag, Position 10)]private string _delFlag;}基于以上实体将自动生成Builder构建器类////// 的构建者。///public class SysClientEntityBuilder{private SysClientEntity _sysClientEntity new SysClientEntity();////// 设置 属性值。//////属性值/// 返回 实例public SysClientEntityBuilder SetId(long? id){this._sysClientEntity.Id id;return this;}////// 设置 属性值。//////属性值/// 返回 实例public SysClientEntityBuilder SetClientKey(string clientKey){this._sysClientEntity.ClientKey clientKey;return this;}////// 设置 属性值。//////属性值/// 返回 实例public SysClientEntityBuilder SetDelFlag(string delFlag){this._sysClientEntity.DelFlag delFlag;return this;}////// 构建 类的实例。///public SysClientEntity Build(){return this._sysClientEntity;}}使用Builder模式可以链式设置实体属性创建实体对象更加方便var client new SysClientEntityBuilder().SetClientKey(client123).SetDelFlag(0).Build();3. DTO/VO代码生成在现代Web应用开发中数据传输对象(DTO)和视图对象(VO)是常见的设计模式。它们用于在不同层之间传输数据避免直接暴露实体类。Mud.EntityCodeGenerator可以自动生成DTO和VO类////// 客户端信息实体类///[DtoGenerator][Table(Name sys_client),SuppressSniffer]public partial class SysClientEntity{////// id///[property: TableField(Fille FieldFill.Insert, Value FillValue.Id)][property: Column(Name id, IsPrimary true, Position 1)][property: Required(ErrorMessage id不能为空)]private long? _id;////// 客户端key///[property: Column(Name client_key, Position 3)][property: Required(ErrorMessage 客户端key不能为空)][property: ExportProperty(客户端key)][property: CustomVo1, CustomVo2][property: CustomBo1, CustomBo2]private string _clientKey;////// 删除标志0代表存在 2代表删除///[property: Column(Name del_flag, Position 10)][property: ExportProperty(删除标志)][IgnoreQuery]private string _delFlag;}基于以上实体将自动生成以下几类代码VO类 (视图对象)////// 客户端信息实体类///[SuppressSniffer, CompilerGenerated]public partial class SysClientListOutput{////// id///public long? id { get; set; }////// 客户端key///[ExportProperty(客户端key)][CustomVo1, CustomVo2]public string? clientKey { get; set; }////// 删除标志0代表存在 2代表删除///[ExportProperty(删除标志)]public string? delFlag { get; set; }}QueryInput类 (查询输入对象)// SysClientQueryInput.g.cs////// 客户端信息实体类///[SuppressSniffer, CompilerGenerated]public partial class SysClientQueryInput : DataQueryInput{////// id///public long? id { get; set; }////// 客户端key///public string? clientKey { get; set; }////// 删除标志0代表存在 2代表删除///public string? delFlag { get; set; }////// 构建通用的查询条件。///public Expression BuildQueryWhere(){var where LinqExtensions.True();where where.AndIF(this.id ! null, x x.Id this.id);where where.AndIF(!string.IsNullOrEmpty(this.clientKey), x x.ClientKey this.clientKey);where where.AndIF(!string.IsNullOrEmpty(this.delFlag), x x.DelFlag this.delFlag);return where;}}CrInput类 (创建输入对象)// SysClientCrInput.g.cs////// 客户端信息实体类///[SuppressSniffer, CompilerGenerated]public partial class SysClientCrInput{////// 客户端key///[Required(ErrorMessage 客户端key不能为空), CustomBo1, CustomBo2]public string? clientKey { get; set; }////// 删除标志0代表存在 2代表删除///public string? delFlag { get; set; }////// 通用的BO对象映射至实体方法。///public virtual SysClientEntity MapTo(){var entity new SysClientEntity();entity.ClientKey this.clientKey;entity.DelFlag this.delFlag;return entity;}}UpInput类 (更新输入对象)////// 客户端信息实体类///[SuppressSniffer, CompilerGenerated]public partial class SysClientUpInput : SysClientCrInput{////// id///[Required(ErrorMessage id不能为空)]public long? id { get; set; }////// 通用的BO对象映射至实体方法。///public override SysClientEntity MapTo(){var entity base.MapTo();entity.Id this.id;return entity;}}4. 实体映射方法生成在不同对象之间进行属性映射是一项常见但繁琐的工作。Mud.EntityCodeGenerator可以自动生成实体与DTO之间的映射方法////// 通用的实体映射至VO对象方法。///public virtual SysClientListOutput MapTo(){var voObj new SysClientListOutput();voObj.id this.Id;voObj.clientKey this.ClientKey;voObj.delFlag this.DelFlag;return voObj;}