This project is read-only.
Project Description
SimpleContainer is very simple IoC container that consists of one class that implements two remarkable methods: one for registration and one for resolving.

The container class is implemented as CommonServiceLocator adapter and so it can be easily used in existing applications.

You can read about SimpleContainer development on my blog.

Quick start
// create container
var container = new SimpleContainer();
// register MyClass class for IMyInterface
container.Register<IMyInterface, MyClass>(); // if MyClass constructor has some parameter then these are resolved (dependency injection!)
// resolve instance
var instance = container.GetInstance<IMyInterface>();

Core usage

var container = new SimpleContainer();
// register new delegate (lambda function) for IMyInterface type resolving
container.Register(typeof (IMyInterface), null/* no key */, (cont, type, key) => new MyClass());
// resolve the instance
IMyInterface iface = container.GetInstance<IMyInterface>();
Registration: The Register method is the only one registration method that is available in SimpleContainer. More powerfull registration methods are available as extension methods (see bellow).
Resolving: The SimpleContainer class implements ServiceLocatorImplBase abstract class from CommonServiceLocator project and this determines the way how instances are resolved - just use GetInstance method (there is generic and non-generic overload).

When you are using some other IoC container via CommonServiceLocator then you can easily migrate to SimpleContainer. Or if you are using SimpleContainer and you want to use some more powerfull IoC container then you can easily migrate too. You don't have to rewrite whole application, you have to change just the registrations (which are always container specific).
So when you decide to use SimpleContainer then you can easily migrate to some other IoC container that has CommonServiceLocator adapter too and vice versa.

Standard usage

I'm sure that you don't want to specify delegate for each resolved type. So the project contains the second class with extension methods that make our life easier:
// registration using generic
container.Register<IMyInterface>((cont, type, key) => new MyClass());

// call to MyClass constructor is made automatically
// if MyClass constructor has some parameter then these are resolved (dependency injection!)
container.Register<IMyInterface, MyClass>();
// one instance registration (the resolving just returns this instance)
container.RegisterInstance<IMyInterface>(new MyClass());
// the same as above but the instance is automatically disposed when container is disposed
container.RegisterSingleton<IMyInterface>(new MyClass());
// there is more registration methods, i.e. for resolve overloading (via container hierarchy)

Extended usage

You can of course write your own extensions methods that handle your specific scenario. As example, I added classes for type registration that ensures that the same instance will be returned during one web-request.
container.RegisterForOneRequest<IMyInterface, MyClass>();
The implementation uses HttpContext.Current.Items class which is accessed via custom HttpModule - SimpleContainerWebRequestLifetimeModule. So when you want to use this feature in your web application then you have to register this module in your web.config file.

      <add name="SimpleContainerWebRequestLifetime" type="SimpleContainerWebRequestLifetimeModule"/>
      <remove name="SimpleContainerWebRequestLifetime" />
      <add name="SimpleContainerWebRequestLifetime" type="SimpleContainerWebRequestLifetimeModule"/>

Last edited May 19, 2010 at 12:09 PM by Augi, version 7