So verwenden Sie die Abhängigkeitsinjektion in WinForms

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: So verwenden Sie die Abhängigkeitsinjektion in WinForms

by Guest » 05 Jan 2025, 08:10

Wie definiere ich die Abhängigkeitsinjektion in Winforms C#?
Schnittstellen-ICategorie:

Code: Select all

public interface ICategory
{
void Save();
}
Klasse CategoryRepository:

Code: Select all

public class CategoryRepository : ICategory
{

private readonly ApplicationDbContext _context;

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

Code: Select all

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:

Code: Select all

public partial class Form2 : Form
{
private readonly  ICategury _ic;
public Form2(ICategury ic)
{
InitializeComponent();
_ic=ic
}
}
Problem?
  • Definition der Abhängigkeitsinjektion in Program.cs

    Code: Select all

    Application.Run(new Form1());
    
  • Definition der Abhängigkeitsinjektion zum Zeitpunkt des Form 2-Aufrufs

    Code: Select all

    Form2 frm= new Form2();
    frm.show();
    

Top