0

CompositeControlに依存関係を挿入する方法は?

私は次のアプローチを試しました - MyServerControl の Calculate はまだ 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;
    }
}

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

ここに画像の説明を入力

4

3 に答える 3