0

The question is about convenient code organization.

I've moved my POCO classes to the persistence independent project. Should I move IQueryable code (part of "business rules" around those POCOs, used for example to generate derived non-persistent business object) together or better to leave all IQueryable code in the library with ObjectContext?

Roman Pokrovskij
  • 9,449
  • 21
  • 87
  • 142
  • 3
    That is completely up to you. – Ladislav Mrnka May 19 '11 at 21:49
  • That means that persistence independent library can contain IQueryable expressions which are "not executable/testable" without persistence layer ... This speculation somehow string up my nerves. – Roman Pokrovskij May 19 '11 at 23:23
  • 1
    http://stackoverflow.com/questions/6051145/using-the-repository-pattern-to-support-multiple-providers/6051328#6051328 Exposing `IQueryable` will not break ability to test your code but you will have to use integration tests instead of unit test. Check also links in that answer. I wrote some ideas about this perhaps you will find some useful information there. – Ladislav Mrnka May 20 '11 at 13:35
  • Thank you, Ladislav. If I get all information right your advice is "don't forget about leaky abstraction , but try don't latch it" – Roman Pokrovskij May 21 '11 at 22:12

1 Answers1

2

In your comment, you say, "That means that persistence independent library can contain IQueryable expressions which are "not executable/testable" without persistence layer...". That's not true. You can test IQueryable expressions in a (mostly) persistence-independent manner.

E.g.:

public IQueryable<Foo> SomeFoos(IQueryable<Foo> foos, string aValue)
{
    return from foo in foos 
           where foo.Bar = aValue
           select foo;
}

You can use this with L2E:

var f = SomeFoos(myContext, "Baz");

You can test it with L2O:

var expected = new Foo { Bar = "Baz" };
var notExpected = new Foo { Bar = "Oops" };
var input = new [] { expected, notExpected };

var actual = instance.SomeFoos(input.AsQueryable(), "Baz");

Assert.AreEqual(actual.First(), expected, "Should have found expected record.");
Assert.AreNotEqual(actual.Single(), notExpected, "Should not have found unexpected record.");

This is a made-up example, but hopefully the point is clear: How you structure this really is up to you, as @Ladislav states.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • Yes, you are right: it is possible to execute IQueryable expression just on the "ad hoc" lists. To finalize: it is possible to keep declarations of SomeFoos and Foo class together (in one project), no contradictions in that. – Roman Pokrovskij May 21 '11 at 12:02
  • You need to pass `input.AsQueryable()` into `SomeFoos` in the LTO example, don't you? – Slauma May 22 '11 at 20:36