Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MVC4中为3层体系结构配置Ninject_C#_Asp.net Mvc 4_Architecture_Ninject - Fatal编程技术网

C# 在MVC4中为3层体系结构配置Ninject

C# 在MVC4中为3层体系结构配置Ninject,c#,asp.net-mvc-4,architecture,ninject,C#,Asp.net Mvc 4,Architecture,Ninject,目前,我正在建设一个有2层的网站 WebUI:这是一个MVC4项目,用于与用户交互 数据访问:这是一个C库。该层负责与数据库交互。 我正在使用Ninject进行依赖注入。到目前为止,还可以。但现在,我想在Web UI和数据访问之间插入一个新的层,称为业务逻辑。因此,架构将是: WebUI:使用来自业务逻辑的接口。 业务逻辑:使用数据访问接口。 数据访问:保持不变。 我的问题是,我应该如何在Web UI和业务逻辑中配置我的Ninject来实现我想要的?以下是我目前的源代码: 数据访问层: 接口IH

目前,我正在建设一个有2层的网站

WebUI:这是一个MVC4项目,用于与用户交互 数据访问:这是一个C库。该层负责与数据库交互。 我正在使用Ninject进行依赖注入。到目前为止,还可以。但现在,我想在Web UI和数据访问之间插入一个新的层,称为业务逻辑。因此,架构将是:

WebUI:使用来自业务逻辑的接口。 业务逻辑:使用数据访问接口。 数据访问:保持不变。 我的问题是,我应该如何在Web UI和业务逻辑中配置我的Ninject来实现我想要的?以下是我目前的源代码:

数据访问层:

接口IHotelRepository.cs

HotelController.cs酒店


谢谢您的帮助。

我不确定是否理解您的问题,但我认为您不必在业务层配置Ninject。您的应用程序中应该只有一个DI配置,它应该在应用程序启动时

您的业务层肯定包含业务对象和使用存储库合同的业务服务。这些服务将是有益的。在这种情况下,您将在应用程序启动时配置服务绑定,将IHotelService绑定到HotelService

我看到的设计是,您的控制器现在将使用您将在控制器构造函数中注入IHotelService的服务

希望它能帮助你!
Julien不确定是否理解您的问题,但我认为您不必在业务层配置Ninject。您的应用程序中应该只有一个DI配置,它应该在应用程序启动时

您的业务层肯定包含业务对象和使用存储库合同的业务服务。这些服务将是有益的。在这种情况下,您将在应用程序启动时配置服务绑定,将IHotelService绑定到HotelService

我看到的设计是,您的控制器现在将使用您将在控制器构造函数中注入IHotelService的服务

希望它能帮助你!
Julien

只需在NinjectControllerFactory中的AddBindings方法中添加业务逻辑中所需其他组件的绑定即可。Ninject能够解析诸如controller->service->repository之类的链式依赖项。

只需在NinjectControllerFactory中为其他所需组件从业务逻辑添加绑定到AddBindings方法。Ninject能够解析诸如控制器->服务->存储库之类的链式依赖项。

我添加您可以使用Ninject定义模块,继承自NinjectModule,这将帮助您排序绑定条件。我添加您可以使用Ninject定义模块,继承自NinjectModule,这将帮助您对绑定条件进行排序
public interface IHotelRepository
{
    IQueryable<Hotel> Hotels { get; }
}
public class HotelRepository : IHotelRepository
{
    private HotelDbEntities context = new HotelDbEntities();

    public IQueryable<Hotel> Hotels { get { return context.Hotels; } }
}
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>();
    }
}
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());
    }
}
public class HotelController : Controller
{
    private IHotelRepository hotelRepository;

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

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

}