Added WebApiContrib.IoC.StructureMap #53

Merged
merged 1 commit into from Apr 19, 2012
View
@@ -30,8 +30,11 @@ EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApiContrib.RavenDb", "src\WebApiContrib.RavenDb\WebApiContrib.RavenDb.csproj", "{3D3C4640-9365-471A-A160-59E7C394B228}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApiContrib.Formatting.RazorViewEngine", "src\WebApiContrib.Formatting.RazorViewEngine\WebApiContrib.Formatting.RazorViewEngine.csproj", "{75FA4F0E-ACC6-424A-8809-B25E7AB17EFE}"
+EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApiContrib.IoC.CastleWindsor", "src\WebApiContrib.IoC.CastleWindsor\WebApiContrib.IoC.CastleWindsor.csproj", "{3735BC1C-6224-417B-8816-D6BC6498682F}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApiContrib.IoC.StructureMap", "src\WebApiContrib.IoC.StructureMap\WebApiContrib.IoC.StructureMap.csproj", "{B295E165-A947-4027-8245-02AB24C7E840}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -90,6 +93,10 @@ Global
{3735BC1C-6224-417B-8816-D6BC6498682F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3735BC1C-6224-417B-8816-D6BC6498682F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3735BC1C-6224-417B-8816-D6BC6498682F}.Release|Any CPU.Build.0 = Release|Any CPU
+ {B295E165-A947-4027-8245-02AB24C7E840}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {B295E165-A947-4027-8245-02AB24C7E840}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {B295E165-A947-4027-8245-02AB24C7E840}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {B295E165-A947-4027-8245-02AB24C7E840}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -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("WebApiContrib.IoC.StructureMap")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("WebApiContrib.IoC.StructureMap")]
+[assembly: AssemblyCopyright("Copyright © 2012")]
+[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("cdaede1c-c0e2-4b35-bedf-4def04e8141b")]
+
+// 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")]
@@ -0,0 +1,40 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Web.Http.Controllers;
+using System.Web.Http.Dispatcher;
+using System.Web.Http.Services;
+using StructureMap;
+
+namespace WebApiContrib.IoC.StructureMap
+{
+ public class StructureMapResolver : IDependencyResolver, IHttpControllerActivator
+ {
+ private readonly IContainer container;
+
+ public StructureMapResolver(IContainer container)
+ {
+ if (container == null)
+ throw new ArgumentNullException("container");
+
+ this.container = container;
+
+ this.container.Inject(typeof(IHttpControllerActivator), this);
+ }
+
+ public object GetService(Type serviceType)
+ {
+ return container.TryGetInstance(serviceType);
+ }
+
+ public IEnumerable<object> GetServices(Type serviceType)
+ {
+ return container.GetAllInstances(serviceType).Cast<object>();
+ }
+
+ public IHttpController Create(HttpControllerContext controllerContext, Type controllerType)
+ {
+ return (IHttpController) container.GetInstance(controllerType);
+ }
+ }
+}
@@ -0,0 +1,79 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>8.0.30703</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{B295E165-A947-4027-8245-02AB24C7E840}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>WebApiContrib.IoC.StructureMap</RootNamespace>
+ <AssemblyName>WebApiContrib.IoC.StructureMap</AssemblyName>
+ <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\..\WebAPIContrib\</SolutionDir>
+ <RestorePackages>true</RestorePackages>
+ </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="StructureMap">
+ <HintPath>..\..\packages\structuremap.2.6.3\lib\StructureMap.dll</HintPath>
+ </Reference>
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Net.Http, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+ <Private>True</Private>
+ <HintPath>..\..\packages\System.Net.Http.2.0.20126.16343\lib\net40\System.Net.Http.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
+ <Private>True</Private>
+ <HintPath>..\..\packages\System.Net.Http.Formatting.4.0.20126.16343\lib\net40\System.Net.Http.Formatting.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Net.Http.WebRequest, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+ <Private>True</Private>
+ <HintPath>..\..\packages\System.Net.Http.2.0.20126.16343\lib\net40\System.Net.Http.WebRequest.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
+ <Private>True</Private>
+ <HintPath>..\..\packages\AspNetWebApi.Core.4.0.20126.16343\lib\net40\System.Web.Http.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Web.Http.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
+ <Private>True</Private>
+ <HintPath>..\..\packages\System.Web.Http.Common.4.0.20126.16343\lib\net40\System.Web.Http.Common.dll</HintPath>
+ </Reference>
+ <Reference Include="Microsoft.CSharp" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="StructureMapResolver.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="packages.config" />
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <Import Project="$(SolutionDir)\.nuget\nuget.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>
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+ <package id="AspNetWebApi.Core" version="4.0.20126.16343" />
+ <package id="structuremap" version="2.6.3" />
+ <package id="System.Net.Http" version="2.0.20126.16343" />
+ <package id="System.Net.Http.Formatting" version="4.0.20126.16343" />
+ <package id="System.Web.Http.Common" version="4.0.20126.16343" />
+</packages>
@@ -1,5 +1,9 @@
namespace WebApiContribTests.Helpers
{
+ public class FileContactRepository : IContactRepository
+ {
+ }
+
public class InMemoryContactRepository : IContactRepository
{
}
@@ -1,15 +1,21 @@
-using System.Net.Http;
+using System.Linq;
+using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Controllers;
+using System.Web.Http.Dispatcher;
using Autofac;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Microsoft.Practices.Unity;
using NUnit.Framework;
using Ninject;
+using Rhino.Mocks;
+using Should;
+using StructureMap;
using WebApiContrib.IoC.Autofac;
using WebApiContrib.IoC.CastleWindsor;
using WebApiContrib.IoC.Ninject;
+using WebApiContrib.IoC.StructureMap;
using WebApiContrib.IoC.Unity;
using WebApiContribTests.Helpers;
@@ -251,5 +257,66 @@ public void WindsorResolver_In_HttpConfig_DoesNot_Resolve_PipelineType_But_Fallb
Assert.IsNotNull(instance);
}
}
+
+ [Test]
+ public void StructureMapResolver_should_be_returned_for_IHttpControllerActivator_WithDefaultConventions()
+ {
+ // When building up a SM container, this is the only thing that doesn't get
+ // resolved "nicely" when using default conventions.
+ var container = new Container(x => x.Scan(s =>
+ {
+ s.TheCallingAssembly();
+ s.WithDefaultConventions();
+ }));
+ var config = new HttpConfiguration();
+ var resolver = new StructureMapResolver(container);
+
+ config.ServiceResolver.SetResolver(resolver);
+
+ var actualActivator = config.ServiceResolver.GetService(typeof (IHttpControllerActivator));
+
+ actualActivator.ShouldBeSameAs(resolver);
+ }
+
+ [Test]
+ public void StructureMapResolver_should_return_both_instaces_of_IContactRepository()
+ {
+ var container = new Container(x => x.Scan(s =>
+ {
+ s.TheCallingAssembly();
+ s.AddAllTypesOf<IContactRepository>().NameBy(t => t.Name);
+ }));
+ var config = new HttpConfiguration();
+ var resolver = new StructureMapResolver(container);
+
+ config.ServiceResolver.SetResolver(resolver);
+
+ var repositories = config.ServiceResolver.GetServices(typeof(IContactRepository));
+ repositories.Count().ShouldEqual(2);
+ }
+
+ [Test]
+ public void StructureMapResolver_should_return_an_empty_collection_if_type_isnt_found()
+ {
+ var config = new HttpConfiguration();
+ var resolver = new StructureMapResolver(new Container());
+
+ config.ServiceResolver.SetResolver(resolver);
+
+ var repositories = config.ServiceResolver.GetServices(typeof(IContactRepository));
+ repositories.Count().ShouldEqual(0);
+ }
+
+ [Test]
+ public void StructureMapResolver_Create_should_delegate_to_GetInstance()
+ {
+ var container = MockRepository.GenerateMock<StructureMap.IContainer>();
+ var resolver = new StructureMapResolver(container);
+ var controllerType = typeof (ContactsController);
+
+ resolver.Create(new HttpControllerContext(), controllerType);
+
+ container.AssertWasCalled(x => x.GetInstance(controllerType));
+ }
}
}
@@ -62,6 +62,9 @@
<Reference Include="Should">
<HintPath>..\..\packages\Should.1.1.12.0\lib\Should.dll</HintPath>
</Reference>
+ <Reference Include="StructureMap">
+ <HintPath>..\..\packages\structuremap.2.6.3\lib\StructureMap.dll</HintPath>
+ </Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Json">
@@ -148,6 +151,10 @@
<Project>{7AFAC6AB-9B5F-41B7-9AB7-84053FC19A7E}</Project>
<Name>WebApiContrib.IoC.Ninject</Name>
</ProjectReference>
+ <ProjectReference Include="..\..\src\WebApiContrib.IoC.StructureMap\WebApiContrib.IoC.StructureMap.csproj">
+ <Project>{B295E165-A947-4027-8245-02AB24C7E840}</Project>
+ <Name>WebApiContrib.IoC.StructureMap</Name>
+ </ProjectReference>
<ProjectReference Include="..\..\src\WebApiContrib.IoC.Unity\WebApiContrib.IoC.Unity.csproj">
<Project>{543DD4BC-C610-4381-82D9-2BA3686AEE0F}</Project>
<Name>WebApiContrib.IoC.Unity</Name>