I am new to using Ninject and applying Dependency Injection to my code. As a result I want to make sure I am not over complicating the solution or undermining the benefits of using Ninject and Dependency Injection.
The following code is a simplified conceptual example of what I am trying to accomplish. I am wondering if if there is a better way to to produce the same result or if I should look at modifying my design. I understand that an alternative solution would be to supply the context to IService and IProvider via method injection and use the WhenInjectedInto method but that would go against how I am trying to use the interface.
public interface IContext
{
}
public interface IProvider
{
IContext Context { get; }
}
public interface IService
{
IProvider Provider { get; }
}
public interface IBindingContext
{
IService Service { get; }
}
public CreateBindings()
{
this.kernel = new StandardKernel();
this.kernel.Bind<IContext>().To<GenericContext>();
this.kernel.Bind<IProvider>().To<GenericProvider>();
this.kernel.Bind<IService>().To<GenericService>();
// Do not want to create specific implementations
//this.kernel.Bind<IContext>().To<SpecificContext>().WhenInjectedInto<SpecificProvider>();
//this.kernel.Bind<IProvider>().To<SpecificProvider>().WhenInjectedInto<SpecificService>();
//this.kernel.Bind<IService>().To<SpecificService>().WhenInjectedExactlyInto<SpecificBindingContext>();
this.kernel.Bind<IContext>().To<SpecificContext>().When(x => this.IsUsedBy<SpecificBindingContext>(x));
}
private bool IsUsedBy<T>(IRequest request)
{
if (request == null)
return false;
if (request.Service == typeof(T))
return true;
if (this.IsUsedBy<T>(request.ParentRequest))
return true;
return false;
}
The actual interfaces that I am dealing with are:
public interface ILogDirectory
{
string FullPath { get; }
}
public interface ILogProvider
{
void Write(string text);
}
public interface ILogService
{
void Write(Exception exception);
void Write(string text);
void WriteDebug(string text);
}
The issue I have is that I don't want the client code to know how or where information is being logged. If I create Write methods that include a signature for ILogDirectory I would be exposing to many implementation details. At this point I can actually create the context specific log implementations pretty easily but the only thing that is unique for each implementation is the directory that contains the log files. As a result I would rather not have to create the context specific implementations for each interface.