Skip to content
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
9 changes: 9 additions & 0 deletions src/WebApiContrib.IoC.CastleWindsor/DependencyAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace WebApiContrib.IoC.CastleWindsor
{
using System;

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class DependencyAttribute : Attribute
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@
<ItemGroup>
<Reference Include="Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Castle.Core.3.3.1\lib\net40-client\Castle.Core.dll</HintPath>
<HintPath>..\..\packages\Castle.Core.3.3.1\lib\net45\Castle.Core.dll</HintPath>
</Reference>
<Reference Include="Castle.Windsor, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Castle.Windsor.3.3.0\lib\net40\Castle.Windsor.dll</HintPath>
<HintPath>..\..\packages\Castle.Windsor.3.3.0\lib\net45\Castle.Windsor.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Newtonsoft.Json.6.0.6\lib\net40\Newtonsoft.Json.dll</HintPath>
<HintPath>..\..\packages\Newtonsoft.Json.6.0.6\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -63,6 +63,9 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="DependencyAttribute.cs" />
<Compile Include="WindsorActionDescriptorFilterProvider.cs" />
<Compile Include="WindsorExtensions.cs" />
<Compile Include="WindsorResolver.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace WebApiContrib.IoC.CastleWindsor
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using Castle.Windsor;

public class WindsorActionDescriptorFilterProvider : ActionDescriptorFilterProvider, IFilterProvider
{
private readonly IWindsorContainer container;

public WindsorActionDescriptorFilterProvider(IWindsorContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}

this.container = container;
}

public new IEnumerable<FilterInfo> GetFilters(HttpConfiguration configuration, HttpActionDescriptor actionDescriptor)
{
var filters = base.GetFilters(configuration, actionDescriptor);

return filters.Select(filter => this.container.BuildUp(filter));
}
}
}
36 changes: 36 additions & 0 deletions src/WebApiContrib.IoC.CastleWindsor/WindsorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace WebApiContrib.IoC.CastleWindsor
{
using System;
using System.Linq;
using Castle.Windsor;

public static class WindsorExtensions
{
public static T BuildUp<T>(this IWindsorContainer container, T instance) where T : class
{
if (instance == null)
{
return null;
}

var properties =
instance.GetType().GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(DependencyAttribute)));

foreach (var property in properties)
{
var value = property.GetValue(instance, null);

if (value != null)
{
continue;
}

var resolvedPropertyInstance = container.Resolve(property.PropertyType);

property.SetValue(instance, resolvedPropertyInstance);
}

return instance;
}
}
}
6 changes: 3 additions & 3 deletions src/WebApiContrib.IoC.CastleWindsor/packages.config
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Castle.Core" version="3.3.1" targetFramework="net40" requireReinstallation="True" />
<package id="Castle.Windsor" version="3.3.0" targetFramework="net40" requireReinstallation="True" />
<package id="Castle.Core" version="3.3.1" targetFramework="net45" />
<package id="Castle.Windsor" version="3.3.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.2" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.2" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.6" targetFramework="net40" requireReinstallation="True" />
<package id="Newtonsoft.Json" version="6.0.6" targetFramework="net45" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@
<ItemGroup>
<Reference Include="Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Castle.Core.3.3.1\lib\net40-client\Castle.Core.dll</HintPath>
<HintPath>..\..\packages\Castle.Core.3.3.1\lib\net45\Castle.Core.dll</HintPath>
</Reference>
<Reference Include="Castle.Windsor, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Castle.Windsor.3.3.0\lib\net40\Castle.Windsor.dll</HintPath>
<HintPath>..\..\packages\Castle.Windsor.3.3.0\lib\net45\Castle.Windsor.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Newtonsoft.Json.6.0.6\lib\net40\Newtonsoft.Json.dll</HintPath>
<HintPath>..\..\packages\Newtonsoft.Json.6.0.6\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="nunit.framework, Version=2.6.3.13283, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down Expand Up @@ -80,6 +80,7 @@
<Compile Include="Helpers\InMemoryContactRepository.cs" />
<Compile Include="Helpers\PrecannedMessageHandler.cs" />
<Compile Include="DependencyInjectionTests.cs" />
<Compile Include="WindsorContainerBuildUpExtensionTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
namespace WebApiContrib.IoC.CastleWindsor.Tests
{
using Castle.MicroKernel.Registration;
using Castle.Windsor;

using NUnit.Framework;

[TestFixture]
public class WindsorContainerBuildUpExtensionTests
{
private interface IPerson
{
}

[Test]
public void Should_Inject_Dependency()
{
var container = new WindsorContainer();
container.Register(Component.For<IPerson>().ImplementedBy<PersonA>());

var instance = new TestClass();

container.BuildUp(instance);

Assert.IsInstanceOf<PersonA>(instance.Person);
}

[Test]
public void Should_Not_Inject_Dependency_When_Injected_Property_Is_Instantiated()
{
var container = new WindsorContainer();
container.Register(Component.For<IPerson>().ImplementedBy<PersonA>());

var instance = new TestClass() { Person = new PersonB() };

container.BuildUp(instance);

Assert.IsInstanceOf<PersonB>(instance.Person);
}

[Test]
public void Should_Not_Inject_Dependency_When_Property_Is_Not_Decorated_With_Dependency_Attribute()
{
var container = new WindsorContainer();
container.Register(Component.For<IPerson>().ImplementedBy<PersonA>());

var instance = new TestClass() { Person = new PersonB() };

container.BuildUp(instance);

Assert.IsNull(instance.AnotherPerson);
}

[Test]
public void Should_Return_Null_When_Type_Is_Not_Instantiated()
{
var container = new WindsorContainer();
container.Register(Component.For<IPerson>().ImplementedBy<PersonA>());

TestClass instance = null;

Assert.IsNull(container.BuildUp(instance));
}

private class TestClass
{
public IPerson AnotherPerson { get; set; }

[Dependency]
public IPerson Person { get; set; }
}

private class PersonA : IPerson
{
}

private class PersonB : IPerson
{
}
}
}
8 changes: 4 additions & 4 deletions test/WebApiContrib.IoC.CastleWindsor.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Castle.Core" version="3.3.1" targetFramework="net40" requireReinstallation="True" />
<package id="Castle.Windsor" version="3.3.0" targetFramework="net40" requireReinstallation="True" />
<package id="Castle.Core" version="3.3.1" targetFramework="net45" />
<package id="Castle.Windsor" version="3.3.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.2" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.2" targetFramework="net45" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" requireReinstallation="True" />
<package id="Newtonsoft.Json" version="6.0.6" targetFramework="net40" requireReinstallation="True" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.6" targetFramework="net45" />
<package id="NUnit" version="2.6.3" targetFramework="net40" />
<package id="NUnit.Runners" version="2.6.0.12051" />
</packages>