1

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:

  1. 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?
  2. 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

Community
  • 1
  • 1
Syg
  • 403
  • 4
  • 17
  • Mm, looks like my first question is commonly solved using the specification pattern – Syg Aug 26 '11 at 09:03

1 Answers1

0

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.

j0k
  • 22,600
  • 28
  • 79
  • 90