petro.sidlovskyy
petro.sidlovskyy

Reputation: 5103

EF 4.1 Code First filtering lazy loaded collections

I have EF Code First solution which supports soft delete so we mark entities as IsDeleted and not remove them from database. I would like to fiter collection of related objects (lazy loading) so entities marked as deleted are not returned to API user.

Here is simple approache I use:

public class FilteredCollection<T> : ICollection<T> where T : DeletableEntity
{
    private List<T> _listWithDeleted = new List<T>();

    protected IEnumerable<T> FilteredItems
    {
        get { return _listWithDeleted.Where(e => e.IsDeleted == false); }
    }

    protected bool _IsReadOnly;

    public virtual T this[int index]
    {
        get
        {                
            return FilteredItems.ToList()[index];
        }
        set
        {
            FilteredItems.ToList()[index] = value;
        }
    }

    public virtual int Count
    {
        get
        {
            return FilteredItems.Count();
        }
    }

    public virtual bool IsReadOnly
    {
        get
        {
            return _IsReadOnly;
        }
    }

    public virtual void Add(T entityObject)
    {
        _listWithDeleted.Add(entityObject);
    }

    public virtual bool Remove(T entityObject)
    {
        if (FilteredItems.Contains(entityObject))
        {
            entityObject.IsDeleted = true;
            return true;
        }
        else
        {
            return false;    
        }
    }

    public bool Contains(T entityObject)
    {
        return FilteredItems.Contains(entityObject);
    }

    public virtual void CopyTo(T[] entityObjectArray, int index)
    {
        var list = FilteredItems.ToList();
        list.CopyTo(entityObjectArray, index);
    }

    public virtual void Clear()
    {
        foreach (var item in _listWithDeleted)
        {
            item.IsDeleted = true;
        }
    }

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

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

I have implemented ICollection so it filter object internally. I use this class instead of ICollection so filtered entities are returned to me. I have done some tests and it seems it works well but I don't feel comfortable about this solution.

Could you please provide me with downsides of this approach or please suggest in case you know some better one.

Thanks beforehand,

-Petro

Upvotes: 0

Views: 536

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364409

It is wrong solution because it makes filtering in your application. If your database will grow and there will be more and more soft deleted items you will always need to load all of them prior to applying filter. This can quickly become very big performance issue.

You should do filtering in the database but that goes against lazy and eager loading. The solution can be conditional mapping (only not deleted items will be mapped) but it will not allow you mapping IsDeleted property - you will have to soft delete your entity with stored procedure and that in turn cannot be mapped with code first.

With combination of EF code first + soft delete you should avoid lazy and eager loading completely and either use separate query to get filtered data or use explicit loading.

Upvotes: 1

Related Questions