Skip to content

Eager vs. Lazy Instantiation

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

Back to Advanced IoC Configuration

By default, dependency providers are created lazily. This means that the dependency will not be created by Deft JS until another object is created which specifies that dependency as an injection.

In cases where lazy instantiation is not desired, you can set up a dependency provider to be created immediately upon application startup by using the eager configuration:

Deft.Injector.configure({
  notificationService: {
    className: "MyApp.service.NotificationService",
    eager: true
  }
});

NOTE: Only singleton dependency providers can be eagerly instantiated. This means that specifying singleton: false and eager: true for a dependency provider won't work. The reason may be obvious: Deft JS can't do anything with a prototype object that is eagerly created, since by definition each injection of a prototype dependency must be a new instance!

Next: Constructor Parameters