From feb13d4d0f8d52d0e7be51aa5800fd120d08618b Mon Sep 17 00:00:00 2001 From: bekodeg Date: Mon, 9 Dec 2024 20:23:39 +0400 Subject: [PATCH] + --- Cop.Borovkov.Var3/Cop.Borovkov.Var3.sln | 8 +- .../Lab4.Plugins/Extensions/DiExtensions.cs | 20 +++ .../Implementations/PluginsConvention.cs | 59 ++++++--- .../Lab4.Plugins/Lab4.Plugins.csproj | 13 ++ .../Configurations/PluginsConfigurations.cs | 7 + .../Lab4/Extensions/DiExtensions.cs | 15 +-- Cop.Borovkov.Var3/Lab4/Forms/FormMain.cs | 56 ++++---- .../Lab4/Interfaces/IPluginsConvention.cs | 6 +- Cop.Borovkov.Var3/Lab4/Lab4.csproj | 4 +- .../Lab4/Models/PluginsConventionElement.cs | 2 +- .../Models/PluginsConventionSaveDocument.cs | 2 +- Cop.Borovkov.Var3/Lab4/Program.cs | 16 +-- Cop.Borovkov.Var3/Lab4/appsettings.json | 3 + .../Forms/Form3.Designer.cs | 59 +++++++++ .../TestCustomComponents/Forms/Form3.cs | 28 ++++ .../TestCustomComponents/Forms/Form3.resx | 120 ++++++++++++++++++ .../TestCustomComponents/Program.cs | 2 +- .../TestCustomComponents.csproj | 2 +- 18 files changed, 343 insertions(+), 79 deletions(-) create mode 100644 Cop.Borovkov.Var3/Lab4.Plugins/Extensions/DiExtensions.cs rename Cop.Borovkov.Var3/{Lab4 => Lab4.Plugins}/Implementations/PluginsConvention.cs (80%) create mode 100644 Cop.Borovkov.Var3/Lab4.Plugins/Lab4.Plugins.csproj create mode 100644 Cop.Borovkov.Var3/Lab4/Configurations/PluginsConfigurations.cs create mode 100644 Cop.Borovkov.Var3/TestCustomComponents/Forms/Form3.Designer.cs create mode 100644 Cop.Borovkov.Var3/TestCustomComponents/Forms/Form3.cs create mode 100644 Cop.Borovkov.Var3/TestCustomComponents/Forms/Form3.resx diff --git a/Cop.Borovkov.Var3/Cop.Borovkov.Var3.sln b/Cop.Borovkov.Var3/Cop.Borovkov.Var3.sln index ac79906..f71bc15 100644 --- a/Cop.Borovkov.Var3/Cop.Borovkov.Var3.sln +++ b/Cop.Borovkov.Var3/Cop.Borovkov.Var3.sln @@ -11,7 +11,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lab3", "Lab3\Lab3.csproj", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lab3.Database", "Lab3.Database\Lab3.Database.csproj", "{698DE9E8-7885-4F98-AFE3-9A9C6CD2FCF5}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab4", "Lab4\Lab4.csproj", "{FAE92C0B-0A2D-48B6-A55C-DE58A310CD58}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lab4", "Lab4\Lab4.csproj", "{FAE92C0B-0A2D-48B6-A55C-DE58A310CD58}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab4.Plugins", "Lab4.Plugins\Lab4.Plugins.csproj", "{F30C8C78-98CB-4C5E-BEE8-125791A9D7AF}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {FAE92C0B-0A2D-48B6-A55C-DE58A310CD58}.Debug|Any CPU.Build.0 = Debug|Any CPU {FAE92C0B-0A2D-48B6-A55C-DE58A310CD58}.Release|Any CPU.ActiveCfg = Release|Any CPU {FAE92C0B-0A2D-48B6-A55C-DE58A310CD58}.Release|Any CPU.Build.0 = Release|Any CPU + {F30C8C78-98CB-4C5E-BEE8-125791A9D7AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F30C8C78-98CB-4C5E-BEE8-125791A9D7AF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F30C8C78-98CB-4C5E-BEE8-125791A9D7AF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F30C8C78-98CB-4C5E-BEE8-125791A9D7AF}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Cop.Borovkov.Var3/Lab4.Plugins/Extensions/DiExtensions.cs b/Cop.Borovkov.Var3/Lab4.Plugins/Extensions/DiExtensions.cs new file mode 100644 index 0000000..46e0253 --- /dev/null +++ b/Cop.Borovkov.Var3/Lab4.Plugins/Extensions/DiExtensions.cs @@ -0,0 +1,20 @@ +using Lab4.Interfaces; +using Lab4.Plugins.Implementations; +using Microsoft.Extensions.DependencyInjection; + +namespace Lab4.Plugins.Extensions +{ + public static class DiExtensions + { + public static IServiceCollection AddScopes( + this IServiceCollection services) + { + services.AddScoped(); + + services.AddScoped>(sp + => (type) => (sp.GetRequiredService(type) as IPluginsConvention)!); + + return services; + } + } +} diff --git a/Cop.Borovkov.Var3/Lab4/Implementations/PluginsConvention.cs b/Cop.Borovkov.Var3/Lab4.Plugins/Implementations/PluginsConvention.cs similarity index 80% rename from Cop.Borovkov.Var3/Lab4/Implementations/PluginsConvention.cs rename to Cop.Borovkov.Var3/Lab4.Plugins/Implementations/PluginsConvention.cs index 9f1253b..fe1997b 100644 --- a/Cop.Borovkov.Var3/Lab4/Implementations/PluginsConvention.cs +++ b/Cop.Borovkov.Var3/Lab4.Plugins/Implementations/PluginsConvention.cs @@ -1,17 +1,22 @@ -using Cop.Borovkov.Var3.Components; -using Lab4.Interfaces; -using Lab4.Models; +using Lab4.Plugins.Models; +using AutoMapper; using Lab3.Database.Repository.Interfaces; -using ComponentsLibrary.entities; +using Cop.Borovkov.Var3.Components; +using CustomComponentsVar2; using ComponentsLibrary; +using System.Windows.Forms; +using ComponentsLibrary.entities; using ComponentsLibrary.entities.enums; using Lab3.Database.DTO; -using CustomComponentsVar2; using Lab3.Forms; using Lab3.Models; -using AutoMapper; +using Lab4.Interfaces; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Configuration; +using Lab3.Extensions; +using Microsoft.Extensions.DependencyInjection; -namespace Lab4.Implementations +namespace Lab4.Plugins.Implementations { public class PluginsConvention : IPluginsConvention { @@ -24,26 +29,26 @@ namespace Lab4.Implementations private readonly CustomExcelTable _tableCreator; private readonly ComponentDiagram _chartCreator; - public PluginsConvention( - IMapper mapper, - IStudentRepository studentRepository, - IEducationFormRepository educationFormRepository) + public PluginsConvention() { - _mapper = mapper; - _studentRepository = studentRepository; - _educationFormRepository = educationFormRepository; - _control = new(); _chartCreator = new(); _tableCreator = new(); _simpleDocumentCreator = new(); + + var serviceProvider = CreateServiceProvider(); + + _mapper = serviceProvider.GetRequiredService(); + _studentRepository = serviceProvider.GetRequiredService(); + _educationFormRepository = serviceProvider.GetRequiredService(); } public string PluginName => "Успеваемость"; public UserControl GetControl => _control; - public PluginsConventionElement GetElement { + public PluginsConventionElement GetElement + { get { var filds = _control.Selected.Split(); @@ -93,7 +98,7 @@ namespace Lab4.Implementations { try { - var values = (_studentRepository.GetAsync()).Result + var values = _studentRepository.GetAsync().Result .Select(s => s.StudentSessions .OrderBy(x => x.Number) .Select(x => x.Score.ToString()) @@ -171,10 +176,10 @@ namespace Lab4.Implementations } } - public Form GetForm(PluginsConventionElement? element = null) + public Form GetForm(PluginsConventionElement? element = null) => new CreateForm(_studentRepository, _educationFormRepository, element?.Id); - public Form GetThesaurus() + public Form GetThesaurus() => new CatalogForm(_educationFormRepository); public async void ReloadData() @@ -182,7 +187,7 @@ namespace Lab4.Implementations try { var students = _mapper.Map>(await _studentRepository.GetAsync()); - + _control.FillValues( students.Select(s => string.Join(" ", [ @@ -200,5 +205,19 @@ namespace Lab4.Implementations } } + + static IServiceProvider CreateServiceProvider() + { + var builder = Host.CreateDefaultBuilder() + .ConfigureAppConfiguration(c + => c.AddJsonFile("appsettings.plugins.json", optional: true, reloadOnChange: true)) + .ConfigureServices((context, services) => { + services.ConfigureDAL(context.Configuration); + services.AddMapping(); + services.AddLab3Forms(); + }); + + return builder.Build().Services; + } } } diff --git a/Cop.Borovkov.Var3/Lab4.Plugins/Lab4.Plugins.csproj b/Cop.Borovkov.Var3/Lab4.Plugins/Lab4.Plugins.csproj new file mode 100644 index 0000000..5b7c256 --- /dev/null +++ b/Cop.Borovkov.Var3/Lab4.Plugins/Lab4.Plugins.csproj @@ -0,0 +1,13 @@ + + + + net8.0-windows7.0 + enable + enable + + + + + + + diff --git a/Cop.Borovkov.Var3/Lab4/Configurations/PluginsConfigurations.cs b/Cop.Borovkov.Var3/Lab4/Configurations/PluginsConfigurations.cs new file mode 100644 index 0000000..93085bb --- /dev/null +++ b/Cop.Borovkov.Var3/Lab4/Configurations/PluginsConfigurations.cs @@ -0,0 +1,7 @@ +namespace Lab4 +{ + public record PluginsConfigurations + { + public string FolderPath { get; set; } = string.Empty; + } +} diff --git a/Cop.Borovkov.Var3/Lab4/Extensions/DiExtensions.cs b/Cop.Borovkov.Var3/Lab4/Extensions/DiExtensions.cs index 129b87a..58f150c 100644 --- a/Cop.Borovkov.Var3/Lab4/Extensions/DiExtensions.cs +++ b/Cop.Borovkov.Var3/Lab4/Extensions/DiExtensions.cs @@ -1,29 +1,16 @@ using Lab4.Forms; -using Lab4.Implementations; -using Lab4.Interfaces; using Microsoft.Extensions.DependencyInjection; namespace Lab4.Extensions { public static class DiExtensions { - public static IServiceCollection AddLab4Forms( + public static IServiceCollection AddForms( this IServiceCollection services) { services.AddScoped(); return services; } - - public static IServiceCollection AddScopes( - this IServiceCollection services) - { - services.AddScoped(); - - 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 bc5a9c1..b01a42d 100644 --- a/Cop.Borovkov.Var3/Lab4/Forms/FormMain.cs +++ b/Cop.Borovkov.Var3/Lab4/Forms/FormMain.cs @@ -1,40 +1,48 @@ using Lab4.Interfaces; -using Lab4.Models; +using Lab4.Plugins.Models; +using Microsoft.Extensions.Options; +using System.Reflection; namespace Lab4.Forms { public partial class FormMain : Form { private readonly Dictionary _plugins; - private string _selectedPlugin; + private readonly PluginsConfigurations _pluginsConfigurations; + private string _selectedPlugin = string.Empty; - private readonly Func _getPluginObjectFunc; - - public FormMain(Func getPluginObjectFunc) + public FormMain(IOptions pluginsConfigs) { InitializeComponent(); - - _getPluginObjectFunc = getPluginObjectFunc; + + _pluginsConfigurations = pluginsConfigs.Value; _plugins = LoadPlugins(); - _selectedPlugin = string.Empty; } private Dictionary LoadPlugins() { Dictionary result = []; - var plurinType = typeof(IPluginsConvention); + var plurinInterface = typeof(IPluginsConvention); - foreach (var type in AppDomain.CurrentDomain - .GetAssemblies() + foreach (var type in Directory + .GetFiles(_pluginsConfigurations.FolderPath, "*.dll", SearchOption.AllDirectories) + .Select(Assembly.LoadFrom) .SelectMany(x => x.GetTypes()) - .Where(x => plurinType.IsAssignableFrom(x) && x != plurinType)) + .Where(x => plurinInterface.IsAssignableFrom(x) && !x.IsInterface)) { - var plugin = _getPluginObjectFunc(type); + var plugin = type.GetConstructor([])?.Invoke([]); - string key = plugin.PluginName; - result[key] = plugin; + if (plugin == null) + { + continue; + } + + IPluginsConvention pluginObject = (plugin as IPluginsConvention)!; + + string key = pluginObject.PluginName; + result[key] = pluginObject; var item = new ToolStripMenuItem(key); item.Click += (s, e) => @@ -127,7 +135,7 @@ namespace Lab4.Forms _plugins[_selectedPlugin].ReloadData(); } } - + private void DeleteElement() { if (MessageBox.Show("Удалить выбранный элемент", "Удаление", @@ -177,7 +185,7 @@ namespace Lab4.Forms _ = MessageBox.Show("Ошибка при создании документа", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - + private void CreateTableDoc() { using var saveFileDialog = new SaveFileDialog @@ -206,7 +214,7 @@ namespace Lab4.Forms _ = MessageBox.Show("Ошибка при создании документа", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - + private void CreateChartDoc() { using var saveFileDialog = new SaveFileDialog @@ -221,7 +229,7 @@ namespace Lab4.Forms if (_plugins[_selectedPlugin].CreateChartDocument(new PluginsConventionSaveDocument() { - FileName= saveFileDialog.FileName, + FileName = saveFileDialog.FileName, })) { _ = MessageBox.Show("Документ сохранен", @@ -234,15 +242,15 @@ 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(); - + private void UpdElementToolStripMenuItem_Click(object sender, EventArgs e) => UpdateElement(); - + private void DelElementToolStripMenuItem_Click(object sender, EventArgs e) => DeleteElement(); - + private void SimpleDocToolStripMenuItem_Click(object sender, EventArgs e) => CreateSimpleDoc(); private void TableDocToolStripMenuItem_Click(object sender, EventArgs e) => CreateTableDoc(); diff --git a/Cop.Borovkov.Var3/Lab4/Interfaces/IPluginsConvention.cs b/Cop.Borovkov.Var3/Lab4/Interfaces/IPluginsConvention.cs index 43b757b..b59da6b 100644 --- a/Cop.Borovkov.Var3/Lab4/Interfaces/IPluginsConvention.cs +++ b/Cop.Borovkov.Var3/Lab4/Interfaces/IPluginsConvention.cs @@ -1,4 +1,4 @@ -using Lab4.Models; +using Lab4.Plugins.Models; namespace Lab4.Interfaces { @@ -12,7 +12,7 @@ namespace Lab4.Interfaces /// /// Получение контрола для вывода набора данных /// - UserControl GetControl { get; } + UserControl GetControl { get; } /// /// Получение элемента, выбранного в контроле @@ -25,7 +25,7 @@ namespace Lab4.Interfaces /// /// Form GetForm(PluginsConventionElement? element = null); - + /// /// Получение формы для работы со справочником /// diff --git a/Cop.Borovkov.Var3/Lab4/Lab4.csproj b/Cop.Borovkov.Var3/Lab4/Lab4.csproj index 13f756f..7231068 100644 --- a/Cop.Borovkov.Var3/Lab4/Lab4.csproj +++ b/Cop.Borovkov.Var3/Lab4/Lab4.csproj @@ -9,7 +9,9 @@ - + + + diff --git a/Cop.Borovkov.Var3/Lab4/Models/PluginsConventionElement.cs b/Cop.Borovkov.Var3/Lab4/Models/PluginsConventionElement.cs index c5b9472..4f1670e 100644 --- a/Cop.Borovkov.Var3/Lab4/Models/PluginsConventionElement.cs +++ b/Cop.Borovkov.Var3/Lab4/Models/PluginsConventionElement.cs @@ -1,4 +1,4 @@ -namespace Lab4.Models +namespace Lab4.Plugins.Models { public class PluginsConventionElement { diff --git a/Cop.Borovkov.Var3/Lab4/Models/PluginsConventionSaveDocument.cs b/Cop.Borovkov.Var3/Lab4/Models/PluginsConventionSaveDocument.cs index d5e4fc7..aac33c1 100644 --- a/Cop.Borovkov.Var3/Lab4/Models/PluginsConventionSaveDocument.cs +++ b/Cop.Borovkov.Var3/Lab4/Models/PluginsConventionSaveDocument.cs @@ -1,4 +1,4 @@ -namespace Lab4.Models +namespace Lab4.Plugins.Models { public class PluginsConventionSaveDocument { diff --git a/Cop.Borovkov.Var3/Lab4/Program.cs b/Cop.Borovkov.Var3/Lab4/Program.cs index e6464cf..0df2534 100644 --- a/Cop.Borovkov.Var3/Lab4/Program.cs +++ b/Cop.Borovkov.Var3/Lab4/Program.cs @@ -1,5 +1,3 @@ -using Lab3.Extensions; -using Lab3.Forms; using Lab4.Extensions; using Lab4.Forms; using Microsoft.Extensions.Configuration; @@ -31,16 +29,10 @@ namespace Lab4 .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(); - }); + services.AddForms(); + services.Configure( + context.Configuration.GetSection(nameof(PluginsConfigurations))); + }); } } } \ No newline at end of file diff --git a/Cop.Borovkov.Var3/Lab4/appsettings.json b/Cop.Borovkov.Var3/Lab4/appsettings.json index 751a2b0..9ce9598 100644 --- a/Cop.Borovkov.Var3/Lab4/appsettings.json +++ b/Cop.Borovkov.Var3/Lab4/appsettings.json @@ -1,4 +1,7 @@ { + "PluginsConfigurations": { + "FolderPath": "C:\\data\\Plugins" + }, "ConnectionStrings": { "COPDataBase": "Host=localhost;Username=postgres;Password=postgres;Database=COP" } diff --git a/Cop.Borovkov.Var3/TestCustomComponents/Forms/Form3.Designer.cs b/Cop.Borovkov.Var3/TestCustomComponents/Forms/Form3.Designer.cs new file mode 100644 index 0000000..c8a9ddf --- /dev/null +++ b/Cop.Borovkov.Var3/TestCustomComponents/Forms/Form3.Designer.cs @@ -0,0 +1,59 @@ +namespace TestCustomComponents.Forms +{ + partial class Form3 + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + button1 = new Button(); + SuspendLayout(); + // + // button1 + // + button1.Location = new Point(103, 83); + button1.Name = "button1"; + button1.Size = new Size(94, 29); + button1.TabIndex = 0; + button1.Text = "button1"; + button1.UseVisualStyleBackColor = true; + button1.Click += button1_Click; + // + // Form3 + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(button1); + Name = "Form3"; + Text = "Form3"; + ResumeLayout(false); + } + + #endregion + + private Button button1; + } +} \ No newline at end of file diff --git a/Cop.Borovkov.Var3/TestCustomComponents/Forms/Form3.cs b/Cop.Borovkov.Var3/TestCustomComponents/Forms/Form3.cs new file mode 100644 index 0000000..5dc6cb1 --- /dev/null +++ b/Cop.Borovkov.Var3/TestCustomComponents/Forms/Form3.cs @@ -0,0 +1,28 @@ +using Lab4.Plugins.Implementations; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace TestCustomComponents.Forms +{ + public partial class Form3 : Form + { + public Form3() + { + InitializeComponent(); + } + + private void button1_Click(object sender, EventArgs e) + { + var test = new PluginsConvention(); + + int a = 1; + } + } +} diff --git a/Cop.Borovkov.Var3/TestCustomComponents/Forms/Form3.resx b/Cop.Borovkov.Var3/TestCustomComponents/Forms/Form3.resx new file mode 100644 index 0000000..8b2ff64 --- /dev/null +++ b/Cop.Borovkov.Var3/TestCustomComponents/Forms/Form3.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Cop.Borovkov.Var3/TestCustomComponents/Program.cs b/Cop.Borovkov.Var3/TestCustomComponents/Program.cs index 13d3c8d..d570b34 100644 --- a/Cop.Borovkov.Var3/TestCustomComponents/Program.cs +++ b/Cop.Borovkov.Var3/TestCustomComponents/Program.cs @@ -13,7 +13,7 @@ namespace TestCustomComponents // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new Form2()); + Application.Run(new Form3()); } } } \ No newline at end of file diff --git a/Cop.Borovkov.Var3/TestCustomComponents/TestCustomComponents.csproj b/Cop.Borovkov.Var3/TestCustomComponents/TestCustomComponents.csproj index 588393f..5158f8d 100644 --- a/Cop.Borovkov.Var3/TestCustomComponents/TestCustomComponents.csproj +++ b/Cop.Borovkov.Var3/TestCustomComponents/TestCustomComponents.csproj @@ -9,7 +9,7 @@ - + \ No newline at end of file