删除实体时如何从集合中删除实体

时间:2019-07-18 03:35:55

标签: c# collections

我有Entity类,它具有IsEntityDeleted属性和DeleteEntity方法:

public class Entity
{
    public virtual bool IsEntityDeleted { get; internal set; }

    public void DeleteEntity()
    {
        // Other logics...
        IsEntityDeleted = true;
    }
}

如您所见,当我调用IsEntityDeleted方法时,DeleteEntity将为真。在很多地方,我有很多收藏。我不想保留已删除的实体。集合类是自定义的:

public class MyVirtualCollection<T> : ICollection<T> where T : Entity
{
    ICollection<T> Items { get; set; }

    public MyVirtualCollection(ICollection<T> source)
    {
        Items = source;
    }
    public int Count => Items.Count;

    public bool IsReadOnly => Items.IsReadOnly;

    public void Add(T item)
    {
        Items.Add(item);
    }

    public void Clear()
    {
        Items.Clear();
    }

    public bool Contains(T item)
    {
        return Items.Contains(item);
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        Items.CopyTo(array, arrayIndex);
    }

    public IEnumerator<T> GetEnumerator()
    {
        return Items.GetEnumerator();
    }

    public bool Remove(T item)
    {
        return Items.Remove(item);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

这是用例:

var firstEntity = new Entity();
var secondEntity = new Entity();

var list = new List<Entity>();
list.Add(firstEntity);
list.Add(secondEntity);

MyVirtualCollection<Entity> entities = new MyVirtualCollection<Entity>(list); // entities.Count is 2 here

firstEntity.DeleteEntity(); // entities.Count should be 1

secondEntity.DeleteEntity(); // entities.Count should be 0

MyVirtualCollection为真时,是否可以从IsEntityDeleted集合中删除实体?

1 个答案:

答案 0 :(得分:1)

我认为您将从集合中删除项目的责任委托给实体。我认为这是错误的,这违反了SOLID原则:Sigle责任。现在,MyVirtualCollection类负责删除项目,但是不知道DeleteEntity()类中的Entity方法。如果您想以任何方式从集合中删除实体,则应在实体类中公开一个事件,该事件建议该集合实际上必须删除实体:

delegate void RemoveEventDelegate(Entity entity);
public class Entity
{
    public event RemoveEventDelegate OnEntityDeleted;

    public virtual bool IsEntityDeleted { get; internal set; }

    public void DeleteEntity()
    {
        // Other logics...
        IsEntityDeleted = true;
        if (OnEntityDeleted != null)
        {
            OnEntityDeleted(this);
        }
    }
}

并订阅集合中的OnEntityDeleted事件

public class MyVirtualCollection<T> : ICollection<T> where T : Entity
{
    ICollection<T> Items { get; set; }

    private void Suscribe(Entity entity)
    { 
        Items.Remove(entity);
    }

    public MyVirtualCollection(ICollection<T> source)
    {
        Items = source;
        foreach(var ent in Items)
        {
            ent.OnEntityDeleted += Suscribe;
        }
    }
    public int Count => Items.Count;

    public bool IsReadOnly => Items.IsReadOnly;

    public void Add(T item)
    {
        Items.Add(item);
        item.OnEntityDeleted += Suscribe;
    }

    public void Clear()
    {
        foreach(var ent in Items)
        {
            ent.OnEntityDeleted -= Suscribe;
        }
        Items.Clear();
    }

    public bool Contains(T item)
    {
        return Items.Contains(item);
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        Items.CopyTo(array, arrayIndex);
    }

    public IEnumerator<T> GetEnumerator()
    {
        return Items.GetEnumerator();
    }

    public bool Remove(T item)
    {
        item.OnEntityDeleted -= Suscribe;
        return Items.Remove(item);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

请注意,当您调用DeleteEntity()方法时,它将从涉及的所有集合中删除该实体。