I'm currently trying to provide a service override based on type
rather than the parameter/property name.
Classic registration:
Component.For<Foo>
.ServiceOverrides(ServiceOverride.ForKey("paramName").Eq
("dependency"))
What I want to achieve:
Component.For<Foo>
.ServiceOverrides(ServiceOverride.ForType<IDependency>().Eq
("dependency"))
Is this possible with Castle Windsor?
If not, are there suggestions how to add that behavior? Is a facility
combined with other infrastructure (which types?) that processes
ServiceOverride.ForKey("fully-qualified type name").Eq("...") to
provide the dependency at construction time a valid approach?
Thanks!
Alex
It basically looks like this:
container.Register(
Component.For<IConsumer>().ImplementedBy<ConsumerWithProp>()
.ServiceOverridesTyped(ServiceOverrideTyped.For<IService>().Eq
("second"))
);
Thanks,
Alex
container.Register(
Component.For<IConsumer>().ImplementedBy<Consumer1Ctor2DifferentArg>
()
.ServiceOverridesTyped(ServiceOverrideTyped.For<IService>().Eq
("second"))
.DependsOnTyped(ParameterTyped.For<bool>().Eq(true))
);
You can look at the TypedConfigTests file to see how to use it.
It works for service overrides and parameters (DependsOn).
You can specify to look for a ctor argument or property, or both (the
default is both).
Basically, the code looks at the implementation type with reflection,
to find a property or a constructor argument that matches the type.
Then it builds a normal string-based descriptor out of that. It is all
static reflection without hooking into the kernel (I didn't know how
to do that when I wrote this code a long time ago).
I use this together with
http://using.castleproject.org/display/IoC/Strongly+Typed+property+wiring+with+Fluent+Registration+API
to use an IOC in a obfuscated project. It's hard to use parameter
names, when every name becomes (!*#>!@{'']\
Wiki page may come later...
Hope this helps
Thank you for posting the code. Very insightful!
Alex
On Jan 19, 2:30 am, alwin <alw...@gmail.com> wrote:
> Ok I managed to pull the needed code out of the project it is used,
> the code is here:http://groups.google.com/group/castle-project-users/web/TypedDependen...
>
> container.Register(
> Component.For<IConsumer>().ImplementedBy<Consumer1Ctor2DifferentArg>
> ()
> .ServiceOverridesTyped(ServiceOverrideTyped.For<IService>().Eq
> ("second"))
> .DependsOnTyped(ParameterTyped.For<bool>().Eq(true))
> );
>
> You can look at the TypedConfigTests file to see how to use it.
> It works for service overrides and parameters (DependsOn).
> You can specify to look for a ctor argument or property, or both (the
> default is both).
>
> Basically, the code looks at the implementation type with reflection,
> to find a property or a constructor argument that matches the type.
> Then it builds a normal string-based descriptor out of that. It is all
> static reflection without hooking into the kernel (I didn't know how
> to do that when I wrote this code a long time ago).
>
> I use this together withhttp://using.castleproject.org/display/IoC/Strongly+Typed+property+wi...