Skip to content

Eager vs. Lazy Instantiation

brian428 edited this page Nov 14, 2012 · 5 revisions

Back to Advanced IoC Configuration

By default, dependency providers are created lazily. This means that the dependency will not be created by DeftJS 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: DeftJS 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