The practical way I found to configure Ninject modules can be found below.
Overview
- Create an assembly called
DependencyResolution - Create Ninject Modules (Which You Will Use In Your WebAPI Project)
- Only have this
DependencyResolution and your projects in the domain specified in your WebAPI project - Initialize / register your modules in
NinjectWebCommon.cs
More details
- It should be easy, if you create a project , add Ninject as a link from NuGet, for example.
- Add a new class file to this project, named after the modules you want to create, for example:
ServiceModule.cs , RepositoryModule.cs , etc. Create a Ninject module (s) . Detailed instructions on this subject can be found in this. - In your WebAPI project, you add a link to this newly created
DependencyResolution project and your domain project. Initialization / registration of the newly created module (s) in the WebAPI project 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 solve another problem that is not related to your question. I think your current layer setup will need to be slightly modified.
The main and probably my biggest problem with your layers is that you mix and therefore tightly connect the Domain and the Repository, which is clearly an infrastructure interest .
I would suggest rebuilding your layers as follows:
- Domain
- Services
- Infrastructure (implementation of the Repository can go here, for example)
- Dependency Resolution
- Webapi
Do not forget that your domain level should not have an idea about infrastructure details, such as a repository, otherwise you will intentionally bind your domain to unnecessary implementation details.
EDIT: From the comments, I see that you have some problems about where to place and how to name things that are obviously one of the most difficult tasks in programming.
So my thoughts on resolving this confusion are:
Layer: this is a logical separation or collection point for classes, methods, etc. that belong to each other.
Each layer can consist of several projects or assemblies. Therefore, if you want to classify your projects into layers, you can create directories in your solution, named by your layers, and put individual projects inside these directories. It really is just a matter of taste in the mouth, take this as a hint.
Structure example
- solution root
- Main directory
- Domain assembly: the root of your domain in which you have your business objects or domain objects, and all the interfaces used by your domain.
- Collection of domain services (it may just be in the domain assembly)
- Service Catalog
- Assembly of application services: for example, an example of this assembly contains services or facades that span operations across multiple domain objects or aggregates, etc.).
- Infrastructure Catalog
- Repository assembly: this is where you have implementations of your EF repositories.
- Custom logging / email / any other builds or implementations that do not belong to the domain.
- DependencyResolution Node: This is the place for your NInject modules and all connections associated with the IOC container.
- user interface directory
- WebAPI assembly
- Asp.Net MVC Assembler
Summary
The dependency resolution project has links to any necessary assemblies (domain for interfaces, services / infrastructure for their implementation) and fully connects them for later use.
For a WebAPI project, you only need to add additional references to the domain and dependency, so you can simply request your interfaces in your constructor for public / public WebAPI methods / functions, and Ninject will do the dirty work behind the scenes.
Please do not forget that this is just an easy quick โdirty architectural proposalโ without knowing your exact requirements and use cases.