LindDotNetCore~ISoftDelete軟刪除介面
阿新 • • 發佈:2018-12-30
概念
ISoftDelete即軟刪除,資料在進行delete後不會從資料庫清除,而只是標記一個狀態,在業務範圍裡都不能獲取到這個資料,這在ORM框架裡還是比較容易實現的,對傳統的ado來說需要對sql統一進行攔截和條件過濾.
實施步驟
- 實體繼承ISoftDelete
- 資料上下文實現對IsDeleted的過濾
- 對刪除方法進行調整,新增對ISoftDelete的支援
程式碼實現
1 實體繼承ISoftDelete
protected override void OnModelCreating(ModelBuilder modelBuilder) {foreach (var entityType in modelBuilder.Model.GetEntityTypes()) { // 1. Add the IsDeleted property entityType.GetOrAddProperty("IsDeleted", typeof(bool)); // 2. Create the query filter var parameter = Expression.Parameter(entityType.ClrType); // 3. EF.Property<bool>(post, "IsDeleted")var propertyMethodInfo = typeof(EF).GetMethod("Property").MakeGenericMethod(typeof(bool)); var isDeletedProperty = Expression.Call(propertyMethodInfo, parameter, Expression.Constant("IsDeleted")); // 4. EF.Property<bool>(post, "IsDeleted") == false BinaryExpression compareExpression = Expression.MakeBinary(ExpressionType.Equal, isDeletedProperty, Expression.Constant(false)); // 5. post => EF.Property<bool>(post, "IsDeleted") == false var lambda = Expression.Lambda(compareExpression, parameter); modelBuilder.Entity(entityType.ClrType).HasQueryFilter(lambda);
2 資料上下文實現對IsDeleted的過濾
public class ProductInfo : EntityBase, ISoftDelete { public string Title { get; set; } public decimal Amount { get; set; } public int Discount { get; set; } public int Inventory { get; set; } public DateTime DeletedDate { get; set; } public string DeletedUser { get; set; } public bool IsDeleted { get; set; } /// <summary> /// domain method /// </summary> /// <returns></returns> public decimal GetSaleAmount() { return Amount * Discount / 100; } }
3 對刪除方法進行調整,新增對ISoftDelete的支援
程式碼來自Lind.DotNetCore.Repository.EFRepository型別
public void Delete(TEntity item) { if (item != null) { if (typeof(ISoftDelete).IsAssignableFrom(item.GetType())) { Db.Entry(item).State = EntityState.Modified; var delEntity = item as ISoftDelete; delEntity.DeletedDate = DateTime.Now; delEntity.IsDeleted = true; } else { //物理刪除 Db.Set<TEntity>().Attach(item as TEntity); Db.Entry(item).State = EntityState.Deleted; Db.Set<TEntity>().Remove(item as TEntity); } this.SaveChanges(); } }
上面幾行程式碼實現了對軟刪除的完整支援,從ISoftDelete介面到資料上下文裡的過濾IsDeleted,再到優化後的Delete()方法,一切看上去都很優雅!
對於軟刪除支援的框架也很多,像abp,eshopconationer,linddotnetcore等!
歡迎大家的閱讀與思考!