Providing service overrides based on type rather than parameter name

91 views
Skip to first unread message

Alexander Groß

unread,
Jan 18, 2010, 10:22:08 AM1/18/10
to Castle Project Users
Hi all,

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

Krzysztof Koźmic

unread,
Jan 18, 2010, 11:15:38 AM1/18/10
to castle-pro...@googlegroups.com
I guess you could use IHandlerSelector for that

alwin

unread,
Jan 18, 2010, 7:03:34 PM1/18/10
to Castle Project Users
I've made something like you described, I'll try to put in in the
wiki.

It basically looks like this:

container.Register(
Component.For<IConsumer>().ImplementedBy<ConsumerWithProp>()
.ServiceOverridesTyped(ServiceOverrideTyped.For<IService>().Eq
("second"))
);

Alexander Groß

unread,
Jan 18, 2010, 7:23:06 PM1/18/10
to Castle Project Users
Alwin, that would be great!

Thanks,
Alex

alwin

unread,
Jan 18, 2010, 8:30:17 PM1/18/10
to Castle Project Users
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/TypedDependencies.zip

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

Alexander Groß

unread,
Jan 28, 2010, 6:48:47 PM1/28/10
to Castle Project Users
Alwin,

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...

Reply all
Reply to author
Forward
0 new messages