views:

154

answers:

1

A Castle Windsor question: Is it possible to register a class that has an internal constructor with the container?

Thanks Joni

+3  A: 

Yes, it is possible. Default component activator looks only for public constructors. You can either provide custom component activator for that component that would take internal constructor into account, or use for example factory to activate the component:

var container = new WindsorContainer()
    .AddFacility<FactorySupportFacility>()
    .Register( Component.For<Foo>()
                   .UsingFactoryMethod( () => new Foo() ) );
var foo = container.Resolve<Foo>();

However you should really reconsider making the .ctor internal in the first place. It is really rarely a good idea to do so, especially when you're exposing the class as a component to the outside world anyway.

Krzysztof Koźmic
Hi Krzystzof, I thought the "and how is it done if it is?" was implicit to the question!I guess not. So:How do you do it?CheersJoni
joniba
Thanks Ian. So in effect I have to create a factory to build these components using reflection. Bit of a shame, since Castle Windsor is already using reflection for its component registrations. But I understand the reasoning.Thanks again.
joniba
+1 for suggesting reconsideration of the design
Mark Seemann

related questions