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

+43 -14
View File
@@ -1,6 +1,7 @@
using DataModel.Database;
using DataModel.Database.Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Processor;
@@ -9,16 +10,24 @@ public static class SectionProcessor
/// <summary>
/// Load one layer of the tree by using the specified depth:
/// </summary>
public static IAsyncEnumerable<Section> LoadLayer(DataContext db, int depth)
public static async Task<List<Section>> LoadLayer(int depth)
{
return db.Sections.Where(n => n.Depth == depth).OrderBy(n => n.Id).AsAsyncEnumerable();
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
var sections = await db.Sections.Where(n => n.Depth == depth).OrderBy(n => n.Id).ToListAsync();
// Ensure, that the database loaded the section's parent:
foreach (var section in sections)
await db.Entry(section).Reference(n => n.Parent).LoadAsync();
return sections;
}
/// <summary>
/// Determine how deep the tree is.
/// </summary>
public static async ValueTask<int> GetDepth(DataContext db)
public static async ValueTask<int> GetDepth()
{
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
if(!await db.Sections.AnyAsync())
{
return 0;
@@ -30,8 +39,10 @@ public static class SectionProcessor
/// <summary>
/// Compute the new sections key and its depth, then store the section in the database.
/// </summary>
public static async Task<ProcessorResult<Section>> AddSection(DataContext db, string text, string? parentKey)
public static async Task<ProcessorResult<Section>> AddSection(string text, string? parentKey)
{
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Remove any whitespaces from the section name, regardless of how many e.g. spaces the user typed:
var key = string.Join('_', text.Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)).ToUpperInvariant();
@@ -97,24 +108,32 @@ public static class SectionProcessor
}
}
public static async Task RemoveSection(DataContext db, string selectedKey)
public static async Task RemoveSection(string selectedKey)
{
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
await SectionProcessor.RemoveOneSectionAndItsChildren(db, selectedKey);
await db.SaveChangesAsync();
}
private static async Task RemoveOneSectionAndItsChildren(DataContext db, string selectedKey)
{
// Remove the section from the database:
var section2Delete = await db.Sections.FirstOrDefaultAsync(n => n.DataKey == selectedKey);
if (section2Delete is null)
throw new ArgumentException($"The section with key {selectedKey} does not exist in the database.");
// Next, remove all children of the section, and the children's children, etc.:
var children = await db.Sections.Where(n => n.Parent == section2Delete).ToListAsync();
foreach (var child in children)
await RemoveSection(db, child.DataKey);
await SectionProcessor.RemoveOneSectionAndItsChildren(db, child.DataKey);
db.Sections.Remove(section2Delete);
await db.SaveChangesAsync();
}
public static async Task<int> NumberChildren(DataContext db, string selectedKey)
public static async Task<int> NumberChildren(string selectedKey)
{
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Read the section from the database:
var section = await db.Sections.FirstOrDefaultAsync(n => n.DataKey == selectedKey);
if (section is null)
@@ -123,8 +142,10 @@ public static class SectionProcessor
return await db.Sections.CountAsync(n => n.Parent == section);
}
public static async Task<ProcessorResult<Section>> RenameSection(DataContext db, string selectedNodeKey, string alteredName)
public static async Task<ProcessorResult<Section>> RenameSection(string selectedNodeKey, string alteredName)
{
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Read the section from the database:
var section = await db.Sections.FirstOrDefaultAsync(n => n.DataKey == selectedNodeKey);
if (section is null)
@@ -158,10 +179,18 @@ public static class SectionProcessor
}
}
public static Section GetSection(DataContext db, string sectionKey) => db.Sections.First(n => n.DataKey == sectionKey);
public static async Task<string> GetSectionPath(DataContext db, string sectionKey)
public static async Task<Section> GetSection(string sectionKey)
{
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
var section = await db.Sections.FirstAsync(n => n.DataKey == sectionKey);
await db.Entry(section).Reference(n => n.Parent).LoadAsync();
return section;
}
public static async Task<string> GetSectionPath(string sectionKey)
{
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
var section = await db.Sections.FirstAsync(n => n.DataKey == sectionKey);
// Ensure, that the database loaded the section's parent:
@@ -1,18 +1,22 @@
using DataModel.Database;
using DataModel.Database.Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Processor;
public static class TextElementProcessor
{
public static Task<IAsyncEnumerable<TextElement>> GetTextElements(DataContext db, Section section)
public static async Task<List<TextElement>> GetTextElements(Section section)
{
return Task.FromResult(db.TextElements.Where(n => n.Section == section).AsAsyncEnumerable());
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
return await db.TextElements.Where(n => n.Section == section).ToListAsync();
}
public static async Task<ProcessorResult<TextElement>> AddTextElement(DataContext db, string? currentSectionKey, string elementName)
public static async Task<ProcessorResult<TextElement>> AddTextElement(string? currentSectionKey, string elementName)
{
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
if(string.IsNullOrWhiteSpace(currentSectionKey))
throw new ArgumentOutOfRangeException(nameof(currentSectionKey));