Dependency Injection in a 3 layer asp.net mvc application

孤者浪人 提交于 2019-12-02 20:50:04

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.

We faced this issue in our enterprise level application as well. How do you load up your dependency injection engine with classes from your business and data tiers without creating hard references to the business and data tiers from your web application. We played with a few designs initially and came up with this very successful design which has worked for us for 18 months in production so far and performs very well.

In the ninjectwebcommon file in your web application, use reflection to access your business and data tiers so that you can load up everything needed

Like so:

        System.Reflection.Assembly assembly;

        assembly = System.Reflection.Assembly.Load("our.biztier");
        kernel.Load(assembly);

        assembly = System.Reflection.Assembly.Load("our.datatier");
        kernel.Load(assembly);

Ninjects "Load" method looks for any class in the assembly which inherits the ninject class "NinjectModule" and then calls it to load everything into the kernel.

So our business and data tiers each contain one simple injection class which we use to load up everything.

public class InjectionModuleBiz : NinjectModule
{


    public override void Load()
    {
        Kernel.Bind<ICustomerBiz>().To<CustomerBiz>().InRequestScope();
        Kernel.Bind<IEmployeeBiz>().To<EmployeeBiz>().InRequestScope();
    }
}

and we have another injectionModule class in our data tier

public class InjectionModuleData : NinjectModule
{


    public override void Load()
    {
        Kernel.Bind<ICustomerData>().To<CustomerData>().InRequestScope();
        Kernel.Bind<IEmployeeData>().To<EmployeeData>().InRequestScope();
    }
}

The end result is that all of our business tier and data tier classes are loaded up in our ioc container and can be injected anywhere.

Hope that helps.

I agree there is lots of confusion when we use Dependency injection in N-Tier applications using Visual Studio.

Mainly because in Visual Studio we structure layers as different projects in a solution and adding the dependencies by referring the project or DLL. This is quite different than the principles of DI and Composition Root concepts.

The basic question is what dependencies we are injecting ?

Business Logic or Repository ?

If it is repository , yes you are right , the web layer need not be knowing about it. It is the BLL select the repository based on certain conditions.

If we have completely isolated applications we need to set up DI at two levels.

Your web application will needs to setup ninject to create your BLL components. The BLL application will setup ninject to create a specific set of logic and repository classes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!