D
D
Dmitry2016-04-26 21:41:10
ASP.NET
Dmitry, 2016-04-26 21:41:10

Repository pattern and Active Record?

Hello! There was such a question, please explain, the Repository pattern contains only the logic of working with the object, selection, etc., but without saving it to the database, but if the Repository contains methods that save, delete the object, does the Repository turn into an Active Record? correct me if I'm wrong somewhere

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2016-04-26
Protko @Fesor

What is a repository? This is something that is responsible for storing data. Completely. That is, here is a simple interface of a simple repository:

interface UserRepository {
    public User getUser(UserID id);
    public void add(User user);
    public void remove(User user);
}

Where exactly the repository stores the data, in the database, in memory, in files, on a remote server with requests via http - these are all implementation details. That is, a repository is a pattern for abstracting our code from the data storage location.
A repository that can only do fetches is not a repository. It's a selections thing, Finder. We should also note here that the "table rows" is an object of the User type, and the repository is an abstraction from the table (very simplified. There is also the table data gateway pattern that is responsible for exactly one table, the repository can also handle relationships between tables and generally operates with the objects of the subject area and not their mapping to the base).
Active Record does exactly what the name says. This is an object that represents a single row from a table. It can insert, update or delete itself from the table. However, it cannot "find itself", for example, some other object is responsible for this (I like the name Finder, because this is exactly what the object does - it looks for our table rows). Often, for simplicity, the methods of the faders are made as static methods.
Essentially, ActiveRecord is nothing more than a simplified combination of Domain Object + Row Data Gateway .

S
Stanislav Makarov, 2016-04-27
@Nipheris

A repository is a repository for all objects of a certain class at once. Repository methods that add or remove an object take that object as a parameter. The repository methods responsible for searching return instances of the found objects. You have one repository instance for all the objects it can store (usually one instance for all objects of a single class or class hierarchy).
In the Active Record pattern, an object can save itself. In other words, the responsibility for storage issues lies with the facility itself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question