2

TLDR: What is the best way to use an IoC container once you have it configured?


I've been reading up on IoC (Dependency Injection, Service Locators, etc.). I get DI is good and that IoC containers can make it easier to instantiate objects, but what I have not found is anything that shows how to actually use the injection container once you have it configured. Most tutorials stop at just getting the container setup and send you on your way, but for some reason it's just not clicking as to how to best utilize your injection container once it's configured.

From what I can tell there are 2 ways I could use a container:

  1. Instantiate every conceivable dependency up front and have those flow through the application via a single entry point with a potentially massive number of dependencies.
    • This seems silly because you may be creating lots of class instances you don't really need. It also seems that you would hardly need a container to do this.
  2. Move the injection container to some global scope and call it whenever I need a new object that has dependencies.
    • This seems silly because a.) now you created a global/singleton (barf) and b.) now your dependency injector iteself is a dependency.

Both of these options don't seem ideal, so I'm wondering if I'm just missing something fundamental about how an injection container is intended to be used in practice. Any help would be appreciated!

DJ Sipe
  • 1,286
  • 13
  • 12
  • 4
    I can advice you to pick up a copy of [This book](https://www.manning.com/books/dependency-injection-in-dot-net). – Steven Aug 05 '16 at 15:52

1 Answers1

-1

You are looking for the concept of object lifetimes. Autofac has a great primer on the subject.

Bryan Watts
  • 44,911
  • 16
  • 83
  • 88
  • 1
    How does this the answer the question? While lifetimes are pretty important, especially when doing DI, the OP's question is clearly not about lifetimes... And certainly not about a specific container... – Ric .Net Aug 05 '16 at 20:41
  • @Ric.Net the question is about when to use a container during the lifetime of an application, after startup and initial composition. This is specifically what I'm referring to: "Move the injection container to some global scope and call it whenever I need a new object that has dependencies." That calling of the global container is generally done to retrieve an object with a lifetime less than the application, say, a controller for an individual request. Lifetimes are thus the concept that address that issue, and important to understanding the question in context. – Bryan Watts Aug 05 '16 at 21:31
  • @Ric.Net And other downvoter I'd really like to see a reasoned counterargument from you, esp after you've reread the OP and the primer. Or perhaps an answer if you believe it to be so waay off base. (I personally don't have the energy to answer container questions much though you may want to downvote this [answer of mine based on the same "Oh well I'm so over thinking or talking about containers but let be nice and go look up that great reference post that springs to mind" instinct which garnered an actual upvote today](http://stackoverflow.com/a/37830713/11635)) – Ruben Bartelink Aug 06 '16 at 04:28