Maciorus
Maciorus

Reputation: 128

Initializing components using Castle Windsor which have a dictionary of dependencies as a dependency

I am using Castle Windsor 3.3.0. I need to create a registration for a component which looks like this:

public class Foo : IFoo
{
 public Foo(Dictionary<string, IBar> allMyBars)
 {...}
}

I was trying to use DynamicParameters which got me:

Component.For<IFoo>()
.ImplementedBy<Foo>()
.Named("MyFoo")
.DynamicParameters(
 (k, d) =>
     d["allMyBars"] = new Dictionary<string, IBar>
                        {
                            {
                                "Bar1",
                                k.Resolve<IBar>("CreepyBar")
                            },
                            {
                                "Bar2",
                                k.Resolve<IBar>("MegaBar")
                            }
                        }); 

But that doesn't seem to work.

I am at my wits end. Could someone please help?

Thank you,

Upvotes: 0

Views: 858

Answers (1)

Old Fox
Old Fox

Reputation: 8725

You forgot to register the components into a container

Example for fix:

        var container = new WindsorContainer();

        container.Register(Component.For<IBar>().ImplementedBy<Bar>().Named("CreepyBar"));
        container.Register(Component.For<IBar>().ImplementedBy<Bar>().Named("MegaBar"));

        container.Register(Component.For<IFoo>()
            .ImplementedBy<Foo>()
            .Named("MyFoo")
            .DynamicParameters(
                (k, d) =>
                    d["allMyBars"] = new Dictionary<string, IBar>
                    {
                        {
                            "Bar1",
                            k.Resolve<IBar>("CreepyBar")
                        },
                        {
                            "Bar2",
                            k.Resolve<IBar>("MegaBar")
                        }
                    }));

        var verifyFoo= container.Resolve<IFoo>(); //I checked for 

Hint: you have to use the same container

Upvotes: 2

Related Questions