Skip to content

Instantly share code, notes, and snippets.

@JakeGinnivan
Created August 17, 2013 09:29
Show Gist options
  • Select an option

  • Save JakeGinnivan/6256092 to your computer and use it in GitHub Desktop.

Select an option

Save JakeGinnivan/6256092 to your computer and use it in GitHub Desktop.
var cb = new ContainerBuilder();
cb.RegisterType<Foo>().As<IFoo>().InstancePerLifetimeScope();
cb.RegisterType<Bar>().As<IBar>().InstancePerMatchingLifetimeScope("LifetimeScopeTag");
var container = cb.Build();
using (var scope = container.BeginLifetimeScope("LifetimeScopeTag"))
{
IFoo foo1;
IFoo foo2;
IBar bar1;
IBar bar2;
using (var scope2 = scope.BeingLifetimeScope())
{
foo1 = scope2.Resolve<IFoo>();
bar1 = scope2.Resolve<IBar>();
}
using (var scope3 = scope.BeingLifetimeScope())
{
foo2 = scope3.Resolve<IFoo>();
bar2 = scope3.Resolve<IBar>();
}
Assert.NotSame(foo1, foo2);
Assert.Same(bar1, bar2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment