抓取创建者与依赖注入
GRASP Creator 与依赖注入完全矛盾吗?
如果不是,请解释原因。
Is GRASP Creator a complete contradiction to Dependency Injection?
If it is not, please explain why.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并不矛盾,但是它们都在不同的情况下使用。
您更有可能在域层中使用 Creator,并在应用程序服务层(日志服务、控制器工厂、身份验证服务等,具体取决于您的平台)和域服务/数据访问层(存储库、数据映射器、数据网关等)。
Larman 在“应用 UML 和模式”(其中定义了 GRASP 模式/原则)中明确了这一区别,指出在重用回收实例对于性能或决定家族中的哪个类需要很重要的情况下,Creator 是禁忌的被实例化。在这些情况下,实例化最好归入工厂、抽象工厂或 DI。
It is not a Contradiction, however they are both used in different circumstances.
You are more likely to use Creator in the Domain Layer, and use Dependency Injection or Factories in your Application Service Layers (logging service, controller factories, authentication services, etc. depending on your platform) and Domain Service / Data Access Layers (Repositories, Data Mappers, Data Gateways, etc.).
This distinction is made clear by Larman in "Applying UML and Patterns" (where GRASP Patterns/Principles are defined) stating that Creator is contraindicated in instances where reuse of recycled instances are important for performance or the decision as to which class among a family needs to be instantiated. In these instances, instantiation is better left relegated to Factory, Abstract Factory, or DI.