14

I see only benefits to the onion architecture over the 3 layered architecture where the BL had responsibility to call methods on DAL (or an interface of DAL) to do CRUD. The onion has better separation of concerns, testability, maintainability and is cleaner.

So is the onion architecture indeed better in all aspects and 3 layered architecture is just an old way of doing things, or there are some scenarios where I should prefer to use the 3 layered architecture, if so - which?

BornToCode
  • 1,273
  • 2
  • 13
  • 16

1 Answers1

16

Layers, Onions, Ports, Adapters: it's all the same

Since this article makes clear that onion is equivalent to 3 layer + application of the Dependency Inversion Principle (DIP), then the question becomes "where should I prefer to use DIP?" I'd say any non-toy project. Using DIP allows the core of your code to be more isolated, testable and maintainable. When you really don't care about that is when it's a throw-away project or when you are trading maintainability for performance.

Also, don't confuse DIP with DI (Dependency Injection) containers. One doesn't imply the other.

Kasey Speakman
  • 4,341
  • 19
  • 26
  • From the Layers, Onions ... link above, I don't understand how the UI will retrieve some domain entities without Data Access Library. For example, let's consider Person Domain. To obtain a list of people from UI, don't we need the Data Access Library? That means UI is dependent on Data Access Library. No? – bostonjava Apr 23 '20 at 17:04
  • @bostonjava The idea behind dependency inversion is for the dependency to be the other way around. The Person domain defines the data structure, and the DAL depends on that and loads data into it. But the Person domain doesn't know anything about it and basically takes no side effect dependencies. App/infrastructure services are responsible for organizing side effects like loading from DB and invoking domain behaviors. Since the core business code has no outside entanglements, it is easier to test and maintain. – Kasey Speakman Jul 08 '20 at 20:01