Skip to content
This repository has been archived by the owner on Apr 26, 2020. It is now read-only.

Update to Unity 5.11.4 #47

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
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ public class Bootstrapper : UnityNancyBootstrapper

You can also override the `GetApplicationContainer` method and return a pre-existing container instance, instead of having Nancy create one for you. This is useful if Nancy is co-existing with another application and you want them to share a single container.

It is recommended that a child container of the application's main container is used so that if the application stops and restarts Nancy, the main container is not disposed of.

```c#
protected override IUnityContainer GetApplicationContainer()
{
// Return application container instance
return parentContainer.CreateChildContainer();
}
```

If you end up overriding `GetApplicationContainer()`, you will have to manually add the `EnumerableExtension` to your container so that it is able to resolve `IEnumerable<T>` types properly (something that Unity has poor support for).

```c#
container.AddNewExtension<EnumerableExtension>();
```
## Changes
V2.0.0 supports Unity 5.11.4

## Code of Conduct

Expand Down
9 changes: 4 additions & 5 deletions src/Nancy.Bootstrappers.Unity/EnumerableExtension.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.ObjectBuilder;
using Unity.Builder;
using Unity.Extension;

namespace Nancy.Bootstrappers.Unity
{
Expand All @@ -8,8 +8,7 @@ public class EnumerableExtension : UnityContainerExtension
protected override void Initialize()
{
// Enumerable strategy
Context.Strategies.AddNew<EnumerableResolutionStrategy>(
UnityBuildStage.TypeMapping);
Context.Strategies.Add(new EnumerableResolutionStrategy(), UnityBuildStage.TypeMapping);
}
}
}
}
43 changes: 19 additions & 24 deletions src/Nancy.Bootstrappers.Unity/EnumerableResolutionStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Practices.ObjectBuilder2;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Utility;
using Unity;
using Unity.Builder;
using Unity.Strategies;

