Well, it is pretty simple, as a matter of fact, here is the full source code for the container:
public class DemoContainer
{
public delegate object Creator(DemoContainer container);
private readonly Dictionary<string, object> configuration
= new Dictionary<string, object>();
private readonly Dictionary<Type, Creator> typeToCreator
= new Dictionary<Type, Creator>();
public Dictionary<string, object> Configuration
{
get { return configuration; }
}
public void Register<T>(Creator creator)
{
typeToCreator.Add(typeof(T),creator);
}
public T Create<T>()
{
return (T) typeToCreator[typeof (T)](this);
}
public T GetConfiguration<T>(string name)
{
return (T) configuration[name];
}
}
Not really hard to figure out, right? And the client code is as simple:
DemoContainer container = new DemoContainer();
container.Register<IRepository>(delegate
{
return new NHibernateRepository();
});
container.Configuration["email.sender.port"] = 1234;
container.Register<IEmailSender>(delegate
{
return new SmtpEmailSender(container.GetConfiguration<int>("email.sender.port"));
});
container.Register<LoginController>(delegate
{
return new LoginController(
container.Create<IRepository>(),
container.Create<IEmailSender>());
});
Console.WriteLine(
container.Create<LoginController>().EmailSender.Port
);