Registration
A registration is an attribute on the container class: [Singleton<EspressoMachine>] tells the container to build an EspressoMachine and share it. Lifetimes explain the three lifetime attributes. This section covers everything else you can tell the container: how to build a service, how to wrap or combine services, how to choose between several implementations, and how to register many types or whole bundles at once.
Pick the page for your task
| You want to | Use | Explained on |
|---|---|---|
| Build a service with your own code, or register an object you already have | Factory or Instance on the lifetime attribute | Factories and instances |
| Run code when an instance is activated or released, without putting it into the service | OnActivated and OnRelease | Lifecycle hooks |
| Add logging, retries or caching around a service without changing it | [Decorate] | Decorators |
| Fan one call out to every implementation of a service | [Composite] | Composites |
| Register several implementations of one service and pick one per consumer | Key, WhenInjectedInto or [FromKey] | Keyed services |
| Register a generic service once for every type argument the graph uses | The typeof form with an unbound generic | Open generics |
| Register every type that matches a marker or a naming convention | [Scan] | Scanning |
| Bundle registrations and reuse them in several containers | [Module] and [Import] | Modules |
| Ship a module from a library that keeps its implementations internal | [Module] in the library | Shipping modules from a library |
| Reuse a production container in tests and replace only some services | Extends on [Container] | Extending containers |
The attribute reference lists every attribute with all its options and defaults.
Where to go next
- Factories and instances when a constructor is not enough.
- Resolution for how consumers ask for what you registered.
- Attribute reference to look up an option.