[ACCEPTED]-Castle Windsor: How do I register a factory method, when the underlying type isn't accessible to my assembly?-castle-windsor

Accepted answer
Score: 16

Using Windsor 2.0:

public class WindsorTests {
    public interface IDataService {}

    public class DataService: IDataService {}

    public interface IDataFactory {
        IDataService Service { get; }

    public class DataFactory: IDataFactory {
        public IDataService Service {
            get { return new DataService(); }

    public void FactoryTest() {
        var container = new WindsorContainer();
        container.AddComponent<IDataFactory, DataFactory>();
        container.Register(Component.For<IDataService>().UsingFactory((IDataFactory f) => f.Service));
        var service = container.Resolve<IDataService>();
        Assert.IsInstanceOfType(typeof(DataService), service);

See the fluent API wiki page for more information.


Score: 0

Got it. The answer is, you don't need to 4 worry about the implementation type:

    IWindsorContainer container = new WindsorContainer();
    container.AddFacility("factories", new FactorySupportFacility());
    container.AddComponent("standard.interceptor", typeof(StandardInterceptor));
    container.AddComponent("factory", typeof(DataFactory));

    MutableConfiguration config = new MutableConfiguration("dataService");
    config.Attributes["factoryId"] = "factory";
    config.Attributes["factoryCreate"] = "get_Service";
    container.Kernel.ConfigurationStore.AddComponentConfiguration("dataService", config);
    container.Kernel.AddComponent("dataService", typeof(IDataService));

    IDataService dataService = (IDataService) container["dataService"];

I had 3 a "whoa" moment when I saw it execute successfully, because 2 I hadn't passed in the specific implementation 1 type to Kernel.AddComponent()

More Related questions