This commit is contained in:
bekodeg 2024-12-09 20:23:39 +04:00
parent d9dea309c5
commit feb13d4d0f
18 changed files with 343 additions and 79 deletions

View File

@ -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

View File

@ -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<PluginsConvention>();
services.AddScoped<Func<Type, IPluginsConvention>>(sp
=> (type) => (sp.GetRequiredService(type) as IPluginsConvention)!);
return services;
}
}
}

View File

@ -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<IMapper>();
_studentRepository = serviceProvider.GetRequiredService<IStudentRepository>();
_educationFormRepository = serviceProvider.GetRequiredService<IEducationFormRepository>();
}
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<List<StudentViewModel>>(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;
}
}
}

View File

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-windows7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Lab3\Lab3.csproj" />
<ProjectReference Include="..\Lab4\Lab4.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,7 @@
namespace Lab4
{
public record PluginsConfigurations
{
public string FolderPath { get; set; } = string.Empty;
}
}

View File

@ -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<FormMain>();
return services;
}
public static IServiceCollection AddScopes(
this IServiceCollection services)
{
services.AddScoped<PluginsConvention>();
services.AddScoped<Func<Type, IPluginsConvention>>(sp
=> (type) => (sp.GetRequiredService(type) as IPluginsConvention)!);
return services;
}
}
}

View File

@ -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<string, IPluginsConvention> _plugins;
private string _selectedPlugin;
private readonly PluginsConfigurations _pluginsConfigurations;
private string _selectedPlugin = string.Empty;
private readonly Func<Type, IPluginsConvention> _getPluginObjectFunc;
public FormMain(Func<Type, IPluginsConvention> getPluginObjectFunc)
public FormMain(IOptions<PluginsConfigurations> pluginsConfigs)
{
InitializeComponent();
_getPluginObjectFunc = getPluginObjectFunc;
_pluginsConfigurations = pluginsConfigs.Value;
_plugins = LoadPlugins();
_selectedPlugin = string.Empty;
}
private Dictionary<string, IPluginsConvention> LoadPlugins()
{
Dictionary<string, IPluginsConvention> 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();

View File

@ -1,4 +1,4 @@
using Lab4.Models;
using Lab4.Plugins.Models;
namespace Lab4.Interfaces
{
@ -12,7 +12,7 @@ namespace Lab4.Interfaces
/// <summary>
/// Получение контрола для вывода набора данных
/// </summary>
UserControl GetControl { get; }
UserControl GetControl { get; }
/// <summary>
/// Получение элемента, выбранного в контроле
@ -25,7 +25,7 @@ namespace Lab4.Interfaces
/// <param name="element"></param>
/// <returns></returns>
Form GetForm(PluginsConventionElement? element = null);
/// <summary>
/// Получение формы для работы со справочником
/// </summary>

View File

@ -9,7 +9,9 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Lab3\Lab3.csproj" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.1" />
</ItemGroup>
<ItemGroup>

View File

@ -1,4 +1,4 @@
namespace Lab4.Models
namespace Lab4.Plugins.Models
{
public class PluginsConventionElement
{

View File

@ -1,4 +1,4 @@
namespace Lab4.Models
namespace Lab4.Plugins.Models
{
public class PluginsConventionSaveDocument
{

View File

@ -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<PluginsConfigurations>(
context.Configuration.GetSection(nameof(PluginsConfigurations)));
});
}
}
}

View File

@ -1,4 +1,7 @@
{
"PluginsConfigurations": {
"FolderPath": "C:\\data\\Plugins"
},
"ConnectionStrings": {
"COPDataBase": "Host=localhost;Username=postgres;Password=postgres;Database=COP"
}

View File

@ -0,0 +1,59 @@
namespace TestCustomComponents.Forms
{
partial class Form3
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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;
}
}

View File

@ -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;
}
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -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());
}
}
}

View File

@ -9,7 +9,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Cop.Borovkov.Var3\Cop.Borovkov.Var3.csproj" />
<ProjectReference Include="..\Lab4.Plugins\Lab4.Plugins.csproj" />
</ItemGroup>
</Project>