📜 ⬆️ ⬇️

Snake on C # and Onion aka Clean Architecture


In my last article, I casually mentioned Onion architecture. There was basically about the fact that you do not need to create extra libraries and folders, that you need to divide the code into modules and that you can write code directly to the controller and use the Entity Framework directly there BUT The domain should still be separate and all calculations should be in it . Now I want to talk about the onion architecture. Most suitable for a complex application with a lot of logic. In this article, as an example, I will use the game " Snake " because there is enough logic in it for a "rich" domain.



Legend


  1. Red color indicates dependencies.
  2. Green indicates important groups. Sub-layers
  3. The blue color of the main layers are separated which is desirable to keep separately

')

Terminology


  1. Anemic model : A data model without logic.
  2. ValueObject : Unchangeable data store without unique identifier
  3. Entity : An object with a unique identifier.
  4. Aggregate : An object containing an Entity. It has a unique identifier.
  5. Data Layer - Repository : Isolates us from what we use . Our Aggregate Store
  6. UseCase / Interactor : Isolates us from what uses us . For example from ASP.NET, gRPC, WCF, WPF, WinForms. May call Repository when needed. Itself does almost nothing and does not store. There should be a minimal logic. Only uses Entity, Aggregate, ValueObject and Repository. In the Anemic model, on the contrary, he does everything, all the logic is there.
  7. DomainEvent : An event that happened in the Domain and to which the outside world should somehow react. You can simply make a boolean property or a collection with objects near your domain in which to add events. You can process them in ApplicationService / UseCase / Interactor.


The essence


Union aka Clean is about the fact that you should be separately:

  1. A class that makes calculations or is an object representation of an entity in an application. Calculator and ResultValueObject
  2. A class that writes data to the data store or reads it from there. CalculatorRepository
  3. A class that reacts to user actions. CalculatorService
  4. Calculator and ResultValueObject must be independent of CalculatorRepository and CalculatorService


Example: Snake game


github.com/VictoremWinbringer/SnakeGameWithOnionAkaCleanArchitecture

Literature


  1. Domain Driven Design - simplify the complicated
  2. Misconceptions Clean Architecture


UPD


In the comments complained that the picture is my curve so added another one.
Note: Gateway == Repository

Source: https://habr.com/ru/post/450412/


All Articles