views:

809

answers:

2

Hi

I'm new to the Repository Pattern and after doing a lot of reading on the web I have a rough understanding of what is going on, but there seems to be a conflict of ideas.

One is what the IRepository should return.

I would like to deal in ONLY Pocos so I would have an IRepository implementation for every aggregate root, like so:

public class OrangeRepository: IOrangeRepository
{
  public Orange GetOrange(IOrangeCriteria criteria);
}

where IOrangeCriteria takes a number of arguments specific to finding an Orange.

The other thing I have is a number of data back-ends - this is why I got into this pattern in the first place. I imagine I will have an implementation for each, e.g

OrangeRepositoryOracle, OrangeRepositorySQL, OrangeRepositoryMock etc

I would like to keep it open so that I could use EF or NHibernate - again if my IOrangeRepository deals in POCOs then I would encapsulate this within the Repository itself, by implementing a OrangeRepositoryNHibernate etc.

Am I on the right lines?

Thanks

EDIT: Thanks for the feedback, I don't have anyone else to bounce these ideas off at the moment so it is appreciated!

+3  A: 

Yes, your version is the safest / most compatible one. You can still use it with about any resources, not only data access ones, but with web services, files, whatever.

Note that with the IQueryable version you still get to work based on your POCOs classes, but you are tied to the IQueryable. Also consider that you could be having code that uses the IQueryable and then turns out it you hit a case where one of the repository's ORM doesn't handle it well.

eglasius
+3  A: 

I use the same pattern as you do. I like it a lot. You can get your data from any resources.

But the advantage of using IQuerable is that you do not have to code your own criteria API like the OrangeCriteria.

When NHibernate gets full Linq support then I may switch to the IQueryable.

Then you get

public class OrangeRepository: IOrangeRepository {  
    public IQueryable<Orange> GetOranges();
}
Arjen de Blok