namespace Nancy.Bootstrappers.Unity
{
Expand All @@ -17,7 +17,7 @@ namespace Nancy.Bootstrappers.Unity
/// </remarks>
public class EnumerableResolutionStrategy : BuilderStrategy
{
private delegate object Resolver(IBuilderContext context);
private delegate object Resolver(BuilderContext context);

private static readonly MethodInfo GenericResolveEnumerableMethod =
typeof(EnumerableResolutionStrategy).GetMethod("ResolveEnumerable",
Expand All @@ -31,17 +31,15 @@ public class EnumerableResolutionStrategy : BuilderStrategy
/// Do the PreBuildUp stage of construction. This is where the actual work is performed.
/// </summary>
/// <param name="context">Current build context.</param>
public override void PreBuildUp(IBuilderContext context)
public override void PreBuildUp(ref BuilderContext context)
{
Guard.ArgumentNotNull(context, "context");

if (!IsResolvingIEnumerable(context.BuildKey.Type))
if (!IsResolvingIEnumerable(context.RegistrationType))
{
return;
}

MethodInfo resolverMethod;
var typeToBuild = GetTypeToBuild(context.BuildKey.Type);
var typeToBuild = GetTypeToBuild(context.RegistrationType);

if (IsResolvingLazy(typeToBuild))
{
Expand Down Expand Up @@ -73,42 +71,39 @@ private static bool IsResolvingLazy(Type type)
return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Lazy<>);
}

private static object ResolveLazyEnumerable<T>(IBuilderContext context)
private static object ResolveLazyEnumerable<T>(BuilderContext context)
{
var container = context.NewBuildUp<IUnityContainer>();

var typeToBuild = typeof(T);
var typeWrapper = typeof(Lazy<T>);

return ResolveAll(container, typeToBuild, typeWrapper).OfType<Lazy<T>>().ToList();;
return ResolveAll(context, typeToBuild, typeWrapper).OfType<Lazy<T>>().ToList();
}

private static object ResolveEnumerable<T>(IBuilderContext context)
private static object ResolveEnumerable<T>(BuilderContext context)
{
var container = context.NewBuildUp<IUnityContainer>();

var typeToBuild = typeof(T);

return ResolveAll(container, typeToBuild, typeToBuild).OfType<T>().ToList();
return ResolveAll(context, typeToBuild, typeToBuild).OfType<T>().ToList();
}

private static IEnumerable<object> ResolveAll(IUnityContainer container, Type type, Type typeWrapper)
private static IEnumerable<object> ResolveAll(BuilderContext context, Type type, Type typeWrapper)
{
var names = GetRegisteredNames(container, type);
var names = GetRegisteredNames(context, type);

if (type.IsGenericType)
{
names = names.Concat(GetRegisteredNames(container, type.GetGenericTypeDefinition()));
names = names.Concat(GetRegisteredNames(context, type.GetGenericTypeDefinition()));
}

return names.Distinct()
.Select(t => t.Name)
.Select(name => container.Resolve(typeWrapper, name));
.Select(name => context.Resolve(typeWrapper, name));
}

private static IEnumerable<ContainerRegistration> GetRegisteredNames(IUnityContainer container, Type type)
private static IEnumerable<IContainerRegistration> GetRegisteredNames(BuilderContext context, Type type)
{
return container.Registrations.Where(t => t.RegisteredType == type);
return context.Container.Registrations.Where(t => t.RegisteredType == type);
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<Authors>Andreas Håkansson, Steven Robbins and contributors</Authors>
<CodeAnalysisRuleSet>..\..\dependencies\Nancy\Nancy.ruleset</CodeAnalysisRuleSet>
<Description>An Unity bootstrapper for the Nancy web framework.</Description>
<DisableImplicitFrameworkReferences Condition=" '$(TargetFramework)' == 'net452' ">true</DisableImplicitFrameworkReferences>
<DisableImplicitFrameworkReferences Condition=" '$(TargetFramework)' == 'net472' ">true</DisableImplicitFrameworkReferences>
<PackageIconUrl>http://nancyfx.org/nancy-nuget.png</PackageIconUrl>
<PackageLicenseUrl>https://github.com/NancyFx/Nancy/blob/master/license.txt</PackageLicenseUrl>
<PackageProjectUrl>http://nancyfx.org</PackageProjectUrl>
<PackageTags>Nancy;Unity</PackageTags>
<TargetFramework>net452</TargetFramework>
<TargetFramework>net472</TargetFramework>
<Version>2.0.0-clinteastwood</Version>
</PropertyGroup>

Expand All @@ -18,10 +18,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Unity" Version="2.1.505.2" />
<PackageReference Include="Unity" Version="5.11.4" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" />
</ItemGroup>
Expand Down
26 changes: 19 additions & 7 deletions src/Nancy.Bootstrappers.Unity/UnityNancyBootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ namespace Nancy.Bootstrappers.Unity
using System;
using System.Collections.Generic;
using Diagnostics;
using Microsoft.Practices.Unity;
using Nancy.Configuration;
using Configuration;
using Bootstrapper;
using global::Unity;
using global::Unity.Lifetime;
using ViewEngines;

/// <summary>
/// Nancy bootstrapper for the Unity container.
/// </summary>
public abstract class UnityNancyBootstrapper : NancyBootstrapperWithRequestContainerBase<IUnityContainer>
public abstract class UnityNancyBootstrapper : NancyBootstrapperWithRequestContainerBase<IUnityContainer>, IDisposable
{
private bool isDisposing = false;

/// <summary>
/// Gets the diagnostics for intialisation
/// Gets the diagnostics for initialisation
/// </summary>
/// <returns>An <see cref="IDiagnostics"/> implementation</returns>
protected override IDiagnostics GetDiagnostics()
Expand Down Expand Up @@ -250,16 +253,25 @@ protected override IEnumerable<INancyModule> GetAllModules(IUnityContainer conta
}

/// <summary>
/// Retreive a specific module instance from the container
/// Retrieve a specific module instance from the container
/// </summary>
/// <param name="container">Container to use</param>
/// <param name="moduleType">Type of the module</param>
/// <returns>An <see cref="INancyModule"/> instance</returns>
protected override INancyModule GetModule(IUnityContainer container, Type moduleType)
{
container.RegisterType(typeof(INancyModule), moduleType, new ContainerControlledLifetimeManager());
return container.Resolve(moduleType) as INancyModule;
}

public new void Dispose()
{
if (this.isDisposing)
{
return;
}

return container.Resolve<INancyModule>();
this.isDisposing = true;
base.Dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<PropertyGroup>
<CodeAnalysisRuleSet>..\..\dependencies\Nancy\Nancy.ruleset</CodeAnalysisRuleSet>
<DisableImplicitFrameworkReferences Condition=" '$(TargetFramework)' == 'net452' ">true</DisableImplicitFrameworkReferences>
<TargetFramework>net452</TargetFramework>
<DisableImplicitFrameworkReferences Condition=" '$(TargetFramework)' == 'net472' ">true</DisableImplicitFrameworkReferences>
<TargetFramework>net472</TargetFramework>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -20,14 +20,17 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommonServiceLocator" Version="1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="Unity" Version="2.1.505.2" />
<PackageReference Include="xunit" Version="2.3.0-beta2-build3683" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-beta2-build1317" />
<PackageReference Include="CommonServiceLocator" Version="2.0.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Unity" Version="5.11.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Runtime" />
<Reference Include="System" />
Expand Down