2

我使用 Ninject 已经有一段时间了,但只用于简单的 DI。我有一个稍微复杂的场景,我想迎合。

我正在使用带有控制器注入的 Ninject.Web.MVC 插件,该插件在我的基本控制器上强制执行,并传递给所有继承控制器。这是我的基本模式:

using System;
using System.Web;
using System.Web.Mvc;

using Business;

using Microsoft.Web.Infrastructure.DynamicModuleHelper;

using Ninject;
using Ninject.Web.Common;

namespace Web {

    #region Controller

    public class MyController : MyBaseController {
        public MyController(IMyService1 myService1, IMyService2 myService2)
            : base(myService1, myService2) {
        }
        public ActionResult MyAction() {
            MyServiceProp1.DoSomething();
            MyServiceProp2.DoSomethingElse();
            return View();
        }
    }

    public class MyBaseController : Controller {
        protected IMyService1 MyServiceProp1 { get; set; }
        protected IMyService2 MyServiceProp2 { get; set; }

        public MyBaseController(IMyService1 myService1, IMyService2 myService2) {
            MyServiceProp1 = myService1;
            MyServiceProp2 = myService2;
        }
    }

    #endregion
}

namespace Business {

    #region Services

    public interface IMyService1 {
        void DoSomething();
    }

    public interface IMyService2 {
        void DoSomethingElse();
    }

    public class MyService1 : IMyService1 {
        public void DoSomething() {
        }
    }

    public class MyService2 : IMyService2 {
        public void DoSomethingElse() {
        }
    }

    #endregion
}

#region Ninject stuff

namespace Web.App_Start {
    public static class NinjectWebCommon {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop() {
            bootstrapper.ShutDown();
        }

        /// <summary>
        /// Creates a kernel that will manage the application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel() {
            var kernel = new StandardKernel();
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

            RegisterServices(kernel);
            return kernel;
        }

        /// <summary>
        /// Loads modules and services
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel) {
            kernel.Bind<IMyService1>().To<MyService1>();
            kernel.Bind<IMyService2>().To<MyService2>();
        }
    }
}

#endregion

如您所见,我有多种服务。如果我想添加 MyService3,我将不得不修改实现基本控制器的每个控制器。虽然这不是一项艰巨的工作,但当项目更大时......

我想做的是实现某种服务容器,我将把它注入基本控制器,因此所有实现控制器(以及任何后续类):

namespace Web {

    #region Controller

    public class MyController : MyBaseController {
        public MyController(IMyServiceContainer container)
            : base(container) {
        }
        public ActionResult MyAction() {
            MyServiceProp1.DoSomething();
            MyServiceProp2.DoSomethingElse();
            return View();
        }
    }

    public interface IMyServiceContainer {
        IMyService1 MyServiceProp1 { get; set; }
        IMyService2 MyServiceProp2 { get; set; }
    }

    public class MyBaseController : Controller, IMyServiceContainer {
        public MyBaseController(IMyServiceContainer container) {
            MyServiceProp1 = container.MyServiceProp1;
            MyServiceProp2 = container.MyServiceProp2;
        }

        #region Implementation of IMyServiceContainer

        public IMyService1 MyServiceProp1 { get; set; }
        public IMyService2 MyServiceProp2 { get; set; }

        #endregion
    }

    #endregion
}

这样,如果我需要添加全局依赖项,我不必更改任何控制器,只需更改基本控制器,这更容易维护。

有没有办法用 Ninject(甚至 Unity,因为我很快就会把它改过来)实现这一点,并且模式/方法是否有一个我还没有听说过的特定名称?

4

1 回答 1

3
public interface IMyServiceContainer 
{
    IMyService1 MyService1 { get; }
    IMyService2 MyService2 { get; }
    IMyService3 MyService3 { get; }
}

public class MyServiceContainer 
{
    public IMyService1 MyService1 { get; private set; }
    public IMyService2 MyService2 { get; private set; }
    public IMyService3 MyService3 { get; private set; }

    public MyServiceContainer(IMyService1 myService1, IMyService2 myService2, IMyService3 myService3)
    {
        MyService1 = myService1;
        MyService2 = myService2;
        MyService3 = myService3;
    }
}

然后是您的基本控制器:

public class MyBaseController : Controller 
{
    public MyBaseController(IMyServiceContainer container) 
    {
        Container = container;
    }

    protected IMyServiceContainer Container { get; private set; }
}

然后是派生的控制器:

public class MyController : MyBaseController 
{
    public MyController(IMyServiceContainer container) : base(container) 
    {
    }

    public ActionResult MyAction() 
    {
        Container.MyService1.DoSomething();
        Container.MyService2.DoSomethingElse();
        return View();
    }
}

现在剩下的就是配置 NInject:

private static void RegisterServices(IKernel kernel) 
{
    kernel.Bind<IMyService1>().To<MyService1>();
    kernel.Bind<IMyService2>().To<MyService2>();
    kernel.Bind<IMyService3>().To<MyService3>();

    kernel.Bind<IMyServiceContainer>().To<MyServiceContainer>();
}
于 2012-05-24T08:40:12.500 回答