Skip to content

Immutable components of parent kernel #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 98 additions & 13 deletions src/Ninject.Extensions.ChildKernel.Test/ChildKernelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,66 @@ namespace Ninject.Extensions.ChildKernel
using Ninject.Parameters;
using Ninject.Planning.Bindings;
using Ninject.Planning.Bindings.Resolvers;
using Ninject.Selection;
using Ninject.Syntax;

using Xunit;


public class ChildKernelTestWithIResolutionRoot : BaseChildKernelTest
{
/// <summary>
/// Initializes a new instance of the <see cref="ChildKernelTestWithIResolutionRoot"/> class.
/// </summary>
public ChildKernelTestWithIResolutionRoot()
{
this.parentKernel = new StandardKernel();
this.testee = new ChildKernel((IResolutionRoot)this.parentKernel);
}
}

public class ChildKernelTestWithParentAndSettings : BaseChildKernelTest
{
/// <summary>
/// Initializes a new instance of the <see cref="ChildKernelTestWithParentAndSettings"/> class.
/// </summary>
public ChildKernelTestWithParentAndSettings()
{
this.parentKernel = new StandardKernel();
this.testee = new ChildKernel(this.parentKernel, this.parentKernel.Settings);
}
}

public class ChildKernelTestWithComponents : BaseChildKernelTest
{
/// <summary>
/// Initializes a new instance of the <see cref="ChildKernelTestWithComponents"/> class.
/// </summary>
public ChildKernelTestWithComponents()
{
this.parentKernel = new StandardKernel();
this.testee = new ChildKernel(
this.parentKernel,
this.parentKernel.Settings,
this.parentKernel.Components);
}
}

public class ChildKernelTestWithOnlyKernel : BaseChildKernelTest
{
/// <summary>
/// Initializes a new instance of the <see cref="ChildKernelTestWithOnlyKernel"/> class.
/// </summary>
public ChildKernelTestWithOnlyKernel()
{
this.parentKernel = new StandardKernel();
this.testee = new ChildKernel(this.parentKernel);
}
}

/// <summary>
/// Tests the implementation of <see cref="ChildKernel"/>.
/// </summary>
public class ChildKernelTest
public abstract class BaseChildKernelTest
{
/// <summary>
/// Name parent's foo.
Expand All @@ -62,21 +115,12 @@ public class ChildKernelTest
/// <summary>
/// The object under test.
/// </summary>
private readonly IKernel testee;
protected IKernel testee;

/// <summary>
/// The parent kernel.
/// </summary>
private readonly IKernel parentKernel;

/// <summary>
/// Initializes a new instance of the <see cref="ChildKernelTest"/> class.
/// </summary>
public ChildKernelTest()
{
this.parentKernel = new StandardKernel();
this.testee = new ChildKernel(this.parentKernel);
}
protected IKernel parentKernel;

/// <summary>
/// All known dependencies the are resolved on child kernel.
Expand Down Expand Up @@ -181,6 +225,47 @@ public void SelectCorrectConstructorWhenBindingsAcrossKernels()
baz.Bar.Should().NotBeNull();
}

[Fact]
public void ChildKernelCannotAccessParentKernelComponents()
{
this.testee.Components.Add<IMissingBindingResolver, BarMissingBindingResolver>();

var nameOfBarMissingBindingResolver = nameof(BarMissingBindingResolver);

this.parentKernel.Components
.GetAll<IMissingBindingResolver>()
.Select(i => i.GetType().Name)
.FirstOrDefault(i => i.Equals(nameOfBarMissingBindingResolver, StringComparison.InvariantCultureIgnoreCase))
.Should().BeNull();
}

[Fact]
public void DisposeOfChildKernelDoesNotChangeParentKernelComponents()
{
this.parentKernel.Components.Add<IMissingBindingResolver, BarMissingBindingResolver>();
this.testee.Dispose();

this.parentKernel.Components.Get<IMissingBindingResolver>().Should().NotBeNull();
}

[Fact]
public void AddImmutableComponent()
{
Assert.Throws<InvalidOperationException>(() => this.testee.Components.Add<ISelector, Selector>());
}

[Fact]
public void RemoveImmutableComponent()
{
Assert.Throws<InvalidOperationException>(() => this.testee.Components.Remove<ISelector, Selector>());
}

[Fact]
public void RemoveAllImmutableComponents()
{
Assert.Throws<InvalidOperationException>(() => this.testee.Components.RemoveAll(typeof(ISelector)));
}

public class BarMissingBindingResolver : NinjectComponent, IMissingBindingResolver
{
private readonly IKernel kernel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Ninject.Extensions.ChildKernel</RootNamespace>
<AssemblyName>Ninject.Extensions.ChildKernel.Test</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SignAssembly>false</SignAssembly>
<AssemblyOriginatorKeyFile>..\Ninject.snk</AssemblyOriginatorKeyFile>
Expand All @@ -34,7 +34,8 @@
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<TargetFrameworkProfile>
</TargetFrameworkProfile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -61,7 +62,7 @@
</Reference>
<Reference Include="Ninject, Version=2.1.0.58, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\Ninject\net-3.5\Ninject.dll</HintPath>
<HintPath>..\..\lib\Ninject\net-4.5\Ninject.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">
Expand Down
6 changes: 6 additions & 0 deletions src/Ninject.Extensions.ChildKernel/ChildActivationCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace Ninject.Extensions.ChildKernel
{
using Ninject.Activation.Caching;
using Ninject.Components;
using System;

/// <summary>
/// The activation cache of child kernels.
Expand All @@ -38,6 +39,11 @@ public class ChildActivationCache : NinjectComponent, IActivationCache
/// <param name="kernel">The kernel.</param>
public ChildActivationCache(IKernel kernel)
{
if (kernel == null)
{
throw new ArgumentNullException(nameof(kernel));
}

this.parentCache = ((IChildKernel)kernel).ParentResolutionRoot.Get<IKernel>().Components.Get<IActivationCache>();
}

Expand Down
Loading