如何注入到depedency使用CompositeControl的Ninject [英] How inject depedency to CompositeControl using Ninject

查看:260
本文介绍了如何注入到depedency使用CompositeControl的Ninject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何注入依赖成<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.compositecontrol%28v=vs.110%29.aspx\"相对=nofollow> CompositeControl的?

我尝试以下方法 - MyServerControl的计算仍然是空

I tried the following approach - MyServerControl's Calculate is still null.

谢谢!

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

默认Ninject.Web.Common引导程序从的NuGet:

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中内核。我缺少的东西吗?

Updated:

I'm not able to get instance to kernel in Page_Load. Am I missing something?

<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
    }
}

推荐答案

我觉得你可以只使用满足现有对象上的依赖关系的功能。在这种特殊情况下,在任何情况下使用你的控制,你只需拨打

I think you could just use the feature that satisfies dependencies on an existing object. In this particular case, in any context your control is used, you just call

kernel.Inject( myControl );

在哪里myControl是你的复合控件的现有实例。这必须从code后面称为​​,在已经创建实例的管道的某个地方。 Page_Load中很可能会被罚款。

where myControl is an existing instance of your composite control. This has to be called from the code behind, somewhere in the pipeline where the instance is already created. Page_Load would most probably be fine.

编辑:有许多方法,能够在应用程序中的任何地方解决。例如,你可以有一个全球性的服务定位器。但是,由于您使用的是引导程序,你应该能够在任何地方resolvd内核

there are numerous ways to be able to resolve anywhere in your application. You could for example have a global service locator. But since you are using the Bootstrapper, you should be able to resolvd your kernel anywhere

 var kernel = (IKernel)Bootstrapper.Container;

这篇关于如何注入到depedency使用CompositeControl的Ninject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