如何使用Ninject将依赖注入到Composite Control中

时间:2013-11-01 22:15:00

标签: c# asp.net dependency-injection ninject

如何将依赖注入CompositeControl

我尝试了以下方法 - MyServerControl的Calculate仍为空。

谢谢!

public class MyServerControl : CompositeControl
{
    private TextBox TextBox1;
    private TextBox TextBox2;
    private Label Label1;

    [Inject] // **** This is null **** 
    public ICalculate Calculate { get; set; }

    protected override void CreateChildControls()
    {
        TextBox1 = new TextBox {ID = "TextBox1", Text = "1"};
        Controls.Add(TextBox1);

        TextBox2 = new TextBox {ID = "TextBox2", Text = "2"};
        Controls.Add(TextBox2);

        var button1 = new Button {ID = "Button1", Text = "Calculate"};
        button1.Click += button1_Click;
        Controls.Add(button1);

        Label1 = new Label {ID = "Label1"};
        Controls.Add(Label1);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        int value1 = Int32.Parse(TextBox1.Text);
        int value2 = Int32.Parse(TextBox2.Text);

        Label1.Text = "Result:" + Calculate.Add(value1, value2);
    }
}

public interface ICalculate
{
    int Add(int x, int y);
}

public class Calculate : ICalculate
{
    public int Add(int x, int y)
    {
        return x + y;
    }
}

来自NuGet的默认Ninject.Web.Common Bootstrapper:

using System.Net.NetworkInformation;

[assembly: WebActivator.PreApplicationStartMethod(typeof(NinjectDemo.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(NinjectDemo.App_Start.NinjectWebCommon), "Stop")]

namespace NinjectDemo.App_Start
{
    using System;
    using System.Web;

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;

    using Ninject;
    using Ninject.Web.Common;

    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 the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            IKernel kernel = new StandardKernel();
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

            RegisterServices(kernel);
            return kernel;
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<ICalculate>().To<Calculate>().InSingletonScope();
        }        
    }
}

更新

我无法在Page_Load中获取内核实例。我错过了什么吗?

<my:MyServerControl ID="MyServerControl1" runat="server" />

public partial class Default : Page
{
    [Inject]
    public ICalculate _calculate { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        kernel.Inject(MyServerControl1); // kernel is not available
    }
}

enter image description here

3 个答案:

答案 0 :(得分:2)

我认为你可以使用满足现有对象依赖性的功能。在这种特殊情况下,在任何使用控件的情况下,只需调用

即可
kernel.Inject( myControl );

其中myControl是复合控件的现有实例。这必须从后面的代码调用,在管道中已经创建实例的地方。 Page_Load很可能会很好。

编辑:有许多方法可以解决您的应用程序中的任何位置。例如,您可以拥有一个全局服务定位器。但是,由于您使用的是Bootstrapper,您应该可以在任何地方重新打印内核

 var kernel = (IKernel)Bootstrapper.Container;

答案 1 :(得分:1)

您的默认页面不知道NinjectWebCommon类是否存在。它也无法知道内核变量,它是NinjectWebCommon.CreateKernel()方法的成员。最简单的解决方案如下:

public static class NinjectWebCommon 
{
    ...
    private static IKernel kernel;

    public static IKernel CreateKernel()
    {
        if(kernel != null)
            return kernel;

        kernel = new StandardKernel();
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

        RegisterServices(kernel);
        return kernel;
    }
    ...
}

public partial class Default : Page
{
    [Inject]
    public ICalculate _calculate { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        NinjectWebCommon.CreateKernel().Inject(MyServerControl1);
    }
}

另一种方式是使用Ninject魔法。您的应用程序类可能需要从Ninject提供的类继承。在 MVC 中,它是一个NinjectHttpApplication类,它会覆盖引导程序。你可能会使用Wiktor的答案。

老实说,我不喜欢那种Ninject魔法,因为它有时对我不起作用,而且很难找到原因。在我的 MVC 应用程序中,我最终创建了自己的ConfrollerFactory,它明确地注入了依赖项。如果要更改IOC容器,也可能会很痛苦。

答案 2 :(得分:0)

您需要注册您的Ioc配置,请参阅示例:

public static void RegisterIoc(HttpConfiguration config)
        {
            var kernel = new StandardKernel(); // Ninject IoC

            kernel.Bind<IMyService>().To<MyService>();

            // Tell WebApi how to use our Ninject IoC
            config.DependencyResolver = new NinjectDependencyResolver(kernel);
        }

public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
    {
        private IKernel kernel;

        public NinjectDependencyResolver(IKernel kernel)
            : base(kernel)
        {
            this.kernel = kernel;
        }

        public IDependencyScope BeginScope()
        {
            return new NinjectDependencyScope(kernel.BeginBlock());
        }
    }

public class NinjectDependencyScope : IDependencyScope
    {
        private IResolutionRoot resolver;

        internal NinjectDependencyScope(IResolutionRoot resolver)
        {
            Contract.Assert(resolver != null);

            this.resolver = resolver;
        }

        public void Dispose()
        {
            var disposable = resolver as IDisposable;
            if (disposable != null)
                disposable.Dispose();

            resolver = null;
        }

        public object GetService(Type serviceType)
        {
            if (resolver == null)
                throw new ObjectDisposedException("this", "This scope has already been disposed");

            return resolver.TryGet(serviceType);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            if (resolver == null)
                throw new ObjectDisposedException("this", "This scope has already been disposed");

            return resolver.GetAll(serviceType);
        }
    }

在App_start文件夹中添加此类,然后在Global.asax.cs中写入:

// Tell WebApi to use our custom Ioc (Ninject)
            IocConfig.RegisterIoc(GlobalConfiguration.Configuration);