Created
July 11, 2014 02:22
-
-
Save kenwarner/4d7429f13a958881081d to your computer and use it in GitHub Desktop.
Is null a valid value for a dependency from a factory method?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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