Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended IRepository and IRepository<T> interface in C#

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

like image 843
David MZ Avatar asked Jul 23 '11 15:07

David MZ


People also ask

Should I use repository pattern?

The Repository pattern makes it easier to test your application logic. The Repository pattern allows you to easily test your application with unit tests. Remember that unit tests only test your code, not infrastructure, so the repository abstractions make it easier to achieve that goal.

What is IRepository C#?

The IRepository interface represents the read and write implementation of the Repository pattern such that it can be used to query entities of a given type. Namespace: DevExpress.Mvvm.DataModel.

What is the use of Unitofwork?

The unit of work class serves one purpose: to make sure that when you use multiple repositories, they share a single database context. That way, when a unit of work is complete you can call the SaveChanges method on that instance of the context and be assured that all related changes will be coordinated.

What is generic repository pattern in Entity Framework?

It is a data access pattern that prompts a more loosely coupled approach to data access. We create a generic repository, which queries the data source for the data, maps the data from the data source to a business entity, and persists changes in the business entity to the data source.


2 Answers

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.

like image 40
David Hoerster Avatar answered Sep 28 '22 19:09

David Hoerster


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

like image 76
Ladislav Mrnka Avatar answered Sep 28 '22 17:09

Ladislav Mrnka