tags:

views:

51

answers:

1

I have been giving the task to rewrite a internal utility in .Net for my work. One of program requirements the new system has is have a DLL that implements a set of interfaces and have the program call the DLL.

Now this DLL will be changed out a lot per deployment. My question is what is the best way to do it from a development standpoint? Do I add a template DLL (one that only has the interfaces but no implementation) to the project references like I would do any other DLL that I would use?

Or do I need to use something like this every time I want to use code from the DLL?

var DropIn = System.Reflection.Assembly.LoadFrom("DropInDll.dll");
var getActions = DropIn.GetType("Main").GetMethod("GetActions");
List<IAction> ActionList = (List<IAction>)getActions.Invoke(null, null);
+2  A: 

Use Dependency Injection with an Inversion of Control Container.

Since you are already coding to defined interfaces, you shouldn't need any reflection.

Here's an example using the Common Service Locator from CodePlex, specifically the Simple Service Locator implementation.

Say you have a IDropIn interface, and different DLLs that implement the interface.

First, you need to register your interface with the system:

void Init()
{
    // Read dropInDllName and dropInClassName from your config file
    Assembly assembly = Assembly.Load(dropInDllName);
    IDropIn dropIn = (IDropIn)assembly.CreateInstance(dropInClassName);

    SimpleServiceLocator container = new SimpleServiceLocator();
    container.RegisterSingle<IDropIn>(dropIn);

    Microsoft.Practices.ServiceLocation.ServiceLocator.SetLocatorProvider(() => container);
}

Then, to get an instance in your code somewhere else, do this:

IDropIn dropIn = ServiceLocator.Current.GetInstance<IDropIn>();
List<IAction> actionList = dropIn.GetActions();
GalacticJello