possible GetObjectsOfType replacement

前端 未结 4 597
-上瘾入骨i
-上瘾入骨i 2020-12-12 03:08

I have this small piece of code

var idObjects = Spring.Context.Support.ContextRegistry.GetContext()
                      .GetObjectsOfType(typeof (ICustomIn         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-12 03:48

    The interesting part of your question to me is: how to use service locator functions (such as spring.net's IApplicationContext.GetObjectsOfType(...)) from within your library, without introducing a dependency on a specific IoC container.

    As stated in the question, this is useful, because we want to build libraries that do not force the consumer to use a specific IoC container. However, we still want to use an IoC container, because it eases the development of our library. This dilemma is nicely described by Jeremy Miller in the post "It’s time for IoC Container Detente".

    His blog post has led to a small project on codeplex called CommonServiceLocator. This project specifies an IServiceLocator interface for service location, that is implemented by many popular IoC containers, including Spring.NET.

    The IServiceLocator defines a IEnumerable GetAllInstances(); method, which basically is what is asked for in your question.

    When your library needs service locator functionality, you can take a dependency on the CommonServiceLocator library and you and other consumers can wire it up using your IoC container of choice.

    And guess what: Spring.NET adapter implements IEnumerable GetAllInstances(); using GetObjectsOfType(serviceType);.

提交回复
热议问题