I have this small piece of code
var idObjects = Spring.Context.Support.ContextRegistry.GetContext()
.GetObjectsOfType(typeof (ICustomIn
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
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
using GetObjectsOfType(serviceType);
.