Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Dependency Injection

Angel3 uses a container hierarchy for DI. Dependency injection makes it easier to build applications with multiple moving parts, because logic can be contained in one location and reused at another place in your application.

Adding a Singleton

Future<void> myPlugin(Angel app) async {
  app.container.registerSingleton(SomeClass('foo'));
  app.container.registerSingleton<SomeAbstractClass>(MyImplClass());
  app.container.registerFactory((_) => SomeClass('foo'));
  app.container.registerLazySingleton((_) => SomeOtherClass());
  app.container.registerNamedSingleton('yes', Yes());
}

You can also inject within a RequestContext, as each one has a container property that extends from the app's global container.

Accessing these injected properties is easy, and strongly typed:

// Inject types.
var todo = req.container!.make<Todo>();
print(todo.isComplete);

// Or by name
var db = req.container!.findByName<Db>('database');
var collection = db.collection('pets');

Registering and reading instances, as above, needs no reflection.

In Routes and Controllers

In Angel3, by wrapping a function in a call to ioc, you can automatically inject the dependencies of any route handler. Reflecting on the function needs a reflector (see below); without one, describe its parameters with injection::

With a reflector:

ioc finds the parameters of the function with the app's reflector. Without one, as in an app compiled with dart compile exe, list them in an InjectionRequest; a parameter given as [name, type] is read from the route parameters:

As you can imagine, this is very useful for managing things such as database connections.

Dependency-Injected Controllers

Controllers have dependencies injected without any additional configuration by you. However, you might want to inject dependencies into the constructor of your controller.

Enabling dart:mirrors or other Reflection

By default, Angel() has no reflector, so that it can be used where dart:mirrors is not available, such as in apps compiled with dart compile exe and in Flutter. Without a reflector, these features are unavailable:

  • @Expose on controller methods

  • The @Middleware and @Hooks annotations, which are ignored

  • Dependency injection into constructors, whether in controllers or plain container.make calls

  • ioc without injection:

To use them, pass a reflector to the app:

  • On the Dart VM, MirrorsReflector of package:angel3_container_mirrors uses dart:mirrors:

  • To compile with dart compile exe, mark the classes with @injectable, generate their reflection data with package:angel3_container_generator and dart run build_runner build, and pass it to StaticReflector. Each library gets one variable named after its file, such as servicesReflection for services.dart:

Mark controllers, services with @Hooks or @Middleware, and top-level functions used as handlers with @injectable. A closure cannot be described, so ioc((Foo foo) => ...) still needs injection: with generated data. The angel3_container_generator README lists its limitations.

Next Up

Continue reading to learn about Middleware.

Last updated

Was this helpful?