Skip to content

Commit

Permalink
Merge pull request #14 from Velir/issue-13
Browse files Browse the repository at this point in the history
Adding new registration extensions for autofac, and fixing resolution…
  • Loading branch information
smithc committed Feb 23, 2016
2 parents 3fb89cb + 4193ac1 commit ed67b7a
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 6 deletions.
62 changes: 62 additions & 0 deletions Jabberwocky.Autofac.Tests/Extensions/ResolutionExtensionsTEsts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Autofac;
using Jabberwocky.Autofac.Extensions;
using NUnit.Framework;

namespace Jabberwocky.Autofac.Tests.Extensions
{
[TestFixture]
public class ResolutionExtensionsTests
{
private ContainerBuilder _builder;

[SetUp]
public void Setup()
{
_builder = new ContainerBuilder();
}

[Test]
public void ResolveWithoutExceptions_MissingRegistration_ReturnsNull()
{
var retVal = _builder.Build().ResolveWithoutExceptions<string>();

Assert.IsNull(retVal);
}

[Test]
public void ResolveWithoutExceptions_ThrowsOnResolve_ReturnsNull()
{
_builder.Register(c =>
{
Assert.Fail();
return "I Failed!";
});

Assert.IsNull(_builder.Build().ResolveWithoutExceptions<string>());
}

[Test]
public void TryResolveWithoutExceptions_MissingRegistration_ReturnsNull()
{
string retVal;

Assert.IsFalse(_builder.Build().TryResolveWithoutExceptions(out retVal));
Assert.IsNull(retVal);
}

[Test]
public void TryResolveWithoutExceptions_ThrowsOnResolve_ReturnsNull()
{
_builder.Register(c =>
{
Assert.Fail();
return "I Failed!";
});

string retVal;

Assert.IsFalse(_builder.Build().TryResolveWithoutExceptions(out retVal));
Assert.IsNull(retVal);
}
}
}
96 changes: 96 additions & 0 deletions Jabberwocky.Autofac.Tests/Jabberwocky.Autofac.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{87913926-895A-422B-B462-EF685DCAE215}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Jabberwocky.Autofac.Tests</RootNamespace>
<AssemblyName>Jabberwocky.Autofac.Tests</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Autofac, Version=3.5.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<HintPath>..\packages\Autofac.3.5.2\lib\net40\Autofac.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nunit.framework, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
</ItemGroup>
<Choose>
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
</ItemGroup>
</When>
<Otherwise />
</Choose>
<ItemGroup>
<Compile Include="Extensions\ResolutionExtensionsTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Jabberwocky.Autofac\Jabberwocky.Autofac.csproj">
<Project>{315DCFFB-97B4-43E5-9ECF-3EDC7E32C93F}</Project>
<Name>Jabberwocky.Autofac</Name>
</ProjectReference>
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
</ItemGroup>
</When>
</Choose>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
36 changes: 36 additions & 0 deletions Jabberwocky.Autofac.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Jabberwocky.Autofac.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("Jabberwocky.Autofac.Tests")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("87913926-895a-422b-b462-ef685dcae215")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
5 changes: 5 additions & 0 deletions Jabberwocky.Autofac.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Autofac" version="3.5.2" targetFramework="net45" />
<package id="NUnit" version="2.6.4" targetFramework="net45" />
</packages>
36 changes: 36 additions & 0 deletions Jabberwocky.Autofac/Extensions/ResolutionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Autofac;
using Autofac.Core;

namespace Jabberwocky.Autofac.Extensions
{
public static class ResolutionExtensions
{
public static bool TryResolveWithoutExceptions<T>(this IComponentContext context, out T instance)
{
try
{
return context.TryResolve(out instance);
}
catch (DependencyResolutionException)
{
}

instance = default(T);

return false;
}

public static T ResolveWithoutExceptions<T>(this IComponentContext context)
{
try
{
return context.Resolve<T>();
}
catch (DependencyResolutionException)
{
}

return default(T);
}
}
}
5 changes: 3 additions & 2 deletions Jabberwocky.Autofac/Jabberwocky.Autofac.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
<HintPath>..\packages\Autofac.Extras.DynamicProxy2.3.0.5\lib\net40\Autofac.Extras.DynamicProxy2.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Castle.Core, Version=3.2.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<HintPath>..\packages\Castle.Core.3.2.2\lib\net45\Castle.Core.dll</HintPath>
<Reference Include="Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<HintPath>..\packages\Castle.Core.3.3.3\lib\net45\Castle.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand All @@ -53,6 +53,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Aspects\Attributes\AspectAttribute.cs" />
<Compile Include="Extensions\ResolutionExtensions.cs" />
<Compile Include="Modules\Aspected\AspectInterceptionModule.cs" />
<Compile Include="Modules\Aspected\Configuration\AspectConfiguration.cs" />
<Compile Include="Modules\Aspected\Configuration\InterceptionContext.cs" />
Expand Down
4 changes: 4 additions & 0 deletions Jabberwocky.Autofac/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<assemblyIdentity name="Autofac" publicKeyToken="17863af14b0044da" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.5.0.0" newVersion="3.5.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Castle.Core" publicKeyToken="407dd0808d44fbdc" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.3.0.0" newVersion="3.3.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
2 changes: 1 addition & 1 deletion Jabberwocky.Autofac/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
<packages>
<package id="Autofac" version="3.5.2" targetFramework="net45" />
<package id="Autofac.Extras.DynamicProxy2" version="3.0.5" targetFramework="net45" />
<package id="Castle.Core" version="3.2.2" targetFramework="net45" />
<package id="Castle.Core" version="3.3.3" targetFramework="net45" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Autofac;
using Autofac.Extras.DynamicProxy2;
using Glass.Mapper.Sc;
using Jabberwocky.Autofac.Extensions;
using Jabberwocky.Core.Utils.Extensions;
using Jabberwocky.Glass.Autofac.Glass;
using Jabberwocky.Glass.Models;
Expand Down Expand Up @@ -102,15 +103,15 @@ public static ContainerBuilder RegisterGlassServices(this ContainerBuilder build
builder.Register((c, p) =>
{
var overriddenDbParam = p.OfType<TypedParameter>().FirstOrDefault(@param => @param.Type == typeof(string));
var overriddenDb = overriddenDbParam != null ? overriddenDbParam.Value as string : null;
var overriddenDb = overriddenDbParam?.Value as string;

if (!string.IsNullOrEmpty(overriddenDb))
{
return new SitecoreService(overriddenDb);
}

var context = c.Resolve<ISitecoreContext>();
return context != null && context.Database != null && context.Database.Name != CoreDatabaseName
var context = c.ResolveWithoutExceptions<ISitecoreContext>();
return context?.Database != null && context.Database.Name != CoreDatabaseName
? (ISitecoreService)context
: new SitecoreService(DefaultDatabaseName);
}).As<ISitecoreService>().ExternallyOwned();
Expand Down
7 changes: 7 additions & 0 deletions Jabberwocky.sln
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jabberwocky.Autofac.Extras.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jabberwocky.Glass.Autofac.WebApi", "Jabberwocky.Glass.Autofac.WebApi\Jabberwocky.Glass.Autofac.WebApi.csproj", "{86B4E810-DCA7-4510-BA2B-EC1DDA9CB7FE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jabberwocky.Autofac.Tests", "Jabberwocky.Autofac.Tests\Jabberwocky.Autofac.Tests.csproj", "{87913926-895A-422B-B462-EF685DCAE215}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -136,6 +138,10 @@ Global
{86B4E810-DCA7-4510-BA2B-EC1DDA9CB7FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{86B4E810-DCA7-4510-BA2B-EC1DDA9CB7FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{86B4E810-DCA7-4510-BA2B-EC1DDA9CB7FE}.Release|Any CPU.Build.0 = Release|Any CPU
{87913926-895A-422B-B462-EF685DCAE215}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{87913926-895A-422B-B462-EF685DCAE215}.Debug|Any CPU.Build.0 = Debug|Any CPU
{87913926-895A-422B-B462-EF685DCAE215}.Release|Any CPU.ActiveCfg = Release|Any CPU
{87913926-895A-422B-B462-EF685DCAE215}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -162,5 +168,6 @@ Global
{338A9D71-3ED1-4E69-AC1B-E5033846B3A4} = {FF1D41BA-5DE4-49CC-8CFF-08B1BF700C3D}
{44DB88AB-C38E-4CAE-ADA0-BC64778A1D4A} = {338A9D71-3ED1-4E69-AC1B-E5033846B3A4}
{86B4E810-DCA7-4510-BA2B-EC1DDA9CB7FE} = {FF1D41BA-5DE4-49CC-8CFF-08B1BF700C3D}
{87913926-895A-422B-B462-EF685DCAE215} = {7EF6690B-FB53-4BC9-A915-93E893FE3225}
EndGlobalSection
EndGlobal

0 comments on commit ed67b7a

Please sign in to comment.