Furion 逻辑删除

Furion 逻辑删除本文就当下最流行的 dotnetcore 框架 furion 中软删除功能的实现进行描述 由于个人水平有限 对 Furion 研究不是特别深入 如有描述不当请各位看官及时批评指正首先我们需要明确使用环境 1 vscode20192 dotnetcore5 0 3 furion2 18 74 数据库 pgsql 这一实现逻辑对数据库依赖不是很敏感 准备工作首先你需要准备好开发环境 并对 furion 有所了解 在自己的项目中搭建好 furion 的运行环境 参考文档 https

本文就当下最流行的dotnet core框架furion中软删除功能的实现进行描述,由于个人水平有限,对Furion研究不是特别深入,如有描述不当请各位看官及时批评指正  

首先我们需要明确使用环境

准备工作  

OK 现在项目环境准备妥当之后,我们开始编写第一个软删除的工具类即软删除字段特性`FakeDeleteAttribute`,本文主要实现的是逻辑删除描述字段为bool型,你可以根据自己的需求进行修改调整,实现代码如下:

  1.  创建软删除特性  `FakeDeleteAttribute`
  2.  创建自定义仓储操作 `PrivateRepository`
  3.  在数据模型中使用 `FakeDelete`
  4.  在数据模型中添加全局过滤 `IEntityTypeBuilder`
  5.  在业务逻辑中进行调用

 1. 创建软删除特性
 

    ///      /// 假删除/软删除     ///      [AttributeUsage(AttributeTargets.Property)]     public class FakeDeleteAttribute : Attribute     {         ///          /// 构造函数         ///此处主要用来标识该特性是否启用         ///          ///          public FakeDeleteAttribute(bool state)         {             State = state;         }         ///          /// 是否启用该特性         ///          public bool State { get; set; }     }

2. 创建自定义仓储操作

///  /// 软删除工具 ///  public static class PrivateRepository { private static string PrimaryKeyName { get; set; } = "Id"; private static string FakeDeleteColumnName { get; set; } = "DeltFlag"; public static bool FakeDelete 
   
     (this IWritableRepository 
    
      repository, long id) where TEntity : class, IPrivateEntity, new() { TEntity t = System.Activator.CreateInstance 
     
       (); t.SetPropertyValue(PrimaryKeyName, id); return repository.FakeDelete(t); } public static bool FakeDelete 
      
        (this IWritableRepository 
       
         repository, TEntity entity) where TEntity : class, IPrivateEntity, new() { entity.SetPropertyValue(FakeDeleteColumnName, true); var res=repository.UpdateInclude(entity,new string[] { FakeDeleteColumnName }, true); if (EntityState.Modified.Equals(res.State)) return true; return false; } /// 
         /// 向对象实体通过反射进行属性赋值 ///  private static TEntity SetPropertyValue 
        
          (this TEntity t, string columnName, object value) { var targetObj = typeof(TEntity); foreach (PropertyInfo sp in targetObj.GetProperties()) { if (sp.Name.Equals(columnName)) { sp.SetValue(t,value); return t; } } return t; } } 
         
        
       
      
     
   

 ///  /// 软删除工具 ///  public static class PrivateRepository { private static string FakeDeleteColumnName = null; public static bool FakeDelete 
   
     (this IWritableRepository 
    
      repository, long id) where TEntity : class, IPrivateEntity, new() { if (id <= 0) return false; TEntity t = System.Activator.CreateInstance 
     
       (); t.SetPropertyValue(id,true); return repository.FakeDelete(t); } public static bool FakeDelete 
      
        (this IWritableRepository 
       
         repository, TEntity entity) where TEntity : class, IPrivateEntity, new() { entity.SetPropertyValue(true,false); if (string.IsNullOrEmpty(FakeDeleteColumnName)) return false; var res = repository.UpdateInclude(entity, new string[] { FakeDeleteColumnName }, true); if (EntityState.Modified.Equals(res.State)) return true; return false; } /// 
         /// 向对象实体通过反射进行属性赋值 ///  private static TEntity SetPropertyValue 
        
          (this TEntity t,object value,bool isKey) { var targetObj = typeof(TEntity); foreach (PropertyInfo sp in targetObj.GetProperties()) { if (sp.IsDefined(typeof(KeyAttribute), true)&&isKey) { sp.SetValue(t, value); break; } else if(sp.IsDefined(typeof(FakeDeleteAttribute), true)&&!isKey) { //判断逻辑删除特性是否启用 if (((FakeDeleteAttribute)sp.GetCustomAttribute(typeof(FakeDeleteAttribute))).State) { FakeDeleteColumnName = sp.Name; sp.SetValue(t, value); break; } } } return t; } } 
         
        
       
      
     
   

public class TestEntity:IEntity, IEntityTypeBuilder 
   
         {         [Key]         public long Id { get; set; }         public string Value { get; set; }         //使用FakeDelete特性         [FakeDelete(true),JsonIgnore]         public string DeleteMark { get; set; }         //添加全局过滤         public void Configure(EntityTypeBuilder 
    
      entityBuilder, DbContext dbContext, Type dbContextLocator)         {             entityBuilder.HasQueryFilter(u => !u.DeltFlag);         }     } 
     
   

       private readonly IWritableRepository 
   
     write;         public TestService(IRepository 
    
      testRepository)         {             write = personRepository.Constraint 
     
       >();         }          /// 
               /// 逻辑删除         ///          /// 
               /// 
                [UnitOfWork]         public bool DeleteLogic(long id)         {            return = write.FakeDelete(id);         }  
      
     
   

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/199210.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月26日 下午1:36
下一篇 2026年3月26日 下午1:36


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号