title: How to use dependency injection in WinForms
body: <p>How to define dependency injection in Winforms C#?</p>
  <p>Interface ICategory:</p>
  <pre><code>public interface ICategory
  {
      void Save();
  }
  </code></pre>
  <p>Class CategoryRepository:</p>
  <pre><code>public class CategoryRepository : ICategory
  {
  
      private readonly ApplicationDbContext _context;
  
      public CategoryRepository(ApplicationDbContext contex)
      {
          _context = contex;
      }
      public void Save()
      {
          _context.SaveChanges();
      }
  }
  </code></pre>
  <p>Form1:</p>
  <pre><code>public partial class Form1 : Form
  {
      private readonly  ICategury _ic;
      public Form1(ICategury ic)
      {
          InitializeComponent();
      _ic=ic
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
      Form2 frm= new Form2();
      frm.show();
  }
   }
  </code></pre>
  <p>Form2:</p>
  <pre><code>public partial class Form2 : Form
  {
      private readonly  ICategury _ic;
      public Form2(ICategury ic)
      {
          InitializeComponent();
      _ic=ic
      }
   }
  </code></pre>
  <p>Problem?</p>
  <ol>
  <li><p>Definition of dependency injection in Program.cs</p>
  <pre><code>Application.Run(new Form1());
  </code></pre>
  </li>
  <li><p>Definition of dependency injection at the time of Form 2 call</p>
  <pre><code>Form2 frm= new Form2();
  frm.show();
  </code></pre>
  </li>
  </ol>