实体框架:POCO和IQueryable代码的封装

时间:2011-05-19 21:45:53

标签: entity-framework entity-framework-4

问题在于方便的代码组织。

我已将POCO课程移至持久性独立项目。我是否应该将IQueryable代码("业务规则"围绕这些POCO的一部分,用于生成派生的非持久性业务对象)放在一起或更好地将所有IQueryable代码保留在带有ObjectContext的库中?

1 个答案:

答案 0 :(得分:2)

在你的评论中,你说,“这意味着持久性独立库可以包含IQueryable表达式,这些表达式”不可执行/可测试“而没有持久层......”。这不是真的。您可以(主要)与持久性无关的方式测试IQueryable表达式。

E.g:

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

您可以在L2E中使用它:

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

您可以使用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.");

这是一个简单的例子,但希望这一点很清楚:正如@Ladislav所说,你如何构建这个问题真的取决于你。

相关问题