Skip to content
Kevin B edited this page Jul 6, 2017 · 12 revisions

You can either use AutoDI with your favorite dependency injection container, or you can let the AutoDI.Container generate a default container for you.

AutoDI + AutoDI.Container

Use this one if you are not not planning on integrating with some other DI container.

  1. Add the AutoDI.Container package your project. This will also include the AutoDI, AutoDI.Fody, and AutoDI.Container nuget packages.
  2. Ensure there is a FodyWeavers.xml file at the root of your project (its Build Action should be None). For many project types it will be added for you, but for some you will need to manually add it.
  3. Add AutoDI and AutoDI.Container to the list of Weavers to run. A simple example file looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
  <AutoDI />
  <AutoDI.Container />
</Weavers>
  1. Mark constructor parameters (these should also specify a default value of null) as dependencies so they automatically get resolved.
using AutoDI;
public class MyExampleClass
{
    public MyExampleClass([Dependency]IService service = null)
    {
        //Use however you want. It will automatically be injected for you.
        //You can even check it for null, just to make sure it gets injected.
        if (service == null) throw new ArgumentNullException(nameof(service));
    }
}
  1. Done. The AutoDI.Container will be automatically generated for your at compile time, and will be used to resolve the dependencies.

For Xamarin.Android projects you will need to manually inject the container (see details below).

AutoDI + (your favorite DI container)

Use this one if you want to use another DI contianer

  1. Add the AutoDI.Fody nuget package to your project. This will also include the AutoDI nuget package.
  2. Ensure there is a FodyWeavers.xml file at the root of your project (its Build Action should be None). For many project types it will be added for you, but for some you will need to manually add it.
  3. Add AutoDI to the list of Weavers to run. A simple example file looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
  <AutoDI />
  <AutoDI.Container />
</Weavers>
  1. Create a custome resolver class that implementat AutoDI.IDependencyResolver. This class is responsible for delegating requests to your DI container of choice.
using AutoDI;
public class MyResolver : IDependencyResolver
{
    public T Resolve<T>(params object[] parameters)
    {
        //Call into your DI container to get an instance of type T.
        return ...;
    }
}
  1. Set your resolver class as the dependency resolver for AutoDI to use.
//Typically done at app start up.
AutoDI.DependencyResolver.Set(new MyResolver());
  1. Mark constructor parameters (these should also specify a default value of null) as dependencies so they automatically get resolved.
using AutoDI;
public class MyExampleClass
{
    public MyExampleClass([Dependency]IService service = null)
    {
        //Use however you want. It will automatically be injected for you.
        //You can even check it for null, just to make sure it gets injected.
        if (service == null) throw new ArgumentNullException(nameof(service));
    }
}
  1. Done. All of your dependencies will be resolved by calling into your custom resolver class.