Whilst the concepts of DI
and IoC containers
are fairly straight forward I seem to be struggling with the implementation. I have a four-tier application in which the UI Layer
to Service Layer
uses IoC
and seems to be working perfect but the Service Layer
to Business Layer
is an absolute pain.
I've read many articles specifically Ninject
and Class Libraries
but I'm still having trouble implementing correctly. I'm hoping you kind folk can point me in the right direction...
Typical hierarchical flow: UI Layer > Service Layer > Business Layer > Data Layer
So If may show the implementation of my UI Layer
to Service Layer
:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IApiAuthorizationService>().To<ApiAuthorizationService>();
}
public class DebateController : ApiController
{
private IApiAuthorizationService _iApiAuthorizationService;
public DebateController(IApiAuthorizationService iApiAuthorizationService)
{
_iApiAuthorizationService = iApiAuthorizationService;
}
}
As you can see the UI Layer
is a WebApi
project that injects the IApiAuthorizationService nothing terribly complicated here.
So once ApiAuthorizationService is constructed it points to many repositories but for now I'll add a snippet of just the one.
We're in the Service Layer now which references the Business Layer:
public class ApiAuthorizationService : IApiAuthorizationService
{
private IApiAuthTokenRepository _iApiAuthTokenRepository;
public ApiAuthorizationService(IApiAuthTokenRepository iApiAuthTokenRepository)
{
_iApiAuthTokenRepository = iApiAuthTokenRepository;
}
}
At this point I've installed Ninject on the Service Layer
and also created a class that will create the bindings:
public class Bindings : NinjectModule
{
public override void Load()
{
Bind<IApiAuthTokenRepository>().To<ApiAuthTokenRepository>();
}
}
Basically I'm stuck at this point and not sure where to go, again I've read many posts but many use Console Applications
to demonstrate and a Class Library
does not have an entry point. What I was thinking was to add a Startup.cs
to initialize the bindings.
Could anyone point me in the right direction with some demo code?
Thank you.