Ecto


Explicitness

Software applications are composed of many small independent units that are intricately woven together. Two of the most critical units of any application are the business logic and data access.

High level overview

Applications rely on databases to store and retrieve data. This is because databases, both relational and document-oriented, are great at storing, sorting, and retrieving massive amounts of data. That is precisely what they are designed for. However, for our application to interface with a database, we'll need to use a third party to translate for us. Databases are designed to store scalar values, while programmers usually work with higher-level abstractions, such as objects. The issue is translating the logical representation of domain models into an atomized form that is capable of being stored in a database without obscuring their properties or relationships. This is the primary objective of a database wrapper like Ecto or Active Record.

Ecto is the database wrapper that ships with Phoenix by default.

Ecto follows the repository pattern for data access.

The issue with the active record pattern is that the Model class represents both the domain model, and the data model. Contrast this with the repository pattern which mediates between the domain and data mapping layers. Instead of combining domain and data access into the same object, the repository pattern provides an explicit boundary to partition

The repository pattern isolates the domain layer from the persistence strategies.

Repository pattern

A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection.

The repository pattern uses a centralized module for communicating with the database. The business logic is separated from the data model.

author = Repo.get(Author, 1)

changeset = Ecto.Changeset.changeset(author, name: "Robert Frost")

Repo.update(changeset)

Active Record pattern

The active record pattern is an architectural pattern used to store in-memory object data in relational databases. Note that we are examining the active record pattern in general, not necessarily the active record library that ships with Rails.

author = Author.get(1)

author.name = "Robert Frost"

author.save

In Elixir, we decouple the data model from the it's behavior.

results matching ""

    No results matching ""