Geni Lou

Geni Lou

  • NA
  • 101
  • 22.3k

Dependency Injection in WinForms

Feb 27 2021 3:06 AM
in Program.cs I add this code:
  1. services.AddDbContext<InventoryDbContext>(context => context.UseSqlServer(constr));  
  2. services.AddScoped<IClassService, ClassService>();  
  3. services.AddScoped<ICategoryService, CategoryService>();  
in MDIForm I add this code:
  1. private readonly IClassService classService;  
  2. private readonly ICategoryService categoryService;  
  3. public MdiForm(IClassificationService classificationService, ICategoryService categoryService){  
  4. this.classService = classService;  
  5. this.categoryService = categoryService;}  
then to show a category from MDI I do this code. But in order to add the classService from CategoryForm to Classification, it should be a part of CategoryForm.
 
from MDI Form to show CategoryForm:
  1. var categoryForm = new frmCategory(classService, categoryService);  
  2. categoryForm.ShowDialog();  
  3. from CategoryForm to show ClassForm:  
  4. var classForm = new frmClass(classService);  
  5. classForm .ShowDialog();  

Answers (3)