Skip to content

Instantly share code, notes, and snippets.

@GraemeF
Created November 11, 2011 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GraemeF/1357829 to your computer and use it in GitHub Desktop.
Save GraemeF/1357829 to your computer and use it in GitHub Desktop.
Test to show that the DefaultTypedFactoryComponentSelector doesn't match parameters on dependencies of the returned type.
namespace TypedFactoryTests
{
#region Using Directives
using Castle.Facilities.TypedFactory;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Should.Fluent;
using Xunit;
#endregion
public interface IFooFactory
{
IFoo CreateFoo(IBaz baz);
}
public interface IBaz
{
}
public class MyBaz : IBaz
{
}
public interface IFoo
{
IBar Bar { get; set; }
}
public class MyFoo : IFoo
{
public MyFoo(IBar bar)
{
Bar = bar;
}
public IBar Bar { get; set; }
}
public interface IBar
{
IBaz Baz { get; set; }
}
public class MyBar : IBar
{
public MyBar(IBaz baz)
{
Baz = baz;
}
public IBaz Baz { get; set; }
}
public class Test
{
private readonly IWindsorContainer container = new WindsorContainer();
public Test()
{
container.AddFacility<TypedFactoryFacility>();
container.Register(Component.For<IFoo>().ImplementedBy<MyFoo>());
container.Register(Component.For<IBar>().ImplementedBy<MyBar>());
container.Register(Component.For<IBaz>().ImplementedBy<MyBaz>());
container.Register(Component.For<IFooFactory>().AsFactory());
}
[Fact]
public void FactoryResolvesDependenciesWithParameters()
{
var theBaz = new MyBaz();
IFoo factoryFoo = container.Resolve<IFooFactory>().CreateFoo(theBaz);
factoryFoo.Bar.Baz.Should().Be.SameAs(theBaz);
}
[Fact]
public void ResolvesDependenciesWithoutParameters()
{
var theBaz = new MyBaz();
var foo = container.Resolve<IFoo>();
foo.Bar.Baz.Should().Not.Be.SameAs(theBaz);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment