zoukankan      html  css  js  c++  java
  • LindDotNetCore~ISoftDelete软删除接口

    回到目录

    概念

    ISoftDelete即软删除,数据在进行delete后不会从数据库清除,而只是标记一个状态,在业务范围里都不能获取到这个数据,这在ORM框架里还是比较容易实现的,对传统的ado来说需要对sql统一进行拦截和条件过滤.

    实施步骤

    1. 实体继承ISoftDelete
    2. 数据上下文实现对IsDeleted的过滤
    3. 对删除方法进行调整,添加对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等!

    欢迎大家的阅读与思考!

     回到目录

  • 相关阅读:
    共享无法访问问题,通过ip地址或者主机名无法访问目的主机
    开机系统更新,一直停在?%处,无法进入系统
    win7电脑访问内网地址报错0x800704cf,0x80070035解决方法
    电脑共享--问题汇总
    win10域账户用户时间无法和域服务器同步
    卸载WPS后,原office出现各种问题,报错,图标混乱
    局域网新装电脑主机网络断断连连解决方案
    win10主机无法进入本地共享,“没有权限”
    win10安装部分软件报错“应用程序无法启动,应用程序并行配置不正确,或使用命令行sxstrace.exe”
    【日常修机】打印机故障维护
  • 原文地址:https://www.cnblogs.com/lori/p/8483613.html
Copyright © 2011-2022 走看看