0

I am reletively new to Entity Framework and LINQ. I have the following code with returns a list of suppliers

    public class SupplierRepository
{
    private DataContext db = new DataContext();

    public IQueryable<Supplier> FindAllSupplier()
    {
        return db.Suppliers;
    }
}

This works fine, but I know you can use Generics with Linq and EF. so that the above code would look something like this...

    public class GenericRepository
{
    private DataContext db = new DataContext();

    public IQueryable<T> FindAll<T>()
    {
        return ...
    }
}

So that I can use it to return any type in my EF model, but I am not sure how to implement this. Can anyone advise please?

3
  • This blog post sums it up pretty neatly Commented Jun 9, 2011 at 11:57
  • That looks great, the only problem is what is the GetQuery Method they mention? I can't seem to get this to work when replicating the code. Commented Jun 9, 2011 at 12:15
  • Can you help with explaining where the GetQuery comes from? Commented Jun 9, 2011 at 19:45

1 Answer 1

4

Instead of returning an IQueryable<T> let your repository implement IQueryable<T> and instead of wrapping a DataContext inside the repository, wrap both the DataContext and the repositories inside a unit of work. Your repository can than look like this:

public abstract class Repository<T> : IQueryable<T>
    where T : class
{
    private readonly IQueryable<T> query;

    protected Repository(IQueryable<T> query)
    {
        this.query = query;
    }

    public Type ElementType
    {
        get { return this.query.ElementType; }
    }

    public Expression Expression
    {
        get { return this.query.Expression; }
    }

    public IQueryProvider Provider
    {
        get { return this.query.Provider; }
    }

    public abstract void InsertOnSubmit(T entity);

    public abstract void DeleteOnSubmit(T entity);

    ...
}

I use this approach to effectively hide my O/RM behind an abstraction, which allows me to unit test my applicationm while still allowing to use LINQ over my repositories and have the unit of work pattern.

You can read all about it here: Faking your LINQ provider.

Sign up to request clarification or add additional context in comments.

4 Comments

This is one of my first attempts at using Linq/EF so I am sorry to say some of that has gone over my head. Can you explain a little more or provide a simplier example please?
@suggy: I'm sorry about that. I agree that this is a bit advanced stuff, but certainly not over engineered. Keep in mind however, that the article describes a design that works with multiple domeain models; this might complicate things a bit. A earlier (and simpler) version can be found in this SO article: stackoverflow.com/questions/4128640/…. Perhaps it is of some help.
thanks for pointing me in the right direction. I guess I have some reading to do!!
@suggy: If you have any questions after you're done reading, don't hesitate to contact me. I'll be happy to help you out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.