Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it a bad practice to create multiple instance of Kernel in Ninject?
    primarykey
    data
    text
    <p>I have a ASP.Net Web Application with many Custom Server Controls.</p> <p>Unfortunately, <a href="https://stackoverflow.com/questions/19736271/how-inject-depedency-to-compositecontrol-using-ninject">Ninject could not inject dependency into CompositeControls</a>.</p> <p>I'm new to Ninject; the following is my simple approach to solve the problem.</p> <p>Since I have many Custom Server Controls, I'll end up creating a multiple of instance of StandardKernel.</p> <p>Is it a bad design? <strong>Please correct me if I'm wrong.</strong> Thanks!</p> <pre><code>public interface ICalculate { int Add(int x, int y); } public class Calculate : ICalculate { public int Add(int x, int y) { return x + y; } } public class DemoModule : NinjectModule { public override void Load() { Bind&lt;ICalculate&gt;().To&lt;Calculate&gt;(); } } public class MyServerControl : CompositeControl { private TextBox TextBox1; private TextBox TextBox2; private Label Label1; public ICalculate Calculate { get; set; } public MyServerControl() { IKernel kernel = new StandardKernel(new DemoModule()); Calculate = kernel.Get&lt;ICalculate&gt;(); } 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); } } </code></pre> <p><img src="https://i.stack.imgur.com/0oBVZ.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload