Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is repository pattern only to be used with Entity Framework?

I have been using Entity Framework and the repository pattern for some time now.

I was asked the other day to write a data layer without using Entity Framework, just plain old ADO.NET. I was wondering what would be the best approach for this? Do I also use a repository pattern for my CRUD operations using plain old ADO.NET?

If I go to Codeplex and search for repository pattern then 99.9% of all the sample projects use Entity Framework. Is there a different pattern that needs to be used if I use plain ADO.NET with stored procedures?

like image 262
Brendan Vogt Avatar asked Apr 10 '13 16:04

Brendan Vogt


2 Answers

No, the repository pattern is used extensively outside of the Entity Framework, and is an all round useful way of handling data access.

From MSDN

  • It centralizes the data logic or Web service access logic.
  • It provides a substitution point for the unit tests.
  • It provides a flexible architecture that can be adapted as the overall design of the application evolves.

http://msdn.microsoft.com/en-us/library/ff649690.aspx

Other benefits:

  • Simple to add logic in the repository, such as caching results over a web request
  • Common query's can be added, such as userRepository.FindByEmailAddress(emailAddress);
  • Repository can be changed out with another, such as switching a dabase to a web service with minimal effort
like image 179
Paul Grimshaw Avatar answered Sep 28 '22 03:09

Paul Grimshaw


I don't think this is the right way. But there are some assumptions

Adding a Repository pattern on top of EF code. This keep distances you from the features of your ORM. The Entity Framework is already an abstraction layer over your database.

If you want to use the Dependency Injection and Test Driven Development over EF then you follow the Repository Pattern. By using RP your code become testable and Injectable / maintainable.

Out of the box EF is not very testable, but it's quite easy to make a mockable version of the EF data context with an interface that can be injected.

If we don’t want our code to be testable or injectable then just don’t use RP.

I saw a blog post: http://www.nogginbox.co.uk/blog/do-we-need-the-repository-pattern

like image 43
JSJ Avatar answered Sep 28 '22 02:09

JSJ