/NanoIoC

A tiny IoC container that does exactly what you want. And nothing more.

Primary LanguageC#

NanoIoC

A tiny IoC container, does exactly what you want, and only that.

Getting Started

Container.Global is a static instance of IContainer. You can use this as your entry point.

Manually Registering Dependencies

Use either of these methods:

void IContainer.Register<TAbstract, TConcrete>(Lifecycle lifecycle = Lifecycle.Singleton);
void IContainer.Register(Type abstract, Type concrete, Lifecycle lifecycle = Lifecycle.Singleton);

You will typically want to put your registrations inside an IContainerRegistry.

NanoIoC will find all IContainerRegistrys in all assemblies in the application's base directory (excluding those that start with the System namespace)

To run all the registries, use:

void IContainer.RunAllRegistries();

Auto Registering Dependencies

You can create TypeProcessors that scan all types allowing you to auto-wire them up.

NanoIoC will find all TypeProcesors in all assemblies in the application's base directory (excluding those that start with the System namespace)

For example:

void IContainer.RunAllTypeProcessors();

Where one of your TypeProcessors might look like:

public class ExampleTypeProcessor : ITypeProcessor
{
	public void Process(Type type, IContainer container)
	{
		if(typeof(MyInterface).IsAssignableFrom(type) && type != typeof(MyInterface))
			container.Register(typeof(MyInterface), type, Lifecycle.Singleton);
	}
}

Resolving Dependencies:

Use either of these methods:

T IContainer.Resolve<T>();
object IContainer.Resolve(Type type);

You can resolve concrete types that aren't registered, as long as all their dependencies are registered or directly constructable.

You can get all registered types:

IEnumerable IContainer.ResolveAll()
IEnumerable IContainer.ResolveAll(Type type);

Injecting instances:

You can inject existing instances:

void IContainer.Inject(T instance, Lifecycle lifeCycle = Lifecycle.Singleton);
void IContainer.Inject(object instance, Type type, Lifecycle lifecycle);