IQueryable

It can be useful to allow operations on an IQueryable without exposing the IQueryable itself.

For example if you want to allow the vast amount of LINQ commands through a repository, but without exposing the IQueryable itself (as the caller may attempt to use the IQueryable after the repository has been disposed).

This can be done with a generic repository method that takes a function that operates on an IQueryable, for example:

public static void Main()
{
    int OrderNumber = 123;
    var repo = new Repository<SalesOrder>();
    SalesOrder so = repo.Operation(iq => iq.Where(s => s.OrderNumber == OrderNumber).First());
}

public class SalesOrder
{
    public int OrderNumber{ get; set; }
}

public class Repository<T> 
    where T: class
{
    public TResult Operation<TResult>(Func<IQueryable<T>, TResult> function)
    {
        return function(Set);
    }
    private IQueryable<T> Set { get { return _set; } }
    private IQueryable<T> _set = /*IQueryable here*/;
}

 

Comments, or Questions?  Get in touch.