Constructor injection by name

27 views
Skip to first unread message

Jordan

unread,
Sep 10, 2018, 10:18:33 AM9/10/18
to Castle Project Users
Hi all,

If there are 2 different named implementations of an interface in the container, will the first one be always be automatically selected by the container for constructor injection?

Is there any way of automatically injecting both instances into a constructor?  Or a dictionary of all implementations?

eg if there are 2 named implementations of ISomeService, "defaultSomeService", and "alternativeSomeService",  and I have a constructor like this:


        public SomeOtherComponent(ISomeService defaultSomeService, ISomeService alternativeSomeService) 
        {
                  //................
        }

is there any way of getting the container to automatically inject both implementations by name? Or will "defaultSomeService" just be injected twice ?

thanks,
Jordan


Sunny

unread,
Sep 10, 2018, 11:05:11 AM9/10/18
to castle-pro...@googlegroups.com
Did you try to iniect IEnumerable<ISomeService>? This will inject both in the order of registration.

Cheers,
Sunny

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-u...@googlegroups.com.
To post to this group, send email to castle-pro...@googlegroups.com.
Visit this group at https://groups.google.com/group/castle-project-users.
For more options, visit https://groups.google.com/d/optout.

Jordan

unread,
Sep 10, 2018, 11:24:16 AM9/10/18
to Castle Project Users
Ah no! I didn't try that.
Thankyou, that could be ok. 

I'd just switched to using ServiceOverride.ForKey which is working but doesn't give me the thrill of automatic registration :)

Jordan.


On Monday, September 10, 2018 at 4:05:11 PM UTC+1, Svetoslav Milenov - Sunny wrote:
Did you try to iniect IEnumerable<ISomeService>? This will inject both in the order of registration.

Cheers,
Sunny

On Mon, Sep 10, 2018, 9:18 AM 'Jordan' via Castle Project Users <castle-pro...@googlegroups.com> wrote:
Hi all,

If there are 2 different named implementations of an interface in the container, will the first one be always be automatically selected by the container for constructor injection?

Is there any way of automatically injecting both instances into a constructor?  Or a dictionary of all implementations?

eg if there are 2 named implementations of ISomeService, "defaultSomeService", and "alternativeSomeService",  and I have a constructor like this:


        public SomeOtherComponent(ISomeService defaultSomeService, ISomeService alternativeSomeService) 
        {
                  //................
        }

is there any way of getting the container to automatically inject both implementations by name? Or will "defaultSomeService" just be injected twice ?

thanks,
Jordan


--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-users+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages