Home > Software engineering >  Which Clean Architecture layer do factories belong to?
Which Clean Architecture layer do factories belong to?

Time:12-24

I suppose the title speaks for itself but in which layer of Clean Architecture do you typically put factories?

If the factory interface does not go in the same layer as the implementation I would like to know this also.

Thanks!

CodePudding user response:

A factory interface is considered to be an abstract concept for creating a domain entity, thus belongs to domain core circle.

A factory implementation deals with technical details of entity creation, thus belongs to infrastructure circle.

Often, the entity returned by a factory is represented by an interface or an abstract class placed within domain circle, whereas the concrete entity class is placed within infrastructure circle.

CodePudding user response:

In which layer of Clean Architecture do you typically put factories?

A factory is a concept that does not belong to one architectural layer. You can use it whereever you want to decouple one layer or architectural boundary from another.

It is used when a client in one architecturaly context needs to instantiate objects of another architectural conext that it should not depend on. E.g. entities use framework or library objects.

It is also often used by the application startup or main component. For details take a look here.

If the factory interface does not go in the same layer as the implementation I would like to know this also.

The interface is often placed beside the client that uses it, because an interface is made for a client and not an implementor. A client needs someone to do something. Therefore the client tells what it wants with an interface. The implementor shows how it is done.

 --------         ||    
| client |        ||    
 --------         ||
     |            ||
     V            ||
 -----------      ||          ------------- 
| Interface |   <-||---      | Implementor |
 -----------      ||          ------------- 

|| = architectural boundary

Sometimes you want to have an interface that serves many clients in different architectural contexts. Then you can either separate the interface from both contexts.

 ----------      ||
| client 1 |     ||
 ----------      ||
                 ||
      |          ||
======|==========||=================
      |          ||
      V          ||      
                 ||           
 -----------     ||      ------------- 
| Interface | <-------- | Implementor |
 -----------     ||      ------------- 
                 ||
      ^          ||
      |          ||
======|==========||==================
      |          ||
                 ||
 ----------      ||
| client 2 |     ||
 ----------      ||

or you can use separate interfaces for each client and let the implementor implement both.

 ----------      ||
| client 1 |     ||
 ----------      ||
      |          ||
      V          ||
 -----------     ||     
| Interface | <--------------- 
 -----------     ||           |
                 ||      ------------- 
=================||     | Implementor | 
                 ||      ------------- 
 -----------     ||           |
| Interface | <--------------- 
 -----------     ||     
      ^          ||
      |          ||
 ----------      ||
| client 2 |     ||
 ----------      ||

You can also use 2 implementors. One for each interface.

  • Related