本文就当下最流行的dotnet core框架furion中软删除功能的实现进行描述,由于个人水平有限,对Furion研究不是特别深入,如有描述不当请各位看官及时批评指正
首先我们需要明确使用环境
准备工作
OK 现在项目环境准备妥当之后,我们开始编写第一个软删除的工具类即软删除字段特性`FakeDeleteAttribute`,本文主要实现的是逻辑删除描述字段为bool型,你可以根据自己的需求进行修改调整,实现代码如下:
- 创建软删除特性 `FakeDeleteAttribute`
- 创建自定义仓储操作 `PrivateRepository`
- 在数据模型中使用 `FakeDelete`
- 在数据模型中添加全局过滤 `IEntityTypeBuilder`
- 在业务逻辑中进行调用
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
