Home > Mobile >  How to use dependency injection in WinForms
How to use dependency injection in WinForms

Time:12-25

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

CodePudding user response:

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, Form2 form2)
    {
        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();
    

CodePudding user response:

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 {...}
  • Related