public void SoftCascadeDelete(ISoftDeletable model) { var deleteTime = DateTime.UtcNow; this.SoftCascadeDelete(model, deleteTime); context.SaveChanges(); }
public void Delete(ISoftDeletable item, DateTime now, bool saveChanges) { item.DeletedOn = now; item.IsDeleted = true; if (saveChanges) { context.SaveChanges(); } }
public void Restore(ISoftDeletable entity) { var entry = ChangeTracker.Entries().First(en => en.Entity == entity); if ((bool)entry.Property(nameof(ISoftDeletable.IsDeleted)).CurrentValue == true) { entry.Property(nameof(ISoftDeletable.IsDeleted)).CurrentValue = false; } }
public void HardRemove(ISoftDeletable entity) { var entry = ChangeTracker.Entries().First(en => en.Entity == entity); // a hack to detect hard deleted entities entry.CurrentValues[SoftDeleteConstants.DeletedDateProperty] = DateTime.MinValue; Remove(entity); }
public void Restore(ISoftDeletable entity) { var entry = ChangeTracker.Entries().First(en => en.Entity == entity); if ((DateTime?)entry.Property(SoftDeleteConstants.DeletedDateProperty).CurrentValue != null) { entry.Property(SoftDeleteConstants.DeletedDateProperty).CurrentValue = null; } }
private void HandleSoftDeletableEntities() { IEnumerable <DbEntityEntry> entries = ChangeTracker.Entries().Where(x => x.Entity is ISoftDeletable && x.State == EntityState.Deleted); foreach (var entry in entries) { entry.State = EntityState.Modified; ISoftDeletable entity = (ISoftDeletable)entry.Entity; entity.IsDeleted = true; } }
/// <summary> /// Get Domain by ID /// </summary> /// <param name="id"></param> /// <returns></returns> public T GetByID(TIdentifier id) { using (ConnectionScope.Enter()) { T entity; entity = (T)NHibernateSession.Get(persitentType, id); ISoftDeletable softDeletable = entity as ISoftDeletable; if (softDeletable != null && softDeletable.DeletedDate != null) { entity = default(T); } return(entity); } }
public virtual void Delete(T obj) { using (var txn = TransactionScope.Enter()) { ISoftDeletable deletableObj = obj as ISoftDeletable; if (obj == null || (deletableObj != null && deletableObj.DeletedDate.HasValue)) { throw new ObjectNotFoundException(); } BeforeDelete(obj); _baseDA.Delete(obj); txn.Commit(); } }
public static void UnDelete(this ISoftDeletable entity) { entity.IsDeleted = false; var deletionAuditedEntity = entity as IDeletionTracking; if (deletionAuditedEntity == null) { return; } deletionAuditedEntity.DeletionDateTime = null; deletionAuditedEntity.DeleterUserId = null; deletionAuditedEntity.DeleterBrowserName = null; deletionAuditedEntity.DeleterIp = null; }
/// <summary> /// Insert a domain /// </summary> /// <param name="obj">Domain</param> /// <returns></returns> public T Insert(T obj) { using (ConnectionScope.Enter()) { ISoftDeletable softDeletable = obj as ISoftDeletable; if (softDeletable != null) { softDeletable.CreatedDate = DateTime.Now; softDeletable.ModifiedDate = DateTime.Now; } TIdentifier id = (TIdentifier)NHibernateSession.Save(obj); obj = GetByID(id); return(obj); } }
/// <summary> /// 检测并执行<see cref="ISoftDeletable"/>接口的逻辑 /// </summary> public static TEntity CheckIDateleTime <TEntity, TKey>(this TEntity entity) where TEntity : IEntity where TKey : IEquatable <TKey> { if (!(entity is ISoftDeletable)) { return(entity); } ISoftDeletable entity1 = (ISoftDeletable)entity; if (entity1.DeleteTime == default(DateTime)) { entity1.DeleteTime = DateTime.Now; } return((TEntity)entity1); }
/// <summary> /// Update Domain /// </summary> /// <param name="obj"></param> /// <returns></returns> public T Update(T obj) { using (ConnectionScope.Enter()) { if (obj.IsNew) { throw new DataException("Cannot insert empty key into ID field"); } ISoftDeletable softDeletable = obj as ISoftDeletable; if (softDeletable != null) { softDeletable.ModifiedDate = DateTime.Now; } NHibernateSession.Update(obj); return(obj); } }
private void DeleteInternal(params TEntity[] entities) { CheckDataAuth(DataAuthOperation.Delete, entities); if (typeof(ISoftDeletable).IsAssignableFrom(typeof(TEntity))) { // 逻辑删除 foreach (TEntity entity in entities) { ISoftDeletable softDeletableEntity = (ISoftDeletable)entity; softDeletableEntity.DeletedTime = DateTime.Now; } } else { // 物理删除 _dbSet.RemoveRange(entities); } }
private void SoftCascadeDelete(ISoftDeletable model, DateTime deleteTime) { var properties = model.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); var wantedProperties = properties .Where(x => x.PropertyType.IsGenericType && x.PropertyType.GetGenericTypeDefinition() == typeof(ICollection <>) && typeof(ISoftDeletable).IsAssignableFrom(x.PropertyType.GetGenericArguments()[0])) .ToArray(); foreach (var prop in wantedProperties) { ///can not cast even though models are implementing the interface - the problem ///might be with differenet intances of the library some assemmbly stuff var listOfChildItems = (HashSet <ISoftDeletable>)prop.GetValue(model); if (typeof(VideoNote).IsAssignableFrom(prop.PropertyType.GetGenericArguments()[0])) { foreach (var item in listOfChildItems) { item.DeletedOn = deleteTime; item.IsDeleted = true; } continue; } foreach (var childItem in listOfChildItems) { SoftCascadeDelete(childItem, deleteTime); } } model.DeletedOn = deleteTime; model.IsDeleted = true; }
private void SetDeleted(ISoftDeletable entity) { entity.SetSoftDeleted(_now.Moment, _currentUserAccessor.User.Login); }
/// <summary> /// Returns true if the given object has been soft-deleted; otherwise, returns false /// </summary> /// <param name="obj"></param> /// <returns></returns> public static bool HasBeenSoftDeleted(this ISoftDeletable obj) { return(obj.DeletedUtc != null); }
/// <summary> /// Marks the given object as soft-deleted /// </summary> /// <param name="obj"></param> public static void SoftDelete(this ISoftDeletable obj) { obj.DeletedUtc = DateTime.UtcNow; }
public override int SaveChanges() { //自动审核 var nowAuditDate = DateTime.Now; var changeSet = ChangeTracker.Entries <IAuditable>(); if (changeSet != null) { foreach (DbEntityEntry <IAuditable> dbEntityEntry in changeSet) { switch (dbEntityEntry.State) { case EntityState.Added: dbEntityEntry.Entity.CreatedOn = nowAuditDate; dbEntityEntry.Entity.ModifiedOn = nowAuditDate; break; case EntityState.Modified: dbEntityEntry.Entity.ModifiedOn = nowAuditDate; break; } } } //乐观并发控制 foreach (var dbEntityEntry in ChangeTracker.Entries().Where(x => x.State == EntityState.Added || x.State == EntityState.Modified)) { IVersionable entity = dbEntityEntry.Entity as IVersionable; if (entity != null) { entity.RowVersion = entity.RowVersion + 1; } } //Id自动生成 foreach (var dbEntityEntry in ChangeTracker.Entries().Where(x => x.State == EntityState.Added)) { IIdentifiable entity = dbEntityEntry.Entity as IIdentifiable; if (entity != null) { if (entity.Id == Guid.Empty.ToString("N")) { IIdentityGenerator identityGenerator = LocalServiceLocator.GetService <IIdentityGenerator>(); if (identityGenerator != null) { entity.Id = (string)identityGenerator.NewId(); } } } } //逻辑删除 foreach (var dbEntityEntry in ChangeTracker.Entries().Where(x => x.State == EntityState.Deleted)) { ISoftDeletable entity = dbEntityEntry.Entity as ISoftDeletable; if (entity != null) { if (!entity.IsDeleted) { entity.IsDeleted = true; } base.Entry(entity).State = EntityState.Modified; } } return(base.SaveChanges()); }
/// <summary> /// Marks the given object as no longer soft-deleted /// </summary> /// <param name="obj"></param> public static void SoftRestore(this ISoftDeletable obj) { obj.DeletedUtc = null; }
public static bool IsNullOrDeleted(this ISoftDeletable entity) { return(entity == null || entity.IsDeleted); }
/// <summary> /// Set soft-delete properties on deletion. /// </summary> /// <param name="entity">The entity</param> /// <param name="now">When the entity was deleted</param> /// <param name="currentUserName">The user who deleted the entity</param> public static void SetSoftDeleted(this ISoftDeletable entity, DateTime now, string currentUserName) { entity.IsDeleted = true; entity.DeletedOn = now; entity.DeletedBy = currentUserName; }
public static bool IsIncludeSoftDeleted(bool?includeSoftDeleted, TEntity entity, out ISoftDeletable softDeletableEntity) { var result = (!(includeSoftDeleted ?? false) && (entity is ISoftDeletable)); if (result) { softDeletableEntity = entity as ISoftDeletable; result = result && softDeletableEntity.IsDeleted; } else { softDeletableEntity = null; } return(result); }
public static bool IsDeleted(this ISoftDeletable model) { return(model.DeletedAt != null); }
public static void MarkAsDeleted(this ISoftDeletable softDeletable) { softDeletable.IsDeleted = true; softDeletable.DeletedDate = DateTimeOffset.Now; }