Skip to content

Instantly share code, notes, and snippets.

@kenwarner
Created July 11, 2014 02:22
Show Gist options
  • Save kenwarner/4d7429f13a958881081d to your computer and use it in GitHub Desktop.
Save kenwarner/4d7429f13a958881081d to your computer and use it in GitHub Desktop.
Is null a valid value for a dependency from a factory method?
namespace ConsoleApplication1
{
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Castle.Windsor.Installer;
public interface IFoo
{
}
public class Foo : IFoo
{
public static IFoo CreateFoo()
{
return null;
}
}
public interface IBar
{
}
public class Bar : IBar
{
private readonly IFoo foo;
public Bar(IFoo foo = null)
{
this.foo = foo;
}
}
class Program
{
private static IWindsorContainer _container;
static void Main(string[] args)
{
_container = new WindsorContainer().Install(FromAssembly.This());
_container.Register(Component.For<IFoo>().UsingFactoryMethod(Foo.CreateFoo));
_container.Register(Component.For<IBar>().ImplementedBy<Bar>());
var service = _container.Resolve<IBar>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment