Registering multiple objects with different service overrides

19 views
Skip to first unread message

Ryan

unread,
Jul 28, 2011, 10:48:19 AM7/28/11
to Castle Project Users
I need to register multiple concrete objects with a different service
injection. So for example

container.Register(Component.For(Of InterfaceA) _
.ImplementedBy(GetType(ConcreteA)) _
.ServiceOverrides(ServiceOverride.ForKey("dependency").Eq("DependencyConcreteA")))
container.Register(Component.For(Of InterfaceA) _
.ImplementedBy(GetType(ConcreteA)) _
.ServiceOverrides(ServiceOverride.ForKey("dependency").Eq("DependencyConcreteB")))
container.Register(Component.For(Of InterfaceA) _
.ImplementedBy(GetType(ConcreteA)) _
.ServiceOverrides(ServiceOverride.ForKey("dependency").Eq("DependencyConcreteC")))


So I've got 4 concrete implementations that are the same except for
what they are injected with. This is not working, when I get to the
second Register statement I get an error that "An item with the same
key has already been added.". Is there a way to do this?

Thanks,
Ryan

Ryan

unread,
Jul 28, 2011, 10:53:16 AM7/28/11
to Castle Project Users
Well so I just added .Named("X") and named them all differently and
this appears to work.

On Jul 28, 9:48 am, Ryan <rlangto...@gmail.com> wrote:
> I need to register multiple concrete objects with a different service
> injection.  So for example
>
>             container.Register(Component.For(Of InterfaceA) _
>                                .ImplementedBy(GetType(ConcreteA)) _
>                                .ServiceOverrides(ServiceOverride.ForKey("dependency").Eq("DependencyConcre­teA")))
>             container.Register(Component.For(Of InterfaceA) _
>                                .ImplementedBy(GetType(ConcreteA)) _
>                                .ServiceOverrides(ServiceOverride.ForKey("dependency").Eq("DependencyConcre­teB")))
>             container.Register(Component.For(Of InterfaceA) _
>                                .ImplementedBy(GetType(ConcreteA)) _
>                                .ServiceOverrides(ServiceOverride.ForKey("dependency").Eq("DependencyConcre­teC")))

Jason Meckley

unread,
Jul 28, 2011, 1:20:39 PM7/28/11
to castle-pro...@googlegroups.com
that would work. however, why not register one instance of A and one instance of dependency A, B & C. then resolve the dependency based on some dynamic factor. there are a number of ways to do this:
sub-dependency resolver
handler selector
factory method/facility

the life style wouldn't be a singleton either. most likely transient.
Reply all
Reply to author
Forward
0 new messages