Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overhead from using Dependency Injection

Does dependency injection potentially cause large overhead?

I would imagine so, especially if the resolver is called many times (which is quite likely looking at pattern examples)? Or am I thinking wrong? Unfortunately I can't test for myself as I have never used it but planned on using it.

like image 454
Alex Avatar asked Sep 13 '09 00:09

Alex


1 Answers

Unless you're using a service locator, I doubt that the overhead will make a significant difference. (Even if you are, it's unlikely to be significant.)

Using constructor injection and a modern framework, the resolver will get called when objects are constructed. For the most part I suspect you'll find that the objects with dependencies are relatively high-level components, long-lived, or both.

If you're using an IoC container and creating a lot of objects with dependencies in a tight loop, you might need to do some optimization. You could always profile or benchmark it.

In short, I wouldn't worry about it.

like image 155
TrueWill Avatar answered Sep 29 '22 07:09

TrueWill