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

Answers


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.


Need Your Help

Sticky header on elements in VBox

actionscript-3 flex user-interface layout

I have a VBox with some container elements in it. The VBox has a limited height and enables scrolling if its elements exceed its height.

CPP- Square Hollow- What is the mistake?

c++ xcode square

Can Someone please help me? I worte this code it compiles fine at first round but when it loops it doesnt print out properly. I cant find the mistake.. Thank you!