Refactored service provider usage

- Remove service provider from WinForms altogether
- Moved service provider handling to processors
This commit is contained in:
tsommer committed 2022-07-17 15:15:55 +02:00
1 parent f49f6079d5
commit 36a8efb05d
5 files changed
+68 -57

No files matched your search

@@ -1,6 +1,4 @@
using DataModel.Database.Common;
using Microsoft.Extensions.DependencyInjection;
using Processor;
using Processor;
using UI_WinForms.Dialogs;
using UI_WinForms.Resources;
@@ -8,8 +6,6 @@ namespace UI_WinForms.Components;
public partial class SectionTree : UserControl
{
private readonly DataContext db;
public SectionTree()
{
this.InitializeComponent();
@@ -18,12 +14,6 @@ public partial class SectionTree : UserControl
if(Program.SERVICE_PROVIDER is null)
return;
// Get the DI context from the main form:
this.db = Program.SERVICE_PROVIDER.GetService<DataContext>()!;
// Dispose of the context when the control is disposed:
this.Disposed += (_, _) => this.db.Dispose();
// Create an image list from a resource:
var imgList = new ImageList();
imgList.ImageSize = new Size(45, 45);
@@ -46,7 +36,7 @@ public partial class SectionTree : UserControl
var treeNodes = new Dictionary<string, TreeNode>();
// Get the max. depth of the tree:
var maxDepth = await SectionProcessor.GetDepth(this.db);
var maxDepth = await SectionProcessor.GetDepth();
// Store nodes, where we cannot find the parents:
var missingParents = new List<TreeNode>();
@@ -54,7 +44,7 @@ public partial class SectionTree : UserControl
// Populate the tree view out of the database, layer by layer:
for (var i = 0; i <= maxDepth; i++)
{
await foreach (var section in SectionProcessor.LoadLayer(this.db, i))
foreach (var section in await SectionProcessor.LoadLayer(i))
{
// Create the tree node:
var node = new TreeNode
@@ -139,7 +129,7 @@ public partial class SectionTree : UserControl
var selectedNode = this.treeView.SelectedNode;
// Add the new section to the database:
var addedSection = await SectionProcessor.AddSection(this.db, result.Text, addRootNode ? null : selectedNode?.Name);
var addedSection = await SectionProcessor.AddSection(result.Text, addRootNode ? null : selectedNode?.Name);
addedSection.ProcessError();
if(!addedSection.Successful)
@@ -173,7 +163,7 @@ public partial class SectionTree : UserControl
// Get the number of children:
// (notice, that the node's name is its key)
var numberChildren = await SectionProcessor.NumberChildren(this.db, selectedNode.Name);
var numberChildren = await SectionProcessor.NumberChildren(selectedNode.Name);
// Ask the user, if he really wants to remove the section:
if(MessageBox.Show(numberChildren > 0 ? $"Are you sure, you want to remove the section '{selectedNode.Text}', its {numberChildren} children and so on?" : $"Are you sure, you want to remove the section '{selectedNode.Text}'?", "Remove section", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
@@ -181,7 +171,7 @@ public partial class SectionTree : UserControl
// Remove the section from the database:
// (notice, that the node's name is its key)
await SectionProcessor.RemoveSection(this.db, selectedNode.Name);
await SectionProcessor.RemoveSection(selectedNode.Name);
// Remove all nodes from the tree control:
this.treeView.Nodes.Clear();
@@ -190,7 +180,7 @@ public partial class SectionTree : UserControl
this.LoadNodes(this, EventArgs.Empty);
}
private void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
private async void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if(this.DesignMode)
return;
@@ -205,7 +195,7 @@ public partial class SectionTree : UserControl
if (selectedNode is not null)
{
// Get the section from the database:
var section = SectionProcessor.GetSection(this.db, selectedNode.Name);
var section = await SectionProcessor.GetSection(selectedNode.Name);
AppEvents.SectionChanged(section);
}
}
@@ -235,7 +225,7 @@ public partial class SectionTree : UserControl
return;
// Rename the section:
var alteredSection = await SectionProcessor.RenameSection(this.db, selectedNode.Name, result.Text);
var alteredSection = await SectionProcessor.RenameSection(selectedNode.Name, result.Text);
alteredSection.ProcessError();
if(!alteredSection.Successful)
@@ -1,6 +1,4 @@
using DataModel.Database;
using DataModel.Database.Common;
using Microsoft.Extensions.DependencyInjection;
using Processor;
using UI_WinForms.Dialogs;
@@ -8,8 +6,6 @@ namespace UI_WinForms.Components;
public partial class TextElements : UserControl
{
private readonly DataContext db;
private Section? currentSection;
public TextElements()
@@ -20,12 +16,6 @@ public partial class TextElements : UserControl
if(Program.SERVICE_PROVIDER is null)
return;
// Get the DI context from the main form:
this.db = Program.SERVICE_PROVIDER.GetService<DataContext>()!;
// Dispose of the context when the control is disposed:
this.Disposed += (_, _) => this.db.Dispose();
// When the section is changed, update this component:
AppEvents.WhenSectionChanged += async (sender, section) =>
{
@@ -36,7 +26,7 @@ public partial class TextElements : UserControl
if (this.currentSection is null)
return;
this.labelSectionPath.Text = await SectionProcessor.GetSectionPath(this.db, this.currentSection.DataKey);
this.labelSectionPath.Text = await SectionProcessor.GetSectionPath(this.currentSection.DataKey);
this.LoadTextElements();
};
}
@@ -48,11 +38,11 @@ public partial class TextElements : UserControl
return;
// Load the text elements:
var textElements = await TextElementProcessor.GetTextElements(this.db, this.currentSection);
var textElements = await TextElementProcessor.GetTextElements(this.currentSection);
// Update the list:
this.listTextElements.Items.Clear();
await foreach (var textElement in textElements)
foreach (var textElement in textElements)
{
var item = new ListViewItem(textElement.Name)
{
@@ -79,7 +69,7 @@ public partial class TextElements : UserControl
return;
// Add the text element to the database into the current section:
var newTextElement = await TextElementProcessor.AddTextElement(this.db, this.currentSection?.DataKey, result.Text);
var newTextElement = await TextElementProcessor.AddTextElement(this.currentSection?.DataKey, result.Text);
newTextElement.ProcessError();
if(!newTextElement.Successful)
+5 -7
View File
@@ -2,6 +2,7 @@ using DataModel;
using DataModel.Database.Common;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Processor;
namespace UI_WinForms;
@@ -34,9 +35,6 @@ internal static class Program
//
builder.ConfigureServices((hostContext, serviceCollection) =>
{
// The main form:
serviceCollection.AddSingleton<Main>();
// The database:
serviceCollection.AddDatabase(loader.DataFile, true);
});
@@ -50,15 +48,15 @@ internal static class Program
// Get a service provider:
SERVICE_PROVIDER = scope.ServiceProvider;
// Set the service provider to the processor:
ProcessorMeta.ServiceProvider = SERVICE_PROVIDER;
// Apply database migrations:
using (var database = SERVICE_PROVIDER.GetRequiredService<DataContext>())
Setup.PerformDataMigration(database).Wait();
// Create the main window:
var mainWindow = SERVICE_PROVIDER.GetService<Main>();
// Start the app:
Application.Run(mainWindow);
Application.Run(new Main());
}
}
}