Doubt about how to implement a C# Dependency Injection using Ninject

Asked

Viewed 970 times

1

I have a C# project with Windows Forms. I have a question about how to make an implementation between two layers of my application: Application and WindowsForms.

I need to carry one DataGridView with a list of "PessoaTipo".

I created an instance of my Application class PessoaTipoAppService in the WindowsForms and called the function _PessoaTipoApp.GetAll(), only when I carry builder, fails to implement Dependency Injection with Ninject.

public partial class Form1 : Form
    {
        private readonly IPessoaTipoAppService _PessoaTipoApp;

        public Form1(IPessoaTipoAppService PessoaTipoApp)
        {
            InitializeComponent();

            _PessoaTipoApp = PessoaTipoApp;

            dataGridView1.DataSource = _PessoaTipoApp.GetAll();
        }
    }

Will someone give me a hand? I left the project available for download. Here we can post the solution here in this topic...

https://onedrive.live.com/? id=40838E65B9F8787E%21120&Cid=40838E65B9F8787E

1 answer

1

Minimal Example:

Install the package by Nuget or Package Manager Console

Install-Package Ninject

Create a class that is responsible for solving dependencies:

FormModule

using Ninject.Modules;
public class FormModule : NinjectModule
{
    public override void Load()
    {
        Bind<IMount>().To<Mount>();
    }

    public static FormModule Create()
    {
        return new FormModule();
    }
}

in the method Load call the Bind for class configuration and put all classes that will be used in your project and that are dependencies of other forms.


Create a class that will be responsible for instantiating Form and consequently the their dependencies:

FormResolve

using Ninject;
using Ninject.Modules;
public class FormResolve
{
    private static IKernel _ninjectKernel;

    public static void Wire(INinjectModule module)
    {
        _ninjectKernel = new StandardKernel(module);
    }

    public static T Resolve<T>()
    {
        return _ninjectKernel.Get<T>();
    }
}

In the archive Program.cs configure as follows:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            FormResolve.Wire(FormModule.Create());
            Application.Run(FormResolve.Resolve<Form1>());
        }    
    }
}

With these two classes and the configuration done in the Program.cs is already ready to solve dependencies.

Example: Form1

using Ninject;
using System.Windows.Forms;  
namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {

        private IMount Mount { get; set; }

        [Inject()]
        public Form1(IMount mount)
        {
            InitializeComponent();
            Mount = mount;
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            label1.Text = Mount?.GetDateTime().ToLongDateString();

        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            Form2 frm = FormResolve.Resolve<Form2>();
            frm.ShowDialog();
        }
    }
}

To call the form2 or any form follow the example of the button below where form1 calls the form2:

private void button1_Click(object sender, System.EventArgs e)
{
    Form2 frm = FormResolve.Resolve<Form2>();
    frm.ShowDialog();
}

References:

  • Thanks Virgilio.... The Program.Cs class is giving error (Underlining red yet....) ;

  • Depending on the version you installed Ninject, red should be obsoleto, please check this out and if this is it install the stable Ninject which is the version Install-Package Ninject -Version 3.2.2

  • Yes, the latest version of Ninject has been installed... The problem is that in Program.Cs it is as if the Application is no longer available or without reference...

  • Look, I saw your project, it’s missing from it class I ended up remembering so I made a minimal example as it should be, in your project it was impossible for me to keep coding that was why I decided to contribute an answer this way, being an ideal project for adaptation with yours. I mean, the mistake is in your project and not in my proposal.

  • OK Virgilio. Anyway, thanks anyway!

  • Look @Jalberromano excuse something, but, your attached project in question missing class and that kind of breaks the rule of here we propose solution, even this is functional beyond is that the project in question lack class!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.