tags:

views:

262

answers:

2

Hi All,

I am using Unity DI container. In the config file I specify the following type as :

<type type="Interfaces.ILogger,Interfaces" 
 mapTo = "ConcreateClasses.ConsoleLogger,ConcreateClasses" />

My understanding is that both the Interfaces dll and ConcreteClasses dll should be referenced in my project in order for this to work.

But What I want to do is not to reference the concrete implementation Classes at design time. I would like them to be loaded at runtime by specifying the path of the ConcreteClasses dll.

Is there a way to do this?

Thanks

+1  A: 

You could do it through reflection:

Assembly a = Assembly.LoadFrom("pathToDll");
Type interfaceType = typeof(Interfaces.ILogger);
Type implementingType = a.GetTypes.Where(t => t.IsAssignableTo(interfaceType)).First(); //add any other constraints to decide mapping

container.RegisterType(interfaceType, implementingType);
Lee
Thanks for the answer. So is there no way to do this via Config file?
rauts
+3  A: 

You don't need to reference the concrete implementation assembly in your project, you only need to have it in the same folder as your config file, or available from the GAC.

It's CONVENIENT to reference the other assembly with the concrete implementation, so that Visual Studio will place a copy of the DLL in the resultant BIN folder of your project, thus making the lookup trivial.

Jeff Fritz
I am sorry but the concrete implementation assembly has be either reference or in GAC. Placing it in same folder as config file doesn't help. Anyways thanks for the answer
rauts
I have a rather large implementation sitting on the EC2 right now that has NO references to concrete objects, and all of my objects are referenced by type in a Unity config section.I don't know what problems you are having, but this configuration runs fine for me.
Jeff Fritz
Thats interesting. Are you deploying all the config file and Dll in the same folder because thats what i am doing right now and its not working. Can you please share with me your deployment strategy?
rauts