Decoupling Python Code: Implementing the Unit of Work and Repository Pattern
Tags: programming, tutorial, python, webdev
Published: 2023-02-24 15:58:19

The repository and unit of work architectural patterns are utilised to create loosely coupled abstractions between the data access and business logic layers. Implementing these patterns helps insulate your application from changes, facilitates automated unit testing or test-driven development (TDD) and makes it easier to manage and maintain over time.
When introducing an architectural pattern, a cost-benefit analysis should be performed. Repositories are an excellent example of this; they add low-level complexity but reduce high-level complexity, meaning each domain object will need its own repository class. In return, a simple interface over the data access layer is provided. A concrete implementation would look something like this:

{% embed https://gist.github.com/ShaneNolan/9242b4494d0f49972f13c3525622a186 %}
<center>
Concrete Implementation of the Repository and Unit of Work Architectural Patterns.
</center>

---

## Repository Pattern
The repository is a layer that sits between the business logic of the application and the data storage and is responsible for retrieving and storing data. Every repository will have two methods: add()to store an object and get_by_id(id: int) to retrieve a stored object by its ID. I prefer to use generics since implementation logic is mostly the same and prevents code duplication.

{% embed https://gist.github.com/ShaneNolan/83286332bee12ed8b438e588f88f44c0 %}
<center>
Generic Repository Interface.
</center>

This implementation uses an Account model class with a username and password field. Since Account is a domain model, it requires a repository. Repositories can have a variety of unique methods to query the backend. For example, IAccountRepository has an additional method: get_all() to return all accounts.

{% embed https://gist.github.com/ShaneNolan/9b846a506e2aa380bdac422395a70191 %}
<center>
Concrete AccountRepository Implementation.
</center>

The AccountRepository is loosely coupled to SQLAlchemy's Session class through the use of [Dependency Injection](https://towardsdev.com/loosely-coupled-python-code-with-dependency-injection-d9adc6725d4a) via its constructor and inherits the IAccountRepository interface. This is important because, let's say, a future feature requires a NoSQL backend database. It can be easily implemented by passing the NoSQL database context into a repository class and implementing the interface without any change to high-level modules.

## Unit of Work Pattern
The unit of work implementation is responsible for managing the changes made to a set of objects. It tracks the changes made to these objects and, when asked, can commit or roll back the changes as a single transaction. Put simply; it ensures that multiple repositories share a single database context. That way, when a unit of work is committed, all related changes will be coordinated and saved.

{% embed https://gist.github.com/ShaneNolan/3e9f456239521e161586e44e61cbcc15 %}
<center>
Interface for the UnitOfWork Pattern.
</center>

The IUnitOfWork interface provides access to the AccountsRepository through its public account attribute. The __enter__ magic method retrieves a session, normally from a session factory. For example, SQLAlchemy has sessionmaker. The __exit__ magic method handles rolling back any changes and closing the session.

{% embed https://gist.github.com/ShaneNolan/ebaef66f012bac531749f76f994682bc %}
<center>
Concrete UnitOfWork Implementation.
</center>

For demonstration purposes, let's assume there's a requirement to get the sum of all account balances. This can be easily implemented by using the UnitOfWork class, calling the AccountRepository.get_all() method and adding each account's balance together.

{% embed https://gist.github.com/ShaneNolan/fe997804a1250ade406c5f6d41cc2a4d %} 
<center>
Implement to get the Sum Of All Account Balances.
</center>

In summary, the unit of work and repository design pattern are powerful techniques for decoupling code in Python. These patterns provide a modular, reusable approach to organizing code that promotes maintainability and testability and helps developers build more robust and flexible applications by separating concerns. Thus, these patterns are excellent choices for anyone looking to write clean, decoupled and maintainable code in Python.

Thanks for reading 🙌. You can find me on [Linkedin](https://www.linkedin.com/in/shanenullain/) or [Github](https://github.com/ShaneNolan) if you want to connect with me.

---

References:
[Architecture Patterns with Python: Enabling Test-Driven Development, Domain-Driven Design, and Event-Driven Microservices](https://amzn.to/3I9GL1U)
[Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application](https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application)