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.
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.
精彩评论