---
description: >-
  This infrastructure layer deals with the APIs, databases and other
  infrastructure-related dependency integrations in the system.
---

# Infrastructure Layer

## Data Source

A `Data Source` represents a remote or local data storage within the system, such as a Database, Rest API or local files. They accept and return data in the form of `Models` which are specific to the `Data Source`.

## Models

A `Model`, also referred to as a Data Transfer Object (DTO), is a data representation for a particular `Data Source`. They are translated into [Entities](../domain-layer#entity) to make them comprehensible to the application. Alternatively, `Entities` are converted into `Models` when it needs to be processed by the target `Data Source`.

## Repository (Implementation)

The `Repository Implementation` provides the logic implementation for the [Repository Interface](../domain-layer#repository-interface), which defines the functions and capabilities of a specific data repository. It is responsible for managing the data and ensuring that it is properly formatted and structured for use in the application.

Among its main tasks is to manage the in-flow and out-flow of data between `Data Source` and the application, ensuring that the Data Source `Models` are converted into [Entities](../domain-layer#entity) and vice-versa, and every exception encountered is converted into their respective [Failure](../domain-layer#failure) objects.

{% hint style="info" %}
**Why do we need to convert `Models` into `Entities` and `Exceptions` into `Failures`?**&#x20;



Essentially, this is to decouple the [Presentation Layer](presentation-layer) from the `Infrastructure Layer.`This helps to ensure that the `Presentation Layer` is independent of the specifics of the `Infrastructure Layer`, making the application more flexible and adaptable to change.

For example, if we need to make changes to the `Infrastructure Layer`, such as switching from one database to another, the data models and exceptions provided by the new database may change. By converting these models and exceptions into `Entities` and `Failures`, respectively, we can ensure that these changes are contained within the `Infrastructure Layer` and do not affect the `Presentation Layer`.
{% endhint %}
