Search code examples
c#winformsc#-4.0ninjectninject-2

Ninject implementation in Window Forms - C#


I am using Window Forms C# with Ninject version: 2.0.0.1

First Installed the Ninject Package : ninject.extensions.infrastructure.winforms

Second I created the new class with following code.

public class CustomModule : NinjectModule
{
    public override void Load()
    {
        Bind<IDAL.IORDR>().To<DAL.DAL_ORDR>();
    }
}

Third in the window form I did below

public partial class SODetails : Form
{
    public IORDR _IORDR { get; set; }

    [Inject]
    public SODetails(IORDR ORDR)
    {
        _IORDR = ORDR;
    }

    public SODetails()
    {
        InitializeComponent();
    }
}

Finally in the Program.cs, I did the below code

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    var kernel = new StandardKernel(new CustomModule());
    var form = kernel.Get<SODetails>();
    Application.Run(form);
}

Although I am able to Inject the dependencies successfully but the form is not showing any control. Am I doing the Ninject implementation in wrong way ?


Solution

  • I changed the following code from

    public partial class SODetails : Form
    {
        public IORDR _IORDR { get; set; }
    
        [Inject]
        public SODetails(IORDR ORDR)
        {
            _IORDR = ORDR;
        }
    
        public SODetails()
        {
            InitializeComponent();
        }
    }
    

    to

    public partial class SODetails : Form
    {
        public IORDR _IORDR { get; set; }
    
        [Inject]
        public SODetails(IORDR ORDR)
        {
            _IORDR = ORDR;
            InitializeComponent();
        }
    
        public SODetails()
        {
    

    InitializeComponent();

        }
    }