问题描述
使用带有Clean Architecture的DDD,我首先实例化所有依赖项(例如存储库和服务),并将其注入到UseCases中。我发现随着时间的推移,我对每个UseCase的依赖关系列表已经变得越来越大。例如,我的UseCase使用3个聚合根,因此我有3个存储库。还不错但是,随着我添加更多功能,我发现自己添加了域服务或更多存储库,并且还必须将它们注入UseCase构造函数中。在UseCase交互器中可以有10个以上的参数吗?我一直认为具有2-3个以上的参数是一种代码味道。有没有更好的方法来解决这个问题?
Using DDD w/ Clean Architecture, I'm instantiating all of my dependencies (e.g. Repositories and Services) first and injecting them into my UseCases. What I've noticed over time is that my list of dependencies for each UseCase has grown quite large over time. For example, my UseCase uses 3 Aggregate Roots so I have 3 repositories. Not so bad. But, as I add more features, I find myself adding Domain Services or more Repositories and having to inject them into the UseCase Constructor as well. Is it ok to have 10+ parameters in a UseCase Interactor? I've always thought having more than 2-3 parameters is a code smell. Is there a better way to handle this? Thank you in advance.
推荐答案
在构造函数中有很多依赖绝对是代码味道。处理它不是很容易,而且通常更多的是经验和技能,但是基本上您需要从不同的角度查看系统,并检查是否可以将某些服务/聚合替换为新的(合并)服务/聚合在你的旧的。比起这一新功能,最终还可以证明它在系统中的其他地方也有用,因此您将减少代码重复性。您必须遵循SRP。
Having to many dependencies in constructor is definitely code smell. It's not very easy to handle it and often time it's more matter of experience and skills, but basically you need to look at your system from different perspective and check whether some services/aggregates can be substituted (merged) by new one, which is dependent on your old ones. Than this new one can eventually prove useful also in other places in your system, so you will reduce your code duplicity. You must just follow SRP.
您可以在此处找到一个不错的示例:
You can find nice example here:http://blog.ploeh.dk/2010/02/02/RefactoringtoAggregateServices/
这篇关于如何处理带有清理架构的DDD中具有过多依赖项参数的UseCase Interactor构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!