The repository pattern
I’m a big fan of single responsibility in code; a good example is the repository pattern. The idea behind the repository pattern is that a specific package handles every single database access. For example, if you want to get a list of users, you can directly query the database with something such as SELECT * FROM users. Still, with the repository pattern, you encapsulate that inside the repository package so you can call repository.GetUsers() and get the list of users. From the outside, you don’t need to know anything about how the data is structured, stored, or retrieved. You just call the repository and get the data.
There are some exciting benefits of using a repository pattern. Some of them can apply to your project, some of them don’t, but I think it is a good idea to know them:
- Database-agnostic: You can change your database without changing the code that interacts with the database. That means that you can have multiple...