rauts
rauts

Reputation: 1018

Unity Container Config question

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

Upvotes: 2

Views: 1242

Answers (2)

Jeff Fritz
Jeff Fritz

Reputation: 9861

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.

Upvotes: 3

Lee
Lee

Reputation: 144136

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);

Upvotes: 1

Related Questions