DEV Community

Cover image for Why SHOULD you use the Repository Pattern in your project? And how to do it, the easy way…
Mihai Blebea
Mihai Blebea

Posted on

Why SHOULD you use the Repository Pattern in your project? And how to do it, the easy way…

The last thing you need is another pattern to worry about…

Why can’t we just write code in a plain simple way?

Well… if you ask me, patterns have a very important role in software development.

Design Patterns VS Simple code

There is no such thing as plain simple code.

Even if you don’t know any patterns, you still use one every time you write code. It’s called “spaghetti code pattern” 😊

Yes, it might sound delicious after some long coding hours in the middle of the night, but believe me, it's a developer’s worst nightmare.

Patterns are your best friend because they help you organize your code in a way that is clear to read, flexible to extends and easy to reason with.

Patterns can make your life a lot easier and can improve the speed at which you can add more features to your project in the future.

  1. So what is the Base Repository pattern supposed to do?

  2. How does it look like?

  3. And what are his main benefits?

Base Repository pattern brings in a layer of abstraction implemented between your models (domain logic) and persistence layer (database).

It helps you decouple the models from the persistence layer, so in the future, you can easily change the database tables without affecting the logic of your app.

You can even change your whole database implementation, your models should still not care about that.

Talking about some ungrateful domain models, right? 😊

Let’s look at some code, so you can grasp the concept better

I will use Typescript for this, mostly because it brings strong types and interfaces to javascript. And also because I use it every day in my job. 😊

If you don’t know much about Typescript, I suggest you read this first: Typescript is Javascript with superpowers

Now, let’s take a look back, down the memory lane…

This is how I used to persist models into the database:

import { User } from './../models'

let user = new User('Bob', 'Smith', 29, 'front end developer')

user.persiste()

And inside the User model:

import { myslqConnection } from './mysql-connection'
export default class User
{
   private _firstName : string
   private _lastName : string
   private _age : number
   private _job : string
   constructor(
       firstName : string, 
       lastName : string, 
       age : number, 
       job : string
   ){
       this._firstName = firstName
       this._lastName  = lastName
       this._age       = age
       this._job       = job
   }
   persist()
   {
      // Somehow you need to pass in the configs for connecting to the database
      return myslqConnection.query(`
              INSERT INTO users 
              (first_name, last_name, age, job) 
              VALUES (?)`, [
          this.firstName, 
          this.lastName, 
          this.age, 
          this.job ])
    }
}

This doesn’t look right.

Here are some reasons why this is an absolute disaster:

  1. I was mixing the model as in business logic with the persistence layer. Obs: The User model should not know how it is persisted in the database because it doesn’t care about that. This ungrateful User models, they don’t care about anything… 😊

  2. I was implementing the connection to the database in the actual model, which is a bad thing if you ever want to change the credentials.

There are a lot of other reasons why this implementation is bad, but I won’t bore you with the details…

If you want to see how I fixed all these problems with the Repository Pattern, then check out the full article on Medium:

https://medium.com/@mihaiblebea/why-should-you-use-the-repository-pattern-in-your-project-and-how-to-do-it-the-easy-way-2076e0889e2b

Top comments (0)