4配置Ninject为3层建筑

4配置Ninject为3层建筑

本文介绍了在MVC 4配置Ninject为3层建筑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我建立一个网站,2层。

Currently, I'm building a website with 2 layers.


  • 的Web UI:这是一个MVC 4项目,用于与用户进行交互

  • 数据访问:这是一个C#库。这一层负责与数据库进行交互。

我使用Ninject依赖注入。截至目前,它的确定。但现在,我要插入Web UI,并数据访问之间一个新的图层,称为业务逻辑。因此,结构将是:

I'm using Ninject for Dependency Injection. Up to now, it's OK. But now, I want to insert a new layer between Web UI and Data Access, called Business Logic. So the architecture would be:


  • 的Web UI:使用界面从业务逻辑

  • 商业逻辑:使用接口与数据访问

  • 数据访问:保持相同

我的问题是,我应该怎么在网页UI和业务逻辑配置我的Ninject,以达到我想要的这是在这个时候我的源$ C ​​$ C:

My question is, how should I configure my Ninject in Web UI and Business Logic to achieve what I want? Here is my source code at this time:

数据访问层:

Data Access Layer:

接口IHotelRepository.cs

public interface IHotelRepository
{
    IQueryable<Hotel> Hotels { get; }
}

混凝土类HotelRepository.cs

public class HotelRepository : IHotelRepository
{
    private HotelDbEntities context = new HotelDbEntities();

    public IQueryable<Hotel> Hotels { get { return context.Hotels; } }
}

网络用户界面层:

Web UI layer:

NinjectControllerFactory.cs

public class NinjectControllerFactory : DefaultControllerFactory
{
    private IKernel ninjectKernel;

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

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

    private void AddBindings()
    {
        ninjectKernel.Bind<IHotelRepository>().To<HotelRepository>();
    }
}

的Global.asax.cs

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();

        ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
    }
}

HotelController.cs

public class HotelController : Controller
{
    private IHotelRepository hotelRepository;

    public HotelController(IHotelRepository repository)
    {
        hotelRepository = repository;
    }

    public ActionResult List()
    {
        return View(hotelRepository.Hotels);
    }

}

感谢您的帮助。

推荐答案

刚刚从经营业务逻辑添加其他必需的组件绑定到你的 AddBindings 方法 NinjectControllerFactory 。 Ninject能够解析链接的依赖就像控制器 - >服务 - >库

Just add bindings for additional required components from bussiness logic to your AddBindings method in NinjectControllerFactory. Ninject is able to resolve chained dependencies like controller -> service -> repository.

这篇关于在MVC 4配置Ninject为3层建筑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 05:20