0

I've been studying and implementing the pattern refered to as Onion Architecture (http://jeffreypalermo.com/blog/the-onion-architecture-part-3/).

One thing that makes me question if I understand correctly is the following:

All interfaces are defined in the Core assembly. Implementations may be implemented in other assemblies and will be linked up with an IOC container.

Since all interfaces are defined in the Core, and all assemblies reference Core, does this imply that every assembly has access to every interface?

I.e. you could theoretically import a UI service into the DAL since the DAL has access to the UI service interface and will have it resolved by the IOC container.

Doesn't this break the purpose of layered design a bit? In a traditional model this would not be possible since the DAL assembly does not reference the UI assembly.

Am I missunderstanding something or is this sort of "abuse" possible in the Onion architecture as opposed to a normal N-tier architecture?

Joacim A
  • 26
  • 3

1 Answers1

2

All interfaces are defined in the Core assembly. Implementations may be implemented in other assemblies and will be linked up with an IOC container.

Interfaces are defined in every layer, and implemented in any layer outer. So a layer depends on all the layers inside it. The number of layers is not fixed, you may have the layers you want. If by assembly you mean a jar file, each layer there will be an assembly.

Since all interfaces are defined in the Core, and all assemblies reference Core, does this imply that every assembly has access to every interface?

I don't understand well your question but I guess what you mean is: "Can a layer access any interface of the inner layer?" The answer is "yes". And not just of the inner layer, but of any inner layer.

I.e. you could theoretically import a UI service into the DAL since the DAL has access to the UI service interface and will have it resolved by the IOC container

Yes, you could do it. You shouldn't though, but the architecture would allow to do it.

Doesn't this break the purpose of layered design a bit? In a traditional model this would not be possible since the DAL assembly does not reference the UI assembly

Yes, you are right. This ocurr because the layers are circles, and because in each circle, the pattern says nothing about spliting it.

Am I missunderstanding something or is this sort of "abuse" possible in the Onion architecture as opposed to a normal N-tier architecture?

You are right.

That's the difference with hexagonal architecture: hexagonal is more explicit and you have different adapters, instead of layers.

Here you have another question and answer about onion compared to hexagonal:

Onion architecture compared to hexagonal

choquero70
  • 4,470
  • 2
  • 28
  • 48
  • Thank you, this made it a bit more clear. "Interfaces are defined in every layer", this might be the crux of my confusion. I was under the impression that all interfaces shall be defined in the Core (inner most layer), regardless of what kind of interface it is. So you're saying that the DAL interfaces could be put in the same layer as the DAL implementations? That would make it a bit more "safe". – Joacim A Dec 21 '18 at 11:46
  • No, an interface and its implementation arent in the same layer. The implementation is in the outer layers. "Interfaces are defined in every layer" means that each layer defines the interfaces that will be implemeted by the outer layer. It doesnt mean that the same interface is defined in all layers. DAL interfaces are defined in the domain layer. DAL implementations are in the infraestructure layer. – choquero70 Dec 21 '18 at 20:34
  • An interface is defined once, in a certain layer, say layer N. An implementation of that interface will be in layer N+1, or even in an outer layer (N+2,...). "Interfaces are defined in every layer" means that not all interfaces are defined in just a layer, but each layer defines certain interfaces, that will be implemeted by an outer layer. – choquero70 Dec 21 '18 at 20:43