This example shows how you can create an application using multiple declarative containers. Using
multiple declarative containers is a good choice for a large application. For
building a moderate or a small size application refer to Application example (single container).
We build an example micro application following the dependency injection principle. It consists
of several services with a domain logic. The services have dependencies on database & AWS S3.
Start from the scratch or jump to the section:
You can find the source code and instructions for running on the Github.
"""Services module."""importloggingimportsqlite3fromtypingimportDictfrommypy_boto3_s3importS3ClientclassBaseService:def__init__(self)->None:self.logger=logging.getLogger(f"{__name__}.{self.__class__.__name__}",)classUserService(BaseService):def__init__(self,db:sqlite3.Connection)->None:self.db=dbsuper().__init__()defget_user(self,email:str)->Dict[str,str]:self.logger.debug("User %s has been found in database",email)return{"email":email,"password_hash":"..."}classAuthService(BaseService):def__init__(self,db:sqlite3.Connection,token_ttl:int)->None:self.db=dbself.token_ttl=token_ttlsuper().__init__()defauthenticate(self,user:Dict[str,str],password:str)->None:assertpasswordisnotNoneself.logger.debug("User %s has been successfully authenticated",user["email"],)classPhotoService(BaseService):def__init__(self,db:sqlite3.Connection,s3:S3Client)->None:self.db=dbself.s3=s3super().__init__()defupload_photo(self,user:Dict[str,str],photo_path:str)->None:self.logger.debug("Photo %s has been successfully uploaded by user %s",photo_path,user["email"],)