一、引言

所谓级联删除是指删除一条记录后,附带关联记录也一起删除,比如删除客户后,联系人也一起删除;

以往我们会依赖于数据库表的外键约束,但存在着明显的问题,增加数据库压力、提示不友好、职责越界、事务冲突等,这类业务相关的功能不应该放到数据库去管理。

二、依赖注入

DI、IOC等概念在这不展开介绍,大家记住它们的主要目的就是解耦,本文利用.netcore内置的DI实现相关功能

二、解决方案

利用DI注入级联删除的服务,在主体删除时获取已注册的级联删除服务,实现记录的级联删除,前面这句话可能绕口,下面用一个例子说明,删除【实体元数据】时同时删除【字段元数据】,看下代码就清晰了:

级联删除接口类 ICascadeDelete

复制代码
1 public interface ICascadeDelete<TParent>2 { 3     void CascadeDelete(params TParent[] parent); 4 }
复制代码

字段删除类 AttributeDeleter,实现上面的接口

复制代码
 1 public class AttributeDeleter : ICascadeDelete<Domain.Entity> 2 {  3     private readonly IAttributeRepository _attributeRepository;  4         public AttributeDeleter(IAttributeRepository attributeRepository)  5     {  6         _attributeRepository = attributeRepository;  7     }  8  9     /// <summary>10     /// 实体级联删除 11     /// </summary>12     /// <param name="parent">被删除的实体</param>13     public void CascadeDelete(params Domain.Entity[] parent) 14     { 15         //删除字段16         _attributeRepository.DeleteMany(x =>x.EntityId.In(parent.Select(x => x.EntityId).ToArray())); 17     } 18 }
复制代码

实体删除类EntityDeleter

复制代码
 1 public class EntityDeleter  2  {  3       private readonly IEntityRepository _entityRepository;  4       private readonly IEnumerable<ICascadeDelete<Domain.Entity>> _cascadeDeletes;//需要同时删除记录的服务类 5       public EntityDeleter(IEntityRepository entityRepository