Skip to content
This repository has been archived by the owner on Sep 16, 2023. It is now read-only.
Craig Fowler edited this page Jan 17, 2018 · 15 revisions

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:

  1. Configure and then create the container
  2. Add registrations to the container
  3. Resolve a component from the container

Creating 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.

Adding registrations

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 components

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 is based on BoDi

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.