MVC3& Ninject / Unity:注入分层服务容器

时间:2012-05-24 08:30:53

标签: c# asp.net-mvc-3 dependency-injection ninject unity-container

我一直在使用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,实现这一点,因为我将很快改变它),并且模式/方法是否有一个我还没有听说过的特定名称?

1 个答案:

答案 0 :(得分:2)

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>();
}