As you know, Ninject binds all interfaces to implementations in the composition root. We have a class which is dependent on an external dll, but we don't want to deploy it (the physical dll file in the bin directory) if it's not being used. The class ExampleClass
is stored in our Framework project, this means it's referenced by any application we are building. And that's why we are getting the following when deploying any of our application:
Could not load file or assembly ExternalDll, Version=x.x.x.xxx, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx
Is there any workaround, like encapsulating the implementation into another class or something? Or runtime initialisation (Factory pattern). Which of these will solve my problem? I also tried Lazy binding but without success.
Constructor injection of implementation where the external dll is being used
namespace Framework
{
public class ExampleClass
{
private readonly IUsesExternalDll _usesExternalDll;
public ExampleClass(IUsesExternalDll usesExternalDll)
{
_usesExternalDll = usesExternalDll;
}
}
}
Interface
public interface IUsesExternalDll
{
}
Implementation
using externalDll; //this using is a reference to external dll
public class UsesExternalDll : IUsesExternalDll
{
}
Binding
kernel.Bind<IUsesExternalDll>().To<UsesExternalDll>().InTransientScope();