开发者

Overuse of "service" classes when using DI containers?

开发者 https://www.devze.com 2023-02-17 13:21 出处:网络
I\'m beginning to learn about to how to use DI containers. I\'m finding myself using a lot of \"service\" classes (e.g. ParsingService) with a few dependencies injected.

I'm beginning to learn about to how to use DI containers. I'm finding myself using a lot of "service" classes (e.g. ParsingService) with a few dependencies injected.

开发者_JS百科

Is it possible that I'm overusing service classes if that's what I gravitate towards whenever I need a central place to consume several dependencies?

Thank you.


Often, you can use Constructor Injection instead of "Services" to accomplish the same, or similar goals. This also often has many advantages, and is more in line with many IoC container goals.

I'd recommend Martin Fowler's article on the subject, particularly his section Service Locator vs Dependency Injection. He discusses the advantages of both approaches.

0

精彩评论

暂无评论...
验证码 换一张
取 消