A practical way I have found to set up Ninject modules can be found below.
Overview
- Create an assembly called
DependencyResolution
- Create the Ninject modules (that you will utilize in your WebAPI project)
- Have only this
DependencyResolution
and your Domain projects referenced in your WebAPI project
- Initalize/register your modules in
NinjectWebCommon.cs
Details
- Should be easy as create a project, add Ninject as reference from NuGet for instance.
- Add new class file(s) to this project named after the modules you want to create like:
ServiceModule.cs
, RepositoryModule.cs
, etc.
Create your Ninject module(s). For detailed instructions on this you can refer my answer on this.
- In your WebAPI project, you add reference to this just created
DependencyResolution
project and your Domain project.
Initializing/registering your just created module(s) in the WebAPI project's NinjectWebCommon.cs
as:
private static void RegisterServices(IKernel kernel)
{
var modules = new List<INinjectModule>
{
new ServiceModule(),
new RepositoryModule()
};
kernel.Load(modules);
}
I will also try to address another concern that is loosely related to your question. I think your current layering setup would need a bit to be changed.
The basic and probably my biggest problem with your layers are that you mix up and therefore tightly couple the Domain and Repository which is clearly an infrastructural concern.
I would suggest to re-architect your layers as:
- Domain
- Services
- Infrastructure (Repository implementations could go here for instance)
- Dependency Resolution
- WebAPI
Do not forget that your Domain layer should not have any idea about infrastructural details like Repository, else you are going to tightly couple your domain with unneeded implementation details.
EDIT: From the comments I see that you have some concerns about where to place and how to name things which is obviously one of the hardest things in programming.
So my thoughts on clearing this confusion up are:
Layer: is a logical separation or collection point of classes, methods, etc. that those belong together.
Each layer can consists of multiple projects or assemblies. So if you want to categorize your projects into layers, you could create directories in your solution named about your layers and place the individual projects inside these directories. It's really just a matter of taste in the mouth, take it just as a tip.
Example structure
- Solution root
- Core directory
- Domain assembly: the root of you domain where you have your business or domain entities AND the all the interfaces that your domain is using.
- Domain services assembly (just could be in Domain assembly as well)
- Services directory
- Application services assembly: for example an example this assembly houses services or facades that spans operations accross multiple domain entities or aggregates, etc.)
- Infrastructure directory
- Repository assembly: this is where you have the implementations of your EF repositories
- Custom logging/email/whatever else assemblies or implementations that doesn't belong to the domain.
- DependencyResolution assembly: this is the place of your NInject modules and all IOC container related wirings.
- UI directory
- WebAPI assembly
- Asp.Net MVC assembly
Summary
The Dependency Resolution project has references to any needed assemblies (Domain for interfaces, Services/Infrastructure for their implementations) and wire them altogether for later use.
The WebAPI project would only need to have reference added Domain and Dependency Resolution so then you could just ask for your interfaces in your WebAPI methods/functions public constructor and Ninject will do the dirty job for you behind the scenes.
Please don't forget that this is just an easy quick 'n dirty architecture suggestion of mine, without knowing your exact requirements and use cases.