0

I need my implementation of IObjectSet<T> to be able to validate expressions as the "real" Linq2Entity provider would. Is it possible?

Background:
I've changed my EF model (model-first) to return IObjectSet<T> instead of ObjectSet<T>
So that I can fake my model, and have it return the data I want.

Problem:
I have written an implementation of IObejctSet<T> that is bacally a wrapper for a List<T>.
This is all good - but when I query my implementation of IObjectSet<T> im using LinqToObject - so there is no guarantee that I have valid Linq2Entity expression.

Proposed solution:
use the real In my implementaion i just use List<T>'s provider:

private class IObjectSetUnitTestImplementation<T> : IObjectSet<T> where T : EntityObject
{
  private IQueryable _qry;

  public IObjectSetUnitTestImplementation(List<T> lst)
  {
    _inner = lst;
    _qry = _inner.AsQueryable();
    Expression = _qry.Expression;
    ElementType = _qry.ElementType;
    Provider = _qry.Provider; //<--should be ObjectSet<T>'s provider
  }
  //Rest of implementaion omitted
}

So I tried this:

ObjectContext context = null;
var _qry = new ObjectQuery<T>(typeof(T).Name, context) as IQueryable;
Expression = _qry.Expression;
ElementType = _qry.ElementType;
Provider = _qry.Provider;

But I have no context to feed it with, and even if I had, it would just try to access the database.

Is there any other way?

Jens Kloster
  • 11,099
  • 5
  • 40
  • 54

1 Answers1

1

so there is no guarantee that I have valid Linq2Entity expression"

Congratulations, you've just discovered a reason, why IObjectSet<T>, IRepository<T> and etc. shouldn't be implemented for IQyeryable<T>. And the reason is simple: query, which is valid for one LINQ provider, can be invalid for another one (even if you just change databases from MS SQL to something other).

And there's no generic way to verify those queries.
So, unit-testing isn't a good choice for IQyeryable<T>, but integration testing is a definitely the thing that you need.

Dennis
  • 37,026
  • 10
  • 82
  • 150
  • I have [heard](http://stackoverflow.com/questions/6904139/fake-dbcontext-of-entity-framework-4-1-to-test) this argument before, but I was hoping someone found a solution. I just dont understand *why* its not possible – Jens Kloster Oct 11 '13 at 09:50
  • @JensKloster: this isn't possible because of LINQ providers nature. The first provider supports feature `A`, the second - doesn't. To validate an expression with this feature you need to build query from it via LINQ provider, and query construction means "query execution". – Dennis Oct 11 '13 at 09:55
  • Im upvoting your answer because the deeper I dive into this problem, the more your answer makes sence :) Still hoping that someone will come up with a solution though.. – Jens Kloster Oct 14 '13 at 09:39
  • I fount [this](http://stackoverflow.com/questions/13332002/how-to-mock-the-limitations-of-entityframeworks-implementation-of-iqueryable/13352779#13352779) - so you where right :) thank you – Jens Kloster Oct 15 '13 at 14:18