简体   繁体   中英

Understanding Dependency Injection, IoC in a MVC nTier Application

I understand DI and IoC but I can't seem to understand how to implement it in a nTier application. This is a simple MVC application I'm trying to build with one domain object.

Layers: DAL and UI will reference the BLL Layer. DAL<--BLL-->UI

DAL will contain EntityFramework, SQLBlogRepository, Blog.cs and Mapper BAL will contain IBlogRepository, Domain object: Blog.cs UI will implement Constructor DI of IBlogRepository

This is where I'm stuck. How can I used Ninject so the constructor know to used SqlBlogRepository implementation? I also ran into a few examples that uses "Composition Root" which add more confusion. Then there are example that uses Repository Pattern. The bottom line is, I'm trying implement a nTier MVC application that is loosely couple and used IoC/Dependency Injection. Please help me map out how I can make a call from the UI layer and have the DAL layer return data via BLL layer while all three layer are loosely coupled.

I am assuming you do not have a ninject configuration file created. So here is how we did it where I am at.

To explain what I am doing. You need to have your concrete class implement whatever Interface you want to use.

In the ninject config file you need to bind your Interfaces to your concrete classes. Then you can call ninject to get your interfaces and it will return the concrete class that you want.

If you want to use this in an N- Tier development environment.

DAL <- >BL <->UI

We stuck the ninject configuration in the BL. Since the Bl is the only layer that References the DAL. We exposed the DAL interfaces/concrete classes to the BL via references. Then in the BL we added the Ninject Configuration.

Once that was done in the UI we were able to access all of the Business objects.

ninject starter tutorial

DAL

public interface IRepository
{
   //does some things
}

public SQLBlogRepository : IRepository
{
//implements IRepository
}

BL

NINJECT CONFIGURATION FILE

public Foo : IFOO
{
     public Foo(IRepository steve){}
}

public interface IFOO
{
}

public class NinjectConfig : NinjectModule
{
    public override void Load()
    {
      Bind<IRepository>.To<SQLBlogRepository>();
      Bind<IFOO>.To<Foo>();
     }
}

then you use it like so.

var repo = new StandardKernel(new NinjectConfig()).Get<IRepository>();
var fooManager = new StandardKernel(new NinjectConfig()).Get<IFOO>();

The var fooManager , through the power of ninject will autoInstantiate your repo. So you do not need to create it yourself. The config file will handle all the dependancies in the constructor. So you never need to know what constructor creations you need to do. And you can also change the creations in one spot and propagate it out automagically through your code.

In your sample the UI (Mvc Project) is your Composition Root and you can config your dependency in it (with any ioc container like ninject). Your mvc project must have a reference to BLL and DAL layer and you must make a mapping like @gh9 answer in your mvc project.

In your controller you have

public class HomeController : Controller
{
    private readonly IFOO _fooBll;
    public HomeController(IFOO fooBll){
    _fooBll=fooBll;
     }
    public ActionResult Index()
    {
        return View();
    }
}

and you must have a controller factory to resolve your controller dependency like :

public class NinjectControllerFactory : DefaultControllerFactory
{
    private IKernel ninjectKernel;

    public NinjectControllerFactory()
    {
        ninjectKernel = new StandardKernel();
    }

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        return controllerType == null
               ? null
               : (IController) ninjectKernel.Get(controllerType);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM