Roman Pokrovskij
Roman Pokrovskij

Reputation: 9776

Entity Framework: POCO and encapsulation of IQueryable code

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?

Upvotes: 0

Views: 625

Answers (1)

Craig Stuntz
Craig Stuntz

Reputation: 126547

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.

Upvotes: 2

Related Questions