Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Injection in a 3 layer asp.net mvc application

I have a 3 layer application and the layers are:

  • Web: Presentation Layer (ASP.NET MVC) --> only sees BLL
  • BLL: Business Logic Layer --> only sees DAL
  • DAL: Data Access Layer

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?

like image 754
Ashkan Avatar asked Mar 08 '14 23:03

Ashkan


People also ask

Can we use dependency injection in ASP.NET MVC?

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.

What are the 3 layers in MVC?

MVC contains Model (Data), View (UI), and Controller (Logic).

What is dependency injection in .NET Core MVC?

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.

What are the layers in ASP.NET MVC?

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.


1 Answers

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.

like image 175
Kenneth Avatar answered Sep 19 '22 08:09

Kenneth