I have a 3 layer application and the layers are:
So the Web
layer doesn't know anything about my DAL
layer. I have repository interfaces and concrete classes in my DAL
, which are used in BLL
layer in business logic classes. The question is, in order to decouple DAL
and BLL
, how do I setup Ninject to inject my repository implementations to the BLL
layer?
The same question is for Web
layer and BLL
layer, I have interfaces and implementations on BLL
which I use them in Web
layer, how should I setup Niject for this?
The Dependency Injection (DI) Design PatternThe Dependency Resolver in ASP.NET MVC can allow you to register your dependency logic somewhere else (e.g. a container or a bag of clubs). The advantages of using Dependency Injection pattern and Inversion of Control are the following: Reduces class coupling.
MVC contains Model (Data), View (UI), and Controller (Logic).
Dependency Injection is the design pattern that helps us to create an application which loosely coupled. This means that objects should only have those dependencies that are required to complete tasks.
In the three layers architecture, as you can see, Presentation layer and business layer communicate with each other, and business layer (Controller) and data access layer (Model) communicate with each other. The presentation layer is used to display the data to the users.
The idea is that you define interfaces for your DAL and BLL. You then take an instance of such an interface as a constructor parameter. Example
interface IDatabase
{
// Methods here
}
Your BLL class:
public class Bll
{
IDatabase _db;
public Bll(IDatabase db)
{
_db = db;
}
public void SomeMethod()
{
// Use db here
}
}
Then in your composition root (in the web application) you use the kernel to configure these dependencies:
kernel.Bind<IDatabase>().To<ConcreteDatabase();
You need the same thing from your controllers to your BLL, but it works the same way.
Apart from that, I think your dependencies are not correctly set up. In general you don't want these vertical dependencies. You should aim for a flatter hierarchy. I wrote a blog post about this: http://www.kenneth-truyers.net/2013/05/12/the-n-layer-myth-and-basic-dependency-injection/
In my blog post I explain what the problem is with such a hierarchy and how you can avoid it. Apart from that, it describes exactly your problem: ASP.NET MVC, BLL, DLL and Ninject to tie it together.
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