Onion architecture and independence

Trying to understand onion architecture. As I understand it, there is different opinions about the names of the layers.
In this post I refer to the layers as: infrastructure -> api -> domain -> core.

The core concept is, as I understood it, that the outer layers can know about the inner layers, but that the inner can not know about the outer.

Question

The question that appears to me is how to handle dependencies. I illustrated in the example below my assumption. The question is if this assumption is correct, which I make a point in the end why I believe it is not.

Example

A db query. The domain has an aggregate root with a repository that retrieves data from a gateway. The gateway is an interface that a driver adapter is implementing. The adapter is located in the infrastructure, sense it has to have access to the driver (I read however that adapters should go in the api). So the repository must be injected with a gateway, that is an adapter, by the controller through the aggregate it uses. Meaning that the controller must be injected with a Gateway.

Assumption

  • Domain
    • Aggregate
    • Repository -> returns an entity, located in the core, along with value objects etc..
    • Gateway (interface)
  • Api
    • Controller -> injected with a gateway by the infrastructure
  • Infrastructure
    • Driver
    • Adapter (implements Gateway)

1.) This means that the domain can never know the database structure.

2.) If we have multiple persistence engines, which I do in my case, the controller can’t know which one of the adapters to use, sense it has no knowledge of anything but the gateway. So the infrastructure needs to have knowledge of which adapter to inject where and when (this seems very strange to me)

5

In you Assumptions you do not distinguish between infrastructureInterfaces and infrastructureImplementation

I see these layers

  • infrastructureInterfaces -> api -> domain -> core -> app
  • infrastructureInterfaces -> infrastructureImplementation -> app

The app knows every thing and is responsible to instatiate
infrastructureInterfaces with infrastructureImplementation.

Domain and Api only need to know the repositoryInterface
while the app initialises Domain and Api with a concrete RepositoryImplementation

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *