Search code examples
c#.netdesign-patternsdecouplingdynamic-loading

how to properly decouple implementation specifics into class libraries in .net


I'm wondering if there is a standard way in c#(or maybe even cli) to efficiently decouple implementation logic into separate class libraries/assemblies that would be dynamically loaded by a process that will preforms actions on those libraries based on a common interface.

More precisely: Say I'm building a service that receives messages, and delegates handling of these messages to something else. Something like:

while(true){
   message = read_message_from_somewhere();
   class_from_another_lib.do_somthing_with_Message(message); 
}

I want the process to load a class lib in runtime from some sort of config. The lib, I assume, would have some main class or class factory that implements some interface part of which looks like this:

ISomePublicIface{
   void do_somthing_with_Message(messageT);
}

This all feels somewhat java beenish where all I would need to do is have the a class in an assembly implement some sort of injectable interface add a line to the app config and get abunch of stuff for free.


Solution

  • Take a look at the Managed Extensibility Framework (MEF), which is included in .NET 4.0. MEF can dynamically load and inject components from external DLLs.