Click here to Skip to main content
15,885,097 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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(new Form1());
            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>
    {
    }


What I have tried:

Above code is that I tried and tried to resolve this problem few times but unfortunately it can not be because i am very new to this desktop app so if any one can help me that is appreciate thanks in advanced
Posted
Updated 23-Dec-20 6:34am

1 solution

solution is you have to resolve Form2 as well.
private void button1_Click(object sender, EventArgs e)
{

Form2 frm = new *Form2()*;
frm.Show();
}
if you rewrite it as
var frm2 = CompositionRoot.Resolve<form2>();
frm2.Show();

It will work and dependency will be resolved.

Regards.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900