Naive Demo:
Boy is a boy
public interface Boy
{
string Name { get; }
}
Susan wants a boy
public class Susan : ContainerBound
{
public void FallInLove()
{
Console.WriteLine("Susan has fallen in love with " + Get<Boy>().Name);
}
}
Lucy wants a boy
public class Lucy : ContainerBound
{
public void Marry()
{
Console.WriteLine("Lucy is marrying " + Get<Boy>().Name);
}
}
Lily wants a boy
public class Lily : ContainerBound
{
public void Kiss()
{
Console.WriteLine("Lily is kissing {0}", Get<Boy>().Name);
}
}
You give girls the boy they want
Containers.GetContainerInContext<object>().Put(new GenericBoy("Van"));
Containers.GetContainerInContext<Lucy>().Put(new GenericBoy("Tom"));
Containers.GetContainerInContext<Lily>().Put(new GenericBoy("Joy"));
Containers.Close();
Now, show time
new Susan().FallInLove();
new Lily().Kiss();
new Lucy().Marry();
Susan has fallen in love with Van
Lily is kissing Joy
Lucy is marrying Tom
Behind the scene
public class ContainerBound
{
protected T Get<T>()
{
return Containers.GetReadonlyContainerInContext(GetType()).Get<T>();
}
}