DI with Unity when multiple instances of the same type is needed

前端 未结 6 1626
忘了有多久
忘了有多久 2021-01-02 09:12

I need help with this. I\'m using Unity as my container and I want to inject two different instances of the same type into my constructor.

class Example
{
           


        
6条回答
  •  天涯浪人
    2021-01-02 09:31

    Well, don't

    You should use the factory pattern in this case.

    class Example
    {
       Example(IQueueFactory factory) 
       {
           _sendQueue = factory.Create("MySend");
           _receiveQueue = factory.Create("MyReceive");
       }
    }
    

    It makes the intention a lot more clear and you can internally in the Example class handle if the queues are not found or incorrectly configured.

提交回复
热议问题