So verwenden Sie die Abhängigkeitsinjektion in WinFormsC#

Ein Treffpunkt für C#-Programmierer
Guest
 So verwenden Sie die Abhängigkeitsinjektion in WinForms

Post by Guest »

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

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post