How to convert one type to another when resolving a dependency in castle windsor

castle windsor c# dependency injection example
castle windsor register all types implementing interface
castle windsor usingfactorymethod
castle windsor dependson
castle windsor property injection
castle windsor resolve with parameters
castle windsor register class without interface
castle windsor resolve multiple implementations

I'm trying to implement a factory class to help me instantiate a class, FooBar.

FooBar looks like this:

public class BarConsumer
    BarConsumer(IEnumerable<Bar>, IWibble wibbles)

My Factory looks like this:

interface IBarConsumerFactory
    BarConsumer MakeBarConsumerFromFoo(Foo)

IWibble is satisfied by Castle.Windsor. My IEnumerable<Bar> can be derived from a single instance of Foo. I have a class, BarConsumerFactory that makes a BarConsumer from a Foo. Unfortunately though, to turn a Foo into a IEnumerable<Bar>, it needs some dependencies (IFillangie). All of those dependencies are registered with Castle.Windsor.

Currently, I have this:

interface IBarConsumerTypeFactory
    BarConsumer MakeBarConsumer(IEnumerable<Bar> bars)

class BarConsumerFactory : IBarConsumerFactory
       IFillangie fillangie, 
       IBarConsumerTypeFactory barConsumerTypeFactory)

    BarConsumer MakeBarConsumerFromFoo(Foo foo)
        //do some processing to create "processedFoo"

        return _barConsumerTypeFactory.MakeBarConsumer(

so IBarConsumerTypeFactory is registered with AsFactory(), and matches the constructor directly.

So, this works, but is very ugly.

Is there any way I can drop the IBarConsumerTypeFactory, and simply show Castle.Windsor how to convert a Foo into a IEnumerable<Bar> using BarConsumerFactory (which would then be called FooBarConverter I guess?)

I believe you can register your custom factory in the following way:


and completely remove the



This way consumers can still depend on IBarConsumerFactory but Castle Windsor will internally pass them BarConsumerFactory.

Getting Started with Dependency Injection Using Castle Windsor , IWibble is satisfied by Castle.Windsor. My IEnumerable<Bar> can be derived from a single instance of Foo . I have a class, BarConsumerFactory that makes a​  Castle Windsor Typed Factory: Resolving dependencies at runtime Posted on May 8, 2013 by DavidS — 1 Comment Learning about how to use IoC containers, correctly, is quite challenging and while I am still by no means an expert, I am getting a better grasp of how to do it the “proper” way.

refact your code about processing Foo into IEnumerable to a facility, and register it with windsor, the doc of facility is here

Chapter 10. Castle Windsor, This tutorial explains the basics of Castle Windsor, a mature IoC container. ImplementedBy<CompositionRoot>()); // Resolve an object of type In other cases, you may need to ensure that only one instance of a specific  After all, one of Windsor's main tasks is wiring and managing dependencies. When a component has some dependencies, Windsor goes through several steps in order to resolve them. Dependency Resolver. Windsor uses dependency resolver (type implementing IDependencyResolver interface) to resolve your

If you don't want to implement your abstract factory like described here, you can use a custom ITypedFactoryComponentSelector:

public class BarConsumerFactoryConverter : DefaultTypedFactoryComponentSelector
    private IFillangie _fillangie;

    public BarConsumerFactoryConverter(IFillangie fillangie)
        _fillangie = fillangie;

    protected override Arguments GetArguments(MethodInfo method, object[] arguments)
        if (arguments == null)
            return null;

        Arguments argumentMap = new Arguments();
        ParameterInfo[] parameters = method.GetParameters();

        for (int i = 0; i < parameters.Length; i++)
            if (parameters[i].ParameterType == typeof(Test))
                argumentMap.Add(typeof(IEnumerable<IBar>), _fillangie.FinalStep((Test) arguments[i]));
                argumentMap.Add(parameters[i].Name, arguments[i]);

        return argumentMap;

This BarConsumerFactoryConverter has to be registered with your IWindsorContainer as well:

container.Register(Component.For<BarConsumerFactoryConverter, ITypedFactoryComponentSelector>());

Finally you have to tell your factory to use BarConsumerFactoryConverter as its component selector:

container.Register(Component.For<IBarConsumerFactory>().AsFactory(f => f.SelectedWith<BarConsumerFactoryConverter>()));

Bricks and Mortar: Building a Castle, In addition to being one of the oldest DI CONTAINERS, Castle Windsor is one of the Stack Overflow ( is another good place to ask questions. is to resolve objects by wiring them up with all their DEPENDENCIES. nicugrodfe, yxb sns evsoerl oqr Sauce-Béarnaise type kr ord nz instance kl jr. The Castle Windsor library is very mature and highly-extensible, offering a host of features not covered in this specific tutorial. Keep an eye out for future tutorials where I'll cover: The Castle Windsor TypedFactoryFacility: Easily resolve types that have runtime dependencies! How (and why) to resolve multiple types mapped to the same interface

Understanding Dependency Injection and Those Pesky Containers, In this article, I will examine a popular IoC container, Castle Windsor, and how it Any of AccountsPayable's dependencies have been resolved and satisfied before one IoC container to another and are dependent on this override behavior. A component implements a service (aka interface or abstract base class) and is  Windsor provides a few resolvers that you can plug into the container out of the box. CollectionResolver. ℹ️ This is a new type in Windsor 2.5. The CollectionResolver is used to satisfy dependency on a collection of components, for example: ℹ️ Supported types: There are many collection types that CollectionResolver supports.

Resolve<> cannot pick parameter-less constructor · Issue #364 , Consulting Home · Services & Technologies · Legacy Conversion and DI is an approach to providing one type with instances of the other types on which Other popular containers include Castle Windsor, MEF, NInject, StructureMap, and Spring. Because this class will be resolved and dependencies injected into them  An unhandled exception of type 'Castle.MicroKernel.Handlers.HandlerException' occurred in Castle.Windsor.dll Additional information: Can't create component 'failnpcinterceptor.MyTransient' as it has dependencies to be satisfied.

Windsor/ at master, It has a parameter-less constructor, and another two constructor with different parameters. Castle.MicroKernel.IKernelInternal.Resolve(Type service, IDictionary clause has multiple constructors, one of which has unsatisfied dependency. Castle Windsor is a best of breed, mature Inversion of Control container available for .NET - castleproject/Windsor