From e66e721e98938d920078eb33d8c4e8da83135270 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Mon, 25 Nov 2024 20:48:20 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D1=87=D1=82=D0=B8=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Lab3/Extensions/DIExtension.cs | 2 +- Cop.Borovkov.Var3/Lab3/Program.cs | 4 +- .../Lab4/Extensions/DiExtensions.cs | 29 +++++++ Cop.Borovkov.Var3/Lab4/Forms/FormMain.cs | 86 ++++++++++++++----- .../Lab4/Implementations/PluginsConvention.cs | 37 +------- Cop.Borovkov.Var3/Lab4/Lab4.csproj | 6 ++ Cop.Borovkov.Var3/Lab4/Program.cs | 31 ++++++- Cop.Borovkov.Var3/Lab4/appsettings.json | 5 ++ 8 files changed, 141 insertions(+), 59 deletions(-) create mode 100644 Cop.Borovkov.Var3/Lab4/Extensions/DiExtensions.cs create mode 100644 Cop.Borovkov.Var3/Lab4/appsettings.json diff --git a/Cop.Borovkov.Var3/Lab3/Extensions/DIExtension.cs b/Cop.Borovkov.Var3/Lab3/Extensions/DIExtension.cs index 9199bff..f98f440 100644 --- a/Cop.Borovkov.Var3/Lab3/Extensions/DIExtension.cs +++ b/Cop.Borovkov.Var3/Lab3/Extensions/DIExtension.cs @@ -21,7 +21,7 @@ namespace Lab3.Extensions return services; } - public static IServiceCollection AddForms( + public static IServiceCollection AddLab3Forms( this IServiceCollection services) { services.AddScoped(); diff --git a/Cop.Borovkov.Var3/Lab3/Program.cs b/Cop.Borovkov.Var3/Lab3/Program.cs index 8cbc6aa..c9c3cbb 100644 --- a/Cop.Borovkov.Var3/Lab3/Program.cs +++ b/Cop.Borovkov.Var3/Lab3/Program.cs @@ -9,7 +9,7 @@ namespace Lab3 internal static class Program { /// - /// The main entry point for the application. + /// The main entry point for the application. /// [STAThread] static void Main() @@ -34,7 +34,7 @@ namespace Lab3 services.AddMapping(); - services.AddForms(); + services.AddLab3Forms(); }); } } diff --git a/Cop.Borovkov.Var3/Lab4/Extensions/DiExtensions.cs b/Cop.Borovkov.Var3/Lab4/Extensions/DiExtensions.cs new file mode 100644 index 0000000..ec824e1 --- /dev/null +++ b/Cop.Borovkov.Var3/Lab4/Extensions/DiExtensions.cs @@ -0,0 +1,29 @@ +using Lab4.Forms; +using Lab4.Implementations; +using Lab4.Interfaces; +using Microsoft.Extensions.DependencyInjection; + +namespace Lab4.Extensions +{ + public static class DiExtensions + { + public static IServiceCollection AddLab4Forms( + this IServiceCollection services) + { + services.AddScoped(); + + services.AddScoped(); + + return services; + } + + public static IServiceCollection AddScopes( + this IServiceCollection services) + { + services.AddScoped>(sp + => (type) => (sp.GetRequiredService(type) as IPluginsConvention)!); + + return services; + } + } +} diff --git a/Cop.Borovkov.Var3/Lab4/Forms/FormMain.cs b/Cop.Borovkov.Var3/Lab4/Forms/FormMain.cs index 83fc957..e2b5c8e 100644 --- a/Cop.Borovkov.Var3/Lab4/Forms/FormMain.cs +++ b/Cop.Borovkov.Var3/Lab4/Forms/FormMain.cs @@ -8,13 +8,13 @@ namespace Lab4.Forms private readonly Dictionary _plugins; private string _selectedPlugin; - private readonly Func _pluginsConventionFunc; + private readonly Func _getPluginObjectFunc; - public FormMain(Func pluginsConventionFunc) + public FormMain(Func getPluginObjectFunc) { InitializeComponent(); - _pluginsConventionFunc = pluginsConventionFunc; + _getPluginObjectFunc = getPluginObjectFunc; _plugins = LoadPlugins(); _selectedPlugin = string.Empty; @@ -24,20 +24,30 @@ namespace Lab4.Forms { Dictionary result = []; - List keys = ["Успеваемость"]; - result = keys.Select(k => KeyValuePair.Create(k, _pluginsConventionFunc(k))).ToDictionary(); - foreach (string key in keys) + var plurinType = typeof(IPluginsConvention); + + foreach (var type in AppDomain.CurrentDomain + .GetAssemblies() + .SelectMany(x => x.GetTypes()) + .Where(x => plurinType.IsAssignableFrom(x) && x != plurinType)) { + var plugin = _getPluginObjectFunc(type); + + string key = plugin.PluginName; + result[key] = plugin; + var item = new ToolStripMenuItem(key); - item.Click += (s, e) => _selectedPlugin = key; - - ControlsStripMenuItem.Container!.Add(item); + item.Click += (s, e) => + { + _selectedPlugin = key; + + panelControl.Controls.Clear(); + panelControl.Controls.Add(_plugins[key].GetControl); + }; + ControlsStripMenuItem.DropDownItems!.Add(item); } - - // TODO При выборе пункта меню получать UserControl и заполнять элемент panelControl этим контролом на всю площадь - // Пример: panelControl.Controls.Clear(); - // panelControl.Controls.Add(ctrl); - return []; + + return result; } private void FormMain_KeyDown(object sender, KeyEventArgs e) @@ -136,8 +146,19 @@ namespace Lab4.Forms private void CreateSimpleDoc() { - // TODO узнать где сохранять - if (_plugins[_selectedPlugin].CreateSimpleDocument(new PluginsConventionSaveDocument())) + using var saveFileDialog = new SaveFileDialog + { + Filter = "pdf|*.pdf" + }; + if (saveFileDialog.ShowDialog() != DialogResult.OK) + { + return; + } + + if (_plugins[_selectedPlugin].CreateSimpleDocument(new PluginsConventionSaveDocument() + { + FileName = saveFileDialog.FileName, + })) { _ = MessageBox.Show("Документ сохранен", "Создание документа", @@ -151,9 +172,21 @@ namespace Lab4.Forms } private void CreateTableDoc() { - // TODO узнать где сохранять + using var saveFileDialog = new SaveFileDialog + { + Filter = "xlsx|*.xlsx" + }; + + if (saveFileDialog.ShowDialog() != DialogResult.OK) + { + return; + } + if (_plugins[_selectedPlugin].CreateTableDocument(new - PluginsConventionSaveDocument())) + PluginsConventionSaveDocument() + { + FileName = saveFileDialog.FileName, + })) { _ = MessageBox.Show("Документ сохранен", "Создание документа", @@ -167,8 +200,20 @@ namespace Lab4.Forms } private void CreateChartDoc() { - // TODO узнать где сохранять - if (_plugins[_selectedPlugin].CreateChartDocument(new PluginsConventionSaveDocument())) + using var saveFileDialog = new SaveFileDialog + { + Filter = "docx|*.docx" + }; + + if (saveFileDialog.ShowDialog() != DialogResult.OK) + { + return; + } + + if (_plugins[_selectedPlugin].CreateChartDocument(new PluginsConventionSaveDocument() + { + FileName= saveFileDialog.FileName, + })) { _ = MessageBox.Show("Документ сохранен", "Создание документа", @@ -180,7 +225,6 @@ namespace Lab4.Forms _ = MessageBox.Show("Ошибка при создании документа", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - private void ThesaurusToolStripMenuItem_Click(object sender, EventArgs e) => ShowThesaurus(); private void AddElementToolStripMenuItem_Click(object sender, EventArgs e) => AddNewElement(); diff --git a/Cop.Borovkov.Var3/Lab4/Implementations/PluginsConvention.cs b/Cop.Borovkov.Var3/Lab4/Implementations/PluginsConvention.cs index 373b463..e2683c5 100644 --- a/Cop.Borovkov.Var3/Lab4/Implementations/PluginsConvention.cs +++ b/Cop.Borovkov.Var3/Lab4/Implementations/PluginsConvention.cs @@ -39,7 +39,7 @@ namespace Lab4.Implementations _simpleDocumentCreator = new(); } - public string PluginName => throw new NotImplementedException(); + public string PluginName => "Успеваемость"; public UserControl GetControl => _control; @@ -61,16 +61,6 @@ namespace Lab4.Implementations { try { - using var saveFileDialog = new SaveFileDialog - { - Filter = "docx|*.docx" - }; - - if (saveFileDialog.ShowDialog() != DialogResult.OK) - { - return false; - } - var data = _studentRepository.GetAsync().Result .GroupBy(s => s.EducationForm) .Select(s => new DataLine( @@ -82,7 +72,7 @@ namespace Lab4.Implementations _chartCreator.AddDiagram( new( - saveFileDialog.FileName, + saveDocument.FileName, "Поступления", "Диаграмма", @@ -103,16 +93,6 @@ namespace Lab4.Implementations { try { - using var saveFileDialog = new SaveFileDialog - { - Filter = "pdf|*.pdf" - }; - - if (saveFileDialog.ShowDialog() != DialogResult.OK) - { - return false; - } - var values = (_studentRepository.GetAsync()).Result .Select(s => s.StudentSessions .OrderBy(x => x.Number) @@ -132,7 +112,7 @@ namespace Lab4.Implementations _simpleDocumentCreator.SaveToPdf(new() { - FilePath = saveFileDialog.FileName, + FilePath = saveDocument.FileName, Title = "Сессии", Tables = [tables] }); @@ -149,18 +129,9 @@ namespace Lab4.Implementations { try { - using var saveFileDialog = new SaveFileDialog - { - Filter = "xlsx|*.xlsx" - }; - if (saveFileDialog.ShowDialog() != DialogResult.OK) - { - return false; - } - _tableCreator.SaveToExcel(new() { - FilePath = saveFileDialog.FileName, + FilePath = saveDocument.FileName, Title = "Студенты", Headers = [ diff --git a/Cop.Borovkov.Var3/Lab4/Lab4.csproj b/Cop.Borovkov.Var3/Lab4/Lab4.csproj index 3b383bd..13f756f 100644 --- a/Cop.Borovkov.Var3/Lab4/Lab4.csproj +++ b/Cop.Borovkov.Var3/Lab4/Lab4.csproj @@ -12,4 +12,10 @@ + + + Always + + + \ No newline at end of file diff --git a/Cop.Borovkov.Var3/Lab4/Program.cs b/Cop.Borovkov.Var3/Lab4/Program.cs index 17d2a90..e6464cf 100644 --- a/Cop.Borovkov.Var3/Lab4/Program.cs +++ b/Cop.Borovkov.Var3/Lab4/Program.cs @@ -1,11 +1,17 @@ +using Lab3.Extensions; +using Lab3.Forms; +using Lab4.Extensions; using Lab4.Forms; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; namespace Lab4 { internal static class Program { /// - /// The main entry point for the application. + /// The main entry point for the application. /// [STAThread] static void Main() @@ -13,7 +19,28 @@ namespace Lab4 // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormMain()); + + var app = CreateHostBuilder().Build(); + + Application.Run(app.Services.GetRequiredService()); + } + + static IHostBuilder CreateHostBuilder() + { + return Host.CreateDefaultBuilder() + .ConfigureAppConfiguration(c + => c.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)) + .ConfigureServices((context, services) => { + + services.ConfigureDAL(context.Configuration); + + services.AddMapping(); + + services.AddLab3Forms(); + services.AddLab4Forms(); + + services.AddScopes(); + }); } } } \ No newline at end of file diff --git a/Cop.Borovkov.Var3/Lab4/appsettings.json b/Cop.Borovkov.Var3/Lab4/appsettings.json new file mode 100644 index 0000000..751a2b0 --- /dev/null +++ b/Cop.Borovkov.Var3/Lab4/appsettings.json @@ -0,0 +1,5 @@ +{ + "ConnectionStrings": { + "COPDataBase": "Host=localhost;Username=postgres;Password=postgres;Database=COP" + } +}