Sharing Top Content from the Angular-sphere.

Getting Familiar With The Awesome Repository Pattern

  • The repository pattern is another abstraction, like most things in Computer Science.
  • We have to mock the data context for people and companies to have literal database records.
  • The repository pattern adds an abstraction layer over the top of data access.
  • With the repository pattern we can add a thin layer of abstraction for accessing the people and company databases.
  • Repository pattern provides us with a nice abstraction for our data.

From the DEV community. Sharing ideas that makes us all better developers.

The repository pattern is another abstraction, like most things in Computer Science. It is a pattern that is applicable in many different languages. In fact a lot of developers use the repository pattern and don’t even realize it.

In this post I am going to transform a piece of code. We start with a piece of code that is loading a single record from a database. Once the record is fetched it is returned to the caller. Let’s take a look at some code.

The record we are loading out of our database is .

The service that is loading a person out of the database is . It consists of the following method definition.

The implementation of the is handled by .

So far, this isn’t so bad. We have a business service that can retrieve a single person from the database.

But then we have a new requirement that says we also need a way to load a company from another database. So we need to add a new method and extend .

represents the model stored in the company database.

We extend to have a method that returns a company by name.

Now we are starting to see the problems with this…

Getting Familiar With The Awesome Repository Pattern