Is there akin to Java's java.util.ServiceLoader, which allows components to be looked up and loaded up dynamically through an SPI?

I'm looking for some lightweight native feature of .NET that allows me to easily plug-in providers of interfaces, rather than bulky DI containers or having to write it myself.

Update: This does not appear to be readily available on .NET, as there's no SPI feature natively build-in like in Java. I accepted the first answer since it's the closest to what I am after.

有帮助吗?

解决方案

There is a native IServiceProvider interface.

That said, there are tons of very rich IoC containers available for .NET

  • Castle Windsor
  • Unity
  • AutoFac
  • NInject

to name a few. Many of these are pretty lightweight. They also offer advanced features such as object lifetime management (singleton, per thread, etc.) and AOP (aspect oriented programming) features.

That said, if you really want lightweight and no dependencies, you could roll your own in about 30 lines of code.

其他提示

As @JeffN825 said, these is no native container for .NET.

That said, what do you mean by bulky? Maybe you refer to the main downside of the Service Locator pattern: you have references to the container everywhere and it is pretty invasive.

With the help of the Composition Root pattern you can relegate use of the container to one place in your application, reducing the impact of using a container. This way you eliminate the "bulk": you configure it once, use it once and forget about it.


UPDATE

Answering your comments: lots of IoC Containers have some sort of autoregistration features that may satisfy your needs. For example, here is a link to the relevant structuremap documentation page.

You can then let your IoC Container pick different assemblies with different configurations of your implementations - i.e. stubs instead of real implementations.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top