-
Notifications
You must be signed in to change notification settings - Fork 17
Quick Start
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.
Use this one if you are not not planning on integrating with some other DI container.
- Add the AutoDI.Container package your project. This will also include the AutoDI, AutoDI.Fody, and AutoDI.Container nuget packages.
- 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.
- 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>
- 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));
}
}
- 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).
Use this one if you want to use another DI contianer
- Add the AutoDI.Fody nuget package to your project. This will also include the AutoDI nuget package.
- 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.
- 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>
- 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 ...;
}
}
- Set your resolver class as the dependency resolver for AutoDI to use.
//Typically done at app start up.
AutoDI.DependencyResolver.Set(new MyResolver());
- 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));
}
}
- Done. All of your dependencies will be resolved by calling into your custom resolver class.