Skip to content

Singleton vs. Prototype Dependency Providers

Brian Kotek edited this page Aug 20, 2013 · 5 revisions

Back to Advanced IoC Configuration

By default, the dependency providers set up with the Deft JS Injector are singletons. This means that only one instance of that dependency will be created, and the same instance will be injected into all objects that request that dependency.

For cases where this is not desired, you can create non-singleton (prototype) dependency providers like this:

Deft.Injector.configure({
  editHistory: {
    className: "MyApp.util.EditHistory",
    singleton: false
  }
});

So in this case, when a class specifies editHistory as an injection, Deft JS will create a new instance of EditHistory and inject it.

Next: Eager vs. Lazy Instantiation