I am developing desktop base application using c# windows form. Recently I added NInject to my project, it is working good but when i call to child forms from MDI form it's asking constructor parameter of child form because i am using form constructor to pass interface parameter. i don't know how to resolve it.
please can any one help me to resolve it. this is my sample project
CompositionRoot
- public class CompositionRoot
- {
- private static IKernel _ninjectKernel;
- public static void Wire(INinjectModule module)
- {
- _ninjectKernel = new StandardKernel(module);
- }
- public static T Resolve<T>()
- {
- return _ninjectKernel.Get<T>();
- }
- }
ApplicationModule
- public class ApplicationModule : NinjectModule
- {
- public override void Load()
- {
- Bind(typeof(IRepository<>)).To(typeof(GenericRepository<>));
- Bind(typeof(ITest)).To(typeof(TestImpl));
- }
- }
Program
- static class Program
- {
- [STAThread]
- static void Main()
- {
- CompositionRoot.Wire(new ApplicationModule());
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
-
- Application.Run(CompositionRoot.Resolve<Form1>());
- }
- }
MDI Form
- public partial class Form1 : Form
- {
- private IRepository<Process> _processRepository;
- public Form1(IRepository<Process> productionRepository, ITest test)
- {
- this._processRepository = productionRepository;
- this._test = test;
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Form2 frm = new *Form2()*;
- frm.Show();
- }
- }
Child form
- public partial class Form2 : Form
- {
- private ITest _test;
- public Form2(ITest test)
- {
- _test = test;
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- var a = _test.DisplayRow();
- MessageBox.Show(a);
- }
- }
ITest
- public interface ITest
- {
- string DisplayRow();
- }
TestImpl
- public class TestImpl : ITest
- {
- public string DisplayRow()
- {
- return "Test service";
- }
- }
GenericRepository
- public class GenericRepository<T> : IRepository<T>
- {
- public override string ToString()
- {
- return "MyRepository with type : " + typeof(T).Name;
- }
- }
IRepository
- public interface IRepository<T>
- {
- }