Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

2K
Vistas
How to use dependency injection in WinForms

How to define dependency injection in Winforms C#?

Interface ICategory:

public interface ICategory
{
    void Save();
}

Class CategoryRepository:

public class CategoryRepository : ICategory
{

    private readonly ApplicationDbContext _context;

    public CategoryRepository(ApplicationDbContext contex)
    {
        _context = contex;
    }
    public void Save()
    {
        _context.SaveChanges();
    }
}

Form1:

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();
}
 }

Form2:

public partial class Form2 : Form
{
    private readonly  ICategury _ic;
    public Form2(ICategury ic)
    {
        InitializeComponent();
    _ic=ic
    }
 }

Problem?

  1. Definition of dependency injection in Program.cs

    Application.Run(new Form1());
    
  2. Definition of dependency injection at the time of Form 2 call

    Form2 frm= new Form2();
    frm.show();
    
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

How to use Dependency Injection (DI) in Windows Forms (WinForms)

To use DI in a WinForms .NET 5 or 6 you can do the following steps:

  1. Create a WinForms .NET Application

  2. Install Microsoft.Extensions.Hosting package (which gives you a bunch of useful features like DI, Logging, Configurations, and etc.)

  3. Add a new interface, IHelloService.cs:

    public interface IHelloService
    {
        string SayHello();
    }
    
  4. Add a new implementation for your service HelloService.cs:

    public class HelloService : IHelloService
    {
        public string SayHello()
        {
            return "Hello, world!";
        }
    }
    
  5. Modify the Program.cs:

    //using Microsoft.Extensions.DependencyInjection;
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.SetHighDpiMode(HighDpiMode.SystemAware);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
    
            var host = CreateHostBuilder().Build();
            ServiceProvider = host.Services;
    
            Application.Run(ServiceProvider.GetRequiredService<Form1>());
        }
        public static IServiceProvider ServiceProvider { get; private set; }
        static IHostBuilder CreateHostBuilder()
        {
            return Host.CreateDefaultBuilder()
                .ConfigureServices((context, services)=>{
                    services.AddTransient<IHelloService, HelloService>();
                    services.AddTransient<Form1>();
                });
        }
    }
    

Now you can inject IHelloService in Form1 and use it:

//using Microsoft.Extensions.DependencyInjection;
public partial class Form1 : Form
{
    private readonly IHelloService helloService;

    public Form1(IHelloService helloService)
    {
        InitializeComponent();
        this.helloService = helloService;
        MessageBox.Show(helloService.SayHello());
    }
}

If you want to show Form2 using DI, you first need to register it services.AddTransient<Form2>();, then depending to the usage of Form2, you can use either of the following options:

  • If you only need a single instance of Form2 in the whole life time of Form1, then you can inject it as a dependency to the constructor of Form1 and store the instance and show it whenever you want.

    But please pay attention: it will be initialized just once, when you open Form1 and it will not be initialized again. You also should not dispose it, because it's the only instance passed to Form1.

    public Form1(IHelloService helloService, Form2 form2)
    { 
         InitializeComponent();
             form2.ShowDialog();
    }
    
  • If you need multiple instances of Form2 or you need to initialize it multiple times, then you may get an instance of it like this:

    using (var form2 = Program.ServiceProvider.GetRequiredService<Form2>())
         form2.ShowDialog();
    
over 4 years ago · Santiago Trujillo Denunciar

0

In winforms the constructors of forms should be default constructors. You should set the values that you wanted to pass in the constructor as a property. During Form Loading you can check if the property has been set, and if not act as desired: use a default value, or warn the operator, or close the form.

Of course, at least one of the forms should decide which ICategory should be injected. For example the main form:

public class MainForm
{
    private ICategory Category {get; }

    public MainForm()
    {
        InitializeComponent();

        CategoryFactory factory = new CategoryFactory();
        this.Category = factory.Create();
    }

Later, the MainForm must Show Form1, for instance after a button click:

    private void ShowForm1()
    {
        using (Form1 form = new Form1())
        {
            form.Category = this.Category;
            var dlgResult = form.ShowDialog(this);
            if (dlgResult == DialogResult.Ok)
            {
                this.ProcessDialogResult(...);
            }
        }
    }

Form1:

class Form1 : ...
{
    public Form1()
    {
        InitializeComponent();  // subscribe to event form load
    }

    public ICategory Category {get; }

    public void FormLoading(object sender, ...)
    {
        // check if Category set, and report problems
        if (this.Category == null)
        {
            this.LogMissingCategory();
            this.WarnOperatorMissingCategory();
            this.Close();
        }
        ...
    } 

    private void ShowForm2()
    {
        using (Form2 form = new Form2())
        {
            form.Category = this.Category;
            var dlgResult = form.ShowDialog(this);
            if (dlgResult == DialogResult.Ok)
            {
                ...
            }
        }
    } 
}

If you have a lot of forms that need a Category, then apparently there is something like a CategoryForm:

class CategoryForm : Form
{
    public ICategory Category {get; }

    protected override void OnFormLoading(...)
    {
        // TODO check Category
        base.OnFormLoading(...);
    }
}

classs Form3 : CategoryForm {...}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda