I'm using Ninject in a .NET project to handle dependency injection.
I have divided my solution into multiple projects :
- Business Logic
- FrontEnd
- ViewModels
They have carefully selected references :
- FrontEnd has a reference to ViewModels
- ViewModels has a reference to BusinessLogic
It seems common to initialize the IoC container in the entrypoint of the application (in my case this is the FrontEnd).
But FrontEnd doesn't have a reference to Business Logic, so I'll get an unresolved reference error.
namespace FrontEnd
{
class ServiceModule : NinjectModule
{
public override void Load()
{
this.Bind<AccountViewModel>().ToSelf();
this.Bind<DetailsViewModel>().ToSelf();
this.Bind<ISessionContext>().To<SessionContext>()
.InSingletonScope();
this.Bind<INavigationViewModel>().To<NavigationViewModel>();
this.Bind<ILoggingService>().To<LoggingService>();
// This will not work because MathClient is in the Business Logic assembly
this.Bind<IMathProvider>().To<MathClient>()
.WithConstructorArgument("binding", new BasicHttpBinding())
.WithConstructorArgument("remoteAddress", new EndpointAddress("http://localhost/server.php"));
}
}
}
I feel like aggregating all the dependency injection declaration in the same place isn't the right thing to do.
I though about declaring some static method in the IoC container so that external projects can register their own modules, but that would make things even worse, because it would mean BackEnd has a reference to the FrontEnd :
namespace FrontEnd
{
class ServiceModule : NinjectModule
{
public static void RegisterModule(Module m)
{
...
}
}
}
namespace BackEnd
{
class BackEnd
{
public void Init()
{
ServiceModule.RegisterModule(new Module() ...)
}
}
}
How can I configure all my services into my IoC container without having suspicious references between projects (like Backend -> FrontEnd) ?