public interface IDependency
public class DependencyOne : IDependency
Console.WriteLine(" creating DependencyOne()");
public class DependencyTwo : IDependency
Console.WriteLine(" creating DependencyTwo()");
public class LazyDependency
Console.WriteLine(" creating LazyDependency()");
public class SingletonDependency
public SingletonDependency()
Console.WriteLine(" creating SingletonDependency()");
public IDependency Dep { get; set; }
public Lazy<LazyDependency> LazyDep { get; set; }
public SingletonDependency SingleDep { get; set; }
public Application(IDependency dep, Lazy<LazyDependency> lazyDep, SingletonDependency singleDep)
Console.WriteLine(" creating Application()");
public static void Main()
var container = new Container();
container.Configure(c => {
c.ForConcreteType<LazyDependency>()
c.ForConcreteType<SingletonDependency>()
c.ForConcreteType<Application>()
Console.WriteLine("Using scope one");
using(var scoped = container.CreateChildContainer())
Console.WriteLine("Using scope two");
using(var scoped = container.CreateChildContainer())
Console.WriteLine("Using main scope, changed IDependency type");
container.Configure(c => {
c.For<IDependency>().Use<DependencyTwo>().ContainerScoped();
RunThing(container, true);
Console.WriteLine("Registering multiple instances");
container.Configure(c => {
c.For<IDependency>().ClearAll();
c.For<IDependency>().Add<DependencyOne>();
c.For<IDependency>().Add<DependencyTwo>();
Console.WriteLine($"IDependency instances: {container.GetAllInstances<IDependency>().Count()}");
public static void RunThing(IContainer container, bool useLazy = false)
var app = container.GetInstance<Application>();
app.LazyDep.Value.GetHashCode();