locked
How do I choose the implementation of a type been registered with Microsoft Unity RRS feed

  • Question

  • User-772854600 posted

    Say I have an interface named IPumaServices, and I have two classes that implement it: POSiXmlServices and TaXmlServices.

    Now I have another interface called IPumaNotification and the class implementing it is called PumaNotification. The constructor of PumaNotification receives IPumaServices implementation.

    My question: In Unity, how can I register an implementation of PumaNotification that injects POSiXmlServices in the contructor and another that injects TaXmlServices?

    this is what I have so far.

    using (_unityContainer = new UnityContainer())
                {
                  _unityContainer
                  .RegisterType<IPumaServices, POSiXmlServices>("POSiXml")
                  .RegisterType<IPumaServices, TaXmlServices>("TaXml")
                  .RegisterType<IPumaNotification, PumaNotification>();
                }


    I have not idea how to make it work with the requirements I have above.

    I was not able to research online for this issue as I am not sure how describe the issue I am facing.

    I appreciate any help.

    Monday, June 4, 2012 2:26 PM

All replies

  • User-1916342745 posted

    Hi there, 

    I had a similar problem the other day, where I needed to implement the same methods on different classes on different types of files. The files matched different patterns. I didn't need to worry about the lifetime feature available in Unity, so this may be of no use to you. I ended up going down a different route (Reflection - not Ioc).

    My main processing method became:

    private static void processFile(string filetype, string filepath)
    {
    Type t = Type.GetType(filetype);
    IFileHandler fh = (IFileHandler) Activator.CreateInstance(t, new string[] { });
    fh.FilePath = filepath;
    fh.ReadFile();
    fh.ExportFile();

    }

    In order to make this work, I created a simple extention in the config file, so I could add: 

    <configSections>
    <section name="FileHandlerSection" type="FileHandlerTest.FileHandlerConfigSection,FileHandlerTest"/>
    </configSections>

    <FileHandlerSection>
    <FileHandlers>
    <add pattern="B" fileHandlerType="FileHandler1.FileType1Handler, FileHandler1" sortOrder="1" />
    <add pattern="A" fileHandlerType="FileHandler2.FileType2Handler, FileHandler2" sortOrder="1" />
    </FileHandlers>
    </FileHandlerSection>

    This gave me the ability to add new handler sections without recompiling or changing any of the main code. For me, I needed to add the custom FileHandler so I could create a matching pattern. 

    Sorry, this doesn't really answer your question, but may give an alternative solution (albeit an ugly one). 

    Wednesday, August 29, 2012 11:13 AM
  • User-1916342745 posted

    Hi, 

    You may also find this one useful:

    http://msdn.microsoft.com/en-us/library/ff664762(v=pandp.50).aspx

    Wednesday, August 29, 2012 11:15 AM
  • User-772854600 posted

    I found what I was looking for.  I needed this.

    _unityContainer.RegisterType<IPumaServices, POSiXmlServices>("POSiXml")
                                    .RegisterType<IPumaServices, TaXmlServices>("TaXml")
                                    .RegisterType<IPumaNotification, PumaNotification>("TaXml", new InjectionConstructor(
                                                                                           new ResolvedParameter<INotificationServices>(),
                                                                                           new ResolvedParameter<IPumaServices>("TaXml")))
                                                                                           .RegisterType<IPumaNotification, PumaNotification>("POSiXml",
                                                                                           new InjectionConstructor(
                                                                                           new ResolvedParameter<INotificationServices>(),
                                                                                           new ResolvedParameter<IPumaServices>("POSiXml")));





    Thursday, August 30, 2012 8:31 PM