jeudi 12 mars 2015

Ways of loading external dll and injecting conditionally multiple implementations using autofac

i have an interface called say IMyHook which has some implementation in an external project(dll). i use this interface in my businesslogic classes and on my application startup i inject external implementation of this interface using Autofac.


my interface



public interface IMyHook {
public void MyHookMethod();
}


my business logic class



public class myBusinessLogic{

// implementation injected by autofac
public IMyHook Hook {set;get;}

public void MyBusinessLogicMethod (flag){

if(Hook !=null){
Hook.MyHookMethod();
}
else{
// other code
}
}

}


auto face properties injection



var hoodAssemblyPath = "C:\\hook.dll";
builder.RegisterAssemblyTypes(hoodAssemblyPath).
AsImplementedInterfaces().PropertiesAutowired();


this works all fine but now issue is that my external dll (hook.dll) can have more then one implementations of IMyHook. And i want to decide which implementation to load in my MyBusinessLogicMethod method using the input paramameter flag. e.g


if (flag ==1) then load implementation 1 else if (flag == 2) then load implememtation 2 etc


i can do this using Reflections (and a custom attribute in my external classes representing each value of flag) by loading assembly and then the appropriate class using custom attribute on it.


But my question is; is this the right way loading external dll on each method call using reflection etc or are there any other ways of doing this? using autofac or anything else?


with all this i want to let users of my application to inject their code in my application for some of the functionality. so external dll or implementations will basically be written by my users and then my application will just load and execute the methods.






Aucun commentaire:

Enregistrer un commentaire