Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repository Pattern - POCOs or IQueryable?

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!

like image 472
Duncan Avatar asked Apr 12 '09 09:04

Duncan


2 Answers

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.

like image 119
eglasius Avatar answered Oct 10 '22 10:10

eglasius


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();
}
like image 41
Arjen de Blok Avatar answered Oct 10 '22 11:10

Arjen de Blok



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!