1

I am struggling with the clean architecure. I have constructed a minimal example of how a usecase/interactor could talk to the database without being dependent on it:

The use case:

class SaveDatatoDBUseCase:
def __init__(self, AbstractDB):
    self.db = AbstractDB

def execute(self):
    self.db.save()

The gateway:

from abc import ABC, abstractmethod    
class AbstractDB(ABC):

def __init__(self, value):
    pass

@abstractmethod
def save(self):
        pass

The Implementation for the Database:

class DBImplemtation(AbstractDB):
def __init__(self):
    pass

def save(self):
   print("INSERT INTO TABLE WHERE ...")

Wiring it altogether:

if __name__ == "__main__":
dbImpl = DBImplemtation()
save_to_db_Usecase = SaveDatatoDBUseCase(dbImpl)
save_to_db_Usecase.execute()

yields to:

INSERT INTO TABLE WHERE ...

Is this how the communcation works between the UseCase/Interactor and the Database or I am I missing something here? I added a picture to clarify what I wanted to achieve with the code.

Can someone please confirm, whether this is a good minimal approach and if not, where I got this wrong or even what could be improved. Thank you in advance.

This is what I wanted to achieve

  • You are in the right path. What you named Gateway is often named Repository, Store or DAO, but this doesn't change the overall picture. UseCase -> Data Access Abstractation <- Data Access Implementation – Laiv Mar 3 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.