Created
May 29, 2013 16:04
-
-
Save bartelink/5671464 to your computer and use it in GitHub Desktop.
Ninject Scoping Learnings and Tests to accompany http://stackoverflow.com/a/15836383/11635
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class NinjectScopingLearning | |
{ | |
public static class InParentScope | |
{ | |
[Fact] | |
public static void WorksIffParentScopeAllWayUp() | |
{ | |
var kernel = new StandardKernel(); | |
kernel.Bind<Root>().ToSelf().InSingletonScope(); | |
kernel.Bind<Child>().ToSelf().InParentScope(); | |
kernel.Bind<DisposableLeaf>().ToSelf().InParentScope(); | |
var root = kernel.Get<Root>(); | |
kernel.Components.Get<ICache>().Clear( root ); | |
Assert.True( root.Child.Leaf.IsDisposed ); | |
} | |
} | |
public static class InCallScope | |
{ | |
[Fact] | |
public static void ClearRootWorks() | |
{ | |
var kernel = new StandardKernel(); | |
kernel.Bind<DisposableLeaf>().ToSelf().InCallScope(); | |
var root = kernel.Get<Root>(); | |
kernel.Components.Get<ICache>().Clear( root ); | |
Assert.True( root.Child.Leaf.IsDisposed ); | |
} | |
[Fact( Skip = "No longer works on Unstable due to fixes to InCallScope" )] | |
public static void ClearRootDisposesAllChilrenCreatedWithContextPreservingGet() | |
{ | |
var kernel = new StandardKernel(); | |
kernel.Bind<Child>().ToSelf() | |
.InCallScope() | |
.WithConstructorArgument( "leaf", ctx => ctx.ContextPreservingGet<DisposableLeaf>() ); | |
kernel.Bind<DisposableLeaf>().ToSelf().InCallScope(); | |
var root = kernel.Get<Root>(); | |
kernel.Components.Get<ICache>().Clear( root ); | |
Assert.True( root.Child.Leaf.IsDisposed ); | |
} | |
[Fact] | |
public static void DisposeKernelWorks() | |
{ | |
var kernel = new StandardKernel(); | |
kernel.Bind<DisposableLeaf>().ToSelf().InCallScope(); | |
var root = kernel.Get<Root>(); | |
kernel.Dispose(); | |
Assert.True( root.Child.Leaf.IsDisposed ); | |
} | |
} | |
public static class DefinesNamedScopeRoot | |
{ | |
[Fact] | |
public static void WorksIffLeafIsSameNamedScope() | |
{ | |
var kernel = new StandardKernel(); | |
kernel.Bind<Root>().ToSelf().DefinesNamedScope( "a" ); | |
kernel.Bind<DisposableLeaf>().ToSelf().InNamedScope( "a" ); | |
var root = kernel.Get<Root>(); | |
kernel.Components.Get<ICache>().Clear( root ); | |
Assert.True( root.Child.Leaf.IsDisposed ); | |
} | |
} | |
} | |
public static class RequestHandlerScoping | |
{ | |
[Fact] | |
public static void InRequestScopedLeafIsDisposed() | |
{ | |
var kernel = CreateKernelWithCqrsModule(); | |
kernel.Bind<DisposableLeaf>().ToSelf().InRequestScope(); | |
DispatchShouldDisposeLeaf( kernel ); | |
} | |
[Fact] | |
public static void InRequestScopedLeafViaContextPreservingGetIsDisposed() | |
{ | |
var kernel = CreateKernelWithCqrsModule(); | |
kernel.Bind<Child>().ToSelf() | |
.WithConstructorArgument( "leaf", ctx => ctx.ContextPreservingGet<DisposableLeaf>() ); | |
kernel.Bind<DisposableLeaf>().ToSelf().InRequestScope(); | |
DispatchShouldDisposeLeaf( kernel ); | |
} | |
[Fact] | |
public static void InRequestScopedLeafViaContextPreservingGetFromRequestScopedParentIsDisposed() | |
{ | |
var kernel = CreateKernelWithCqrsModule(); | |
kernel.Bind<Child>().ToSelf() | |
.InRequestScope() | |
.WithConstructorArgument( "leaf", ctx => ctx.ContextPreservingGet<DisposableLeaf>() ); | |
kernel.Bind<DisposableLeaf>().ToSelf().InRequestScope(); | |
DispatchShouldDisposeLeaf( kernel ); | |
} | |
static void DispatchShouldDisposeLeaf( StandardKernel kernel ) | |
{ | |
var leaf = default( DisposableLeaf ); | |
kernel.Get<IHandlerComposer>().ComposeCallDispose( typeof( Root ), instance => leaf = ((Root)instance).Child.Leaf ); | |
Assert.True( leaf.IsDisposed ); | |
} | |
static StandardKernel CreateKernelWithCqrsModule() | |
{ | |
return new StandardKernel( new Module() ); | |
} | |
} | |
public class Module : NinjectModule | |
{ | |
public override void Load() | |
{ | |
Bind<IHandlerComposer>().To<NinjectRequestScopedHandlerComposer>(); | |
// Wire it up so InRequestScope will work for Handler scopes | |
Bind<INinjectRequestHandlerScopeFactory>().To<NinjectRequestHandlerScopeFactory>(); | |
NinjectRequestHandlerScopeFactory.NinjectHttpApplicationPlugin.RegisterIn( Kernel ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment