3

Reading the blue book at page 152, we can find this:

[A repository] provide methods to add and remove objects, which will encapsulate the actual insertion or removal of data in the data store. Provide methods that select objects based on some criteria and return fully instantiated objects or collections of objects whose attribute values meet the criteria, thereby encapsulating the actual storage and query technology.

One of advantages in repositories introduction is that:

They allow easy substitution of a dummy implementation, for use in testing

Crystal clear! I can use an interface to define the repository; implement it with many classes (i.e. real database, rather than in-memory value for testing); inject one implementation or another using DI engine.

On the other hand:

The hexagonal architecture (aka ports and adapters) divides a system into several loosely-coupled interchangeable components, such as the application core, the database, the user interface, test scripts and interfaces with other systems.

To me it look like extend the repository concepts not only to a database. Do I have a notification port? Then I will implement two adapters one for RabbitMQ, another one for Amazon SNS or in-memory topic.

Put in this way, to me: a repository could be considered an adapter for the database port.

Am I missing something?

1 Answer 1

9

No, bang on.

A Repository pattern is a kind of port in an Hexagonal Architecture.

The difference is how far in/out you are zoomed.

The Repository pattern is a much more detailed perspective:

  • zoomed out it might be just a port in your hexagonal architecture.
  • zoomed out it might also be used in other architectures, such as a tiered architecture.
2
  • If the IRepository is the port, can I consider MySqlRepository or MongoDbRepository its adapters?
    – BAD_SEED
    Commented Mar 22, 2020 at 16:28
  • Yes. When you are considering it in terms of hexagonal architecture.
    – Kain0_0
    Commented Mar 22, 2020 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.