Clean Architecture – How Domain Layer Communicates with Infrastructure Layer

clean-architecture

I'm trying to get a grasp of the Clean Architecture. In the examples I found the typical solution structure consists of three projects: Core, Infrastructure, and Presentation/UI. The Core project must not have any dependencies. It contains the domain objects and business rules. The Infrastructure contains all external concerns: persistency, file system, web etc.
What I don't understand is that since the Core doesn't have any dependencies and it contains all the business rules, how does it communicate to the Infrastructure layer? For example, if it needs to send an email or save/load a file from the system how does it request such action from the Infrastructure layer?

Best Answer

This happens when you follow the Dependency inversion principle.

Your core layer will define an interface, such as Sender. Your infrastructure layer will depend on the core layer and provide an implementation of the interface, such as a EmailSender. Any class in your core layer which requires to send an email will only depend on the interface and remain ignorant of how it is actually implemented. Therefore, the core layer does not have any knowledge of the technical details of sending an email as they reside only in the infrastructure layer.

Of course, at some point, you will need to pass a concrete implementation of the interface when constructing your objects. This generally happens in some main module, which depends on all your different layers and act as a glue between all of them. It only contains the code require to run your application, leaving the business logic encapsulated and isolated in the core layer.

Related Topic