我在我的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
,这就是为什么我为它们创建不同的factories
TestTranslationFactory
和TestAudioFactory
当我试图为TranslationUseCase
编写测试时,这会产生问题,因为它硬编码了AudioFactory
这可能是最好的解决方案
我想到的一个解决方案是在TranslationUsecase
构造函数中传递audio_factory
以删除依赖项
目前没有回答
相关问题 更多 >
编程相关推荐