-
Notifications
You must be signed in to change notification settings - Fork 0
Home
FlexDi is a small and extensible dependency injection container, also sometimes referred to as an inversion of control container. It aims to serve a niche between 'large' DI containers and no DI at all.
The three steps which most consumers of FlexDi will go through are:
- Configure and then create the container
- Add registrations to the container
- Resolve a component from the container
The recommended way to create the container is via a builder.
var container = Container.CreateBuilder()
// Optional: Configure the container here
.Create();
The builder presents methods which control FlexDi's configuration options as well as providing access to extension points.
The next thing you will typically want to do with your container is to add registrations for the services you wish to resolve. Note that it is possible to resolve classes from a container without first registering them, if the corresponding option is enabled. Documentation may be found on the registrations wiki page but in summary, registrations are added using the following method.
container.AddRegistrations(helper => {
// Use the helper to create each registration here
});
Resolving services from the container is as simple as using an overload of the Resolve
method. This is present on both the container and the interface IResolvesServices
.
var myService = container.Resolve<IMyService>();
Other overloads of Resolve
also allow you to get a specific named instance of a service. Finally, you may also want to read about some dependency injection best practices.
FlexDi began its life as a fork of another lightweight DI container, named BoDi. BoDi was originally created for the SpecFlow project.
It is possible, via a 'compatibility' NuGet package, to use FlexDi as a drop-in replacement for BoDi.