module Athena::DependencyInjection
Overview
Athena's Dependency Injection (DI) component, ADI
for short, adds a service container layer to your project. This allows a project to share useful objects, aka services, throughout the project.
These objects live in a special struct called the ADI::ServiceContainer
(SC). Object instances can be retrieved from the container, or even injected directly into types as a form of constructor DI.
The SC is lazily initialized on fibers; this allows the SC to be accessed anywhere within the project. The ADI.container
method will return the SC for the current fiber.
Since the SC is defined on fibers, it allows for each fiber to have its own SC. This can be useful for web frameworks as each request would have its own SC scoped to that request.
This however, is up to the each project to implement.
- See
ADI::Register
for documentation on registering services. - See
ADI::ServiceContainer
for documentation on working directly with the SC. - See
ADI::Injectable
for documentation on auto injecting services into non service types.
NOTE It is highly recommended to use interfaces as opposed to concrete types when defining the initializers for both services and non-services. Using interfaces allows changing the functionality of a type by just changing what service gets injected into it. See this blog post for an example of this.
Defined in:
athena-dependency_injection.crservice_container.cr
Class Method Summary
-
.container : ADI::ServiceContainer
Returns the
ADI::ServiceContainer
for the current fiber.
Class Method Detail
Returns the ADI::ServiceContainer
for the current fiber.