Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repository Pattern Step by Step Explanation [closed]

Can someone please explain to me the Repository Pattern in .NET, step by step giving a very simple example or demo.

I know this is a very common question but so far I haven't found a satisfactory answer.

like image 355
Sa Patil Avatar asked Aug 16 '12 11:08

Sa Patil


People also ask

How does repository pattern work?

The idea behind the Repository pattern is to decouple the data access layer from the business access layer of the application so that the operations (such as adding, updating, deleting, and selecting items from the collection) is done through straightforward methods without dealing with database concerns such as ...

Is the repository pattern dead?

That is, it's dead if you are using Entity Framework Core. If you're still using straight ADO.NET, or even just dapper, then the repository pattern still probably makes sense for you.

What problem does repository pattern solve?

They decouple application and domain design from persistence technology, multiple database strategies, or even multiple data sources.

What type of pattern is repository pattern?

A repository is a specialisation of the Facade pattern which is structural.


1 Answers

As a summary, I would describe the wider impact of the repository pattern. It allows all of your code to use objects without having to know how the objects are persisted. All of the knowledge of persistence, including mapping from tables to objects, is safely contained in the repository.

Very often, you will find SQL queries scattered in the codebase and when you come to add a column to a table you have to search code files to try and find usages of a table. The impact of the change is far-reaching.

With the repository pattern, you would only need to change one object and one repository. The impact is very small.

Perhaps it would help to think about why you would use the repository pattern. Here are some reasons:

  • You have a single place to make changes to your data access

  • You have a single place responsible for a set of tables (usually)

  • It is easy to replace a repository with a fake implementation for testing - so you don't need to have a database available to your unit tests

There are other benefits too, for example, if you were using MySQL and wanted to switch to SQL Server - but I have never actually seen this in practice!

like image 138
Fenton Avatar answered Oct 10 '22 22:10

Fenton