Dependency Injection
Angel 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
You can also inject within a RequestContext
, as each one has a controller
property that extends from the app's global container.
Accessing these injected properties is easy, and strongly typed:
In Routes and Controllers
In Angel 2.0, by wrapping a function in a call to ioc
, you can automatically inject the dependencies of any route handler.
As you can imagine, this is very useful for managing things such as database connections.
Dependency-Injected Controllers
Controller
s 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
dart:mirrors
or other ReflectionBy default, Angel will use the EmptyReflector()
to power its Container
instances, which has no support for dart:mirrors
, so that it can be used in contexts where Dart reflection is not available.
However, by using a different Reflector
, you can use the full power of Angel's DI system. angel init
projects use the MirrorsReflector()
by default.
If your application is using any sort of functionality reliant on annotations or reflection, either include the MirrorsReflector, or use a static reflector variant.
The following use cases require reflection:
Use of
Controller
s, via@Expose()
or@ExposeWS()
Use of dependency injection into constructors, whether in controllers or plain
container.make
callsUse of the
ioc
function in any route
The MirrorsReflector
from package:angel_container/mirrors.dart
is by far the most convenient pattern, so use it if possible.
However, the following alternatives exist:
Generation via
package:angel_container_generator
Creating an instance of
StaticReflector
Manually implementing the
Reflector
interface (cumbersome; not recommended)
Last updated