5

I am looking for a simple IRepository interface for my mvc web application, I have done a lot of searching around, and there are as many opinions as there are people. So i decided to ask the experts

If you can recommend a IRepository and IRepository interfaces that are commonly used and answer the basic CRUD and query operations ( to support filtering ).

If you know of frameworks that also include implementations and can work with EF 4 I would love if you can mention them.

Thank you.

Edit: As @Ladislav suggests what is the alternative, always just call linq to ADO.net calls from my code? Is it a good idea to use a POCO repository that abstract creation of custom POCOS from my business model, I have a Jewel POCO class that need to be parsed from varies DB entries, is this a common practice with legacy systems where I can't touch the DB architecture but only the presentation

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
David MZ
  • 3,648
  • 6
  • 33
  • 50
  • 2
    This isn't really a proper question for SO. It's too subjective. – Alastair Pitts Jul 23 '11 at 15:26
  • I am looking for the best interface around, so it's like an advice more than a question – David MZ Jul 23 '11 at 15:29
  • 1
    @David, the fact that there is lots of opinions should tell you that there is no “best interface around”. Different circumstances ask for different solutions and different people have different preferences. – svick Jul 23 '11 at 15:41
  • I agree, I am looking for a simple but robust enough interface for a online store, I am looking for ideas, I can't decide by my self because I lack experience with the pattern. – David MZ Jul 23 '11 at 15:58

2 Answers2

9

Stop. Patterns should be used when they are needed - when they solve some common problem - not because they exist. You obviously don't need it at the moment because you didn't write any single requirement what the repository should solve for you - such requirements are crucial if you want to choose a "good one" to start with. Anyway a good one usually evolve during your development, it is most often not defined upfront.

Also generic repository with EF is one of the most stupid patterns I have ever used in my own project. It is good only to be parent of specific repository. It works only in basic scenarios where you most often don't need repository at all.

Something to read: What is the point of generic repository

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • I want to use a repository to abstract the data layer from my more advanced code, I want to inject it to some classes that use the database object to generate POCOS with complex properties that are composed from varies database fields – David MZ Jul 23 '11 at 17:20
  • And that makes all common referenced repository implementations completely useless as well as generic repository because this is exactly requirement for your own custom solution where each repository will be completely specific to support your custom object composition. – Ladislav Mrnka Jul 23 '11 at 17:23
  • I see your point clearly, but still I need to create "custom" repositories for each data object in my application, with GetByXXX methods, so no common interface will be used, but the idea of a factory for my POCOS that is also queryable is good idea? – David MZ Jul 23 '11 at 17:41
  • It is for much bigger discussion and perhaps review of your requirements and SO is not suitable for that (perhaps the chat is). Composition of entity from multiple tables should be done by ORM itself. Perhaps you should make some exploration of other ORMs like NHibernate and you will end with direct mapping between your business objects and database (EF can do it as well but you will need stored procedures to save your entities). Using additional level of objects will make your querying terribly complex and hard to use. – Ladislav Mrnka Jul 23 '11 at 17:49
  • I didn't explain myself well, I do not take fields from multiple tables but compose a new object with conditionals and alike from a single table, so my repository returns an object that is more complex then my model, I want to restrict my querying to the repository methods only ( e.g. FilterBYXXX ) , so further output will not change the List of objects, this is not a base repository pattern but something in the middle, does this makes sense? – David MZ Jul 23 '11 at 18:06
  • If you only expect to have methods like FilterByXXX and you don't expect upper layer to build the query (use `IQueryable`) it can work. If you expect upper layer to build the query you will need to implement specifications (Specification pattern) and transform them to linq expressions in the repository and it will again make your solution quite complex. – Ladislav Mrnka Jul 23 '11 at 18:14
  • EF currently does not impose a requirement that you have stored procedures for writing back to your store. Many people currently use IQueryable and DbContext::SaveChanges to perform updates, where SaveChanges has been abstracted away in a number of ways. Your common CRUD operations are typically reduced down to extension methods that understand how to work with whatever framework/api you are using. All of this assumes you are using a framework/api that does change tracking (such as db4o, Perst, EF, NHiber, and many others.) – Shaun Wilson Jan 19 '13 at 02:17
2

The repository pattern is pretty straight-forward...you really don't need to use an existing framework. You can build the interface(s) yourself. There are a number of good examples out there in blogs of people building their own and also allows them to have near 100% code coverage in their tests. Here's a few examples (but they all follow similar patterns):

Using Repository Pattern with Entity Framework (Gil Fink)

100% UNIT TESTABLE LINQ TO SQL REPOSITORY (Kazi Manzur Rashid) -- I'm actually following some of his examples in my work

Working together: LINQ to SQL, IRepository, Unit Of Work and Dependency Injection (Stuart Harris)

And there are a ton more.

I think building it yourself, especially if you're just learning the pattern, will definitely teach you a ton about the pattern and also give you insight into what works for your situation and what doesn't.

I hope this helps! Good luck.

David Hoerster
  • 28,421
  • 8
  • 67
  • 102
  • 1
    I just skimmed through *100% unit testable linq to sql repository* to clarify that it is not unit testable at all because it exposes `IQueryable`. [Here](http://stackoverflow.com/questions/6766478/unit-testing-dbcontext/6768712#6768712) is some explanation why it is not unit testable. – Ladislav Mrnka Jul 23 '11 at 16:44
  • You're not unit testing the db context - you're unit testing your code which relies on the context. If the dbcontext object was designed right initially, it would have been open to be fully mocked, so a generic repository wouldn't be necessary. As it is, it isn't, so this is the next best thing, IMHO. – David Hoerster Jul 23 '11 at 19:58