Skip to content

Simple Usage of Lightweight IOC Container

Simon Gockner edited this page Oct 15, 2019 · 9 revisions

This is a simple usage guide of the Lightweight IOC Container.

Instantiate container

The easiest way to instantiate the IocContainer is to create an instance of it at the start of your application:

IocContainer container = new IocContainer();

Register types with the container

To be able to resolve instances from the IocContainer, you need to register them first.
There are multiple ways to accomplish this and the best solution depends on the complexity of your project. In this simple usage guide we will only take a look at the most straightforward way that is to just register the interfaces and their classes with the IocContainer:

container.Register<IFoo, Foo>();

There are multiple lifestyles available, make sure you choose the correct one for your need.

Resolving instances

To resolve an instance with the IocContainer, do the following:

IFoo interface = container.Resolve<IFoo>();

Disposing Container

When your application is finished make sure to dispose your IocContainer:

container.Dispose();