I'm trying to design WebApi application with using IoC like Ninject. I have the following layers(3 projects):
The Repository layer has interface IRepository<T>
and a few implementations of it. And in the Service also exists interface IService<T>
with a two different implementations.
Could you please advise me should I use DI container (Ninject) in WebApi project to bind IService<T>
and ServiceConcrete<T>
and DI container in the Service project to bind IRepository<T>
and RepositoryConcrete<T>
?
Or maybe should I use only one DI in WebAppi project?
A practical way I have found to set up Ninject modules can be found below.
DependencyResolution
DependencyResolution
and your Domain projects referenced in your WebAPI projectNinjectWebCommon.cs
ServiceModule.cs
, RepositoryModule.cs
, etc.
Create your Ninject module(s). For detailed instructions on this you can refer my answer on this.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:
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.
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.
If I understand your question, you're having troubles configuring your Repository layer because your config code is in your application layer, which probably only references your service layer (which in turn references your repository layer). What I've done in order to get around this is first create your configurations in modules (these can live on any layer, but you must reference Ninject)
For your repo layer:
public class RepoNinjectModule : NinjectModule
{
public override void Load()
{
Bind<IMyRepo>().To<MyRepo>();
}
}
create a similar Module in your service layer:
public class ServiceNinjectModule : NinjectModule
{
public override void Load()
{
Bind<IMyService>().To<MyServce>();
}
}
Then, in your application layer, you can load the modules dynamically (this is what NinjectWebCommon.cs
looks like):
private static void RegisterServices(IKernel kernel)
{
kernel.Load(AppDomain.CurrentDomain.GetAssemblies());
}
More info on modules: https://github.com/ninject/Ninject/wiki/Modules-and-the-Kernel
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With