使用 EF4 存储库模式加入集合时遇到困难
我无法正确理解其背后的设计。我正在使用存储库模式来管理我的数据层。在我的一个控制器 (MVC3) 中,我正在构建一个需要执行联接的 LINQ 查询。我对此有两个问题:
- 向我的存储库添加一个方法来执行所有连接、投影等是否更好?我对此有点犹豫,因为这会导致我的存储库上的合同定义不断增长?
- 假设我在 Post 存储库中有一个 List() 方法,它返回所有项目。我目前无法在 linq(连接)查询中使用此方法,因为它无法将其转换为存储表达式。请注意,下面的代码使用一个名为 repo 的类,它使用相同的上下文实例保存对我的所有存储库(帖子、朋友)的引用。
大编辑: 现在事情对我来说更加清楚了,但我希望有人能跳到这里来帮助我把一切组织起来;-)。我想要做的是实现规范模式以及我的存储库模式。问题是,我正在使用 POCO,而我的存储库正在使用 IContext 接口,该接口使用 IObjectSets。因此,在下面的列表方法中,Posts 是一个 IObjectSet,Context 是一个 IContext 接口,我将实际的上下文注入其中。
我一直在阅读,有一些非常好的现成可用代码,例如 实现-repository-pattern-with -ef4-poco-support 和 实现-repository-pattern-with-entity-framework 。
这两个示例都使用存储库中的 objectContext。把它也抽象出来不是更好吗?
我的存储库:
public System.Linq.IQueryable<Post> List()
{
return this.context.Posts;
}
在我的控制器方法中:
var friendquestions = (from q in base._repo.Post.List().OfType<Question>()
from f in _repo.Friends.List()
where f.userId == myid
where q.Author == f.friendId
select q.Id).ToArray();
但是以下内容确实有效(为什么?):
var temp = (from q in base._repo.Post.List().OfType<Question>()
where q.Id > 6
select q.Id).ToArray();
这基本上与此处描述的问题相同: linq-to-entities-does-not-recognize-the-method
我该如何围绕这个进行设计?我一直在阅读模型定义的函数,但我不确定这是否是正确的方法?
提前致谢
I'm having trouble getting the design behind this correct. I'm using a repository pattern to manage my datalayer. In one of my controllers (MVC3) i am constructing a LINQ query that needs to perform a join. I have 2 questions about this:
- Is is better to add a method to my repository to perform all joins, projections etc? I'm a bit hesitant about that because it would result in an ever growing contract definition on my repository?
- Let's say I have a List() method in a Post repo that returns all items. I currently can't use this method in a linq (join) query because it can't convert it to a store expression. Please note that the code below use a class called repo, which holds a reference to all my repositories (post, friends) using the same context instance.
BIG EDIT:
Things are a bit more clear to me now, but i'm hoping someone will jump in here and help me get everything organised ;-). What I'm looking to do is implement a specification pattern along with my repository pattern. The problem is, i'm using POCO and my repositories are using an IContext interface which uses IObjectSets. So in the list method below, Posts is an IObjectSet and Context is an IContext interface, into which I inject my actual context.
I've been reading up and there is some very nice ready to use code, like
implementing-repository-pattern-with-ef4-poco-support and implementing-repository-pattern-with-entity-framework.
Both these examples use the objectContext in the repository. Isn't it better to abstract that out as well?
My repository:
public System.Linq.IQueryable<Post> List()
{
return this.context.Posts;
}
And in my controller method:
var friendquestions = (from q in base._repo.Post.List().OfType<Question>()
from f in _repo.Friends.List()
where f.userId == myid
where q.Author == f.friendId
select q.Id).ToArray();
The following does work however (why?):
var temp = (from q in base._repo.Post.List().OfType<Question>()
where q.Id > 6
select q.Id).ToArray();
It's basically the same problem as described here: linq-to-entities-does-not-recognize-the-method
How do I design around this? I've been reading up on Model defined functions, but i'm not sure if that's the way to go?
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
把它也抽象出来不是更好吗?
答案:您可以查看 - NCommon。已经使用IEFSession来包装
对象上下文。
Isn't it better to abstract that out as well?
Answer : You can look into - NCommon of Ritesh rao. It has been used IEFSession to wrap
the ObjectContext.