干净体系结构中的用例对其他用例的依赖性

2024-09-30 22:13:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我在我的python项目中使用了干净的体系结构。我有两个usecase,一个是TranslationUseCase,第二个是AudioUsecase翻译酶取决于音频用例

翻译用例

class TranslationUseCase:
   def __init(self, trans_repo):
       self.trans_repo = trans_repo


   def find_translation(self, also_audio=False):
       if also_audio:
          audio_factory = AudioFactory.find_audio()
          audio = audio_factory.find_by_id(123)

       translation = trans_repo.find_by_id(123)
       # .... Other Code

音频用例

class AudioUseCase:
     def __init(self, audio_repo):
        self.audio_repo = audio_repo


     def find_by_id(self, id):
         return self.audio_repo.find_by_id(id)

翻译工厂

 class TranslationFactory:

    @classmethod
    def find_translation(cls):
        trans_repo = TranslationRepo()
        return TranslationUseCase(trans_repo)

AudioFactory

 class AudioFactory:

    @classmethod
    def find_audio(cls):
        audio_repo = AudioRepo()
        return AudioUseCase(audio_repo)

我用这种方式,一切都很好,我不知道这是不是一个正确的方式。因为当我编写测试时有问题,在测试中有完全不同的repos,这就是为什么我为它们创建不同的factoriesTestTranslationFactoryTestAudioFactory当我试图为TranslationUseCase编写测试时,这会产生问题,因为它硬编码了AudioFactory这可能是最好的解决方案

我想到的一个解决方案是在TranslationUsecase构造函数中传递audio_factory以删除依赖项


Tags: selfidtransbyreturnfactorydefrepo