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 EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lab3.Database", "Lab3.Database\Lab3.Database.csproj", "{698DE9E8-7885-4F98-AFE3-9A9C6CD2FCF5}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lab3.Database", "Lab3.Database\Lab3.Database.csproj", "{698DE9E8-7885-4F98-AFE3-9A9C6CD2FCF5}"
EndProject 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 EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.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.ActiveCfg = Release|Any CPU
{FAE92C0B-0A2D-48B6-A55C-DE58A310CD58}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE 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.Plugins.Models;
using Lab4.Interfaces; using AutoMapper;
using Lab4.Models;
using Lab3.Database.Repository.Interfaces; using Lab3.Database.Repository.Interfaces;
using ComponentsLibrary.entities; using Cop.Borovkov.Var3.Components;
using CustomComponentsVar2;
using ComponentsLibrary; using ComponentsLibrary;
using System.Windows.Forms;
using ComponentsLibrary.entities;
using ComponentsLibrary.entities.enums; using ComponentsLibrary.entities.enums;
using Lab3.Database.DTO; using Lab3.Database.DTO;
using CustomComponentsVar2;
using Lab3.Forms; using Lab3.Forms;
using Lab3.Models; 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 public class PluginsConvention : IPluginsConvention
{ {
@ -24,26 +29,26 @@ namespace Lab4.Implementations
private readonly CustomExcelTable _tableCreator; private readonly CustomExcelTable _tableCreator;
private readonly ComponentDiagram _chartCreator; private readonly ComponentDiagram _chartCreator;
public PluginsConvention( public PluginsConvention()
IMapper mapper,
IStudentRepository studentRepository,
IEducationFormRepository educationFormRepository)
{ {
_mapper = mapper;
_studentRepository = studentRepository;
_educationFormRepository = educationFormRepository;
_control = new(); _control = new();
_chartCreator = new(); _chartCreator = new();
_tableCreator = new(); _tableCreator = new();
_simpleDocumentCreator = new(); _simpleDocumentCreator = new();
var serviceProvider = CreateServiceProvider();
_mapper = serviceProvider.GetRequiredService<IMapper>();
_studentRepository = serviceProvider.GetRequiredService<IStudentRepository>();
_educationFormRepository = serviceProvider.GetRequiredService<IEducationFormRepository>();
} }
public string PluginName => "Успеваемость"; public string PluginName => "Успеваемость";
public UserControl GetControl => _control; public UserControl GetControl => _control;
public PluginsConventionElement GetElement { public PluginsConventionElement GetElement
{
get get
{ {
var filds = _control.Selected.Split(); var filds = _control.Selected.Split();
@ -93,7 +98,7 @@ namespace Lab4.Implementations
{ {
try try
{ {
var values = (_studentRepository.GetAsync()).Result var values = _studentRepository.GetAsync().Result
.Select(s => s.StudentSessions .Select(s => s.StudentSessions
.OrderBy(x => x.Number) .OrderBy(x => x.Number)
.Select(x => x.Score.ToString()) .Select(x => x.Score.ToString())
@ -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.Forms;
using Lab4.Implementations;
using Lab4.Interfaces;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
namespace Lab4.Extensions namespace Lab4.Extensions
{ {
public static class DiExtensions public static class DiExtensions
{ {
public static IServiceCollection AddLab4Forms( public static IServiceCollection AddForms(
this IServiceCollection services) this IServiceCollection services)
{ {
services.AddScoped<FormMain>(); services.AddScoped<FormMain>();
return services; 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.Interfaces;
using Lab4.Models; using Lab4.Plugins.Models;
using Microsoft.Extensions.Options;
using System.Reflection;
namespace Lab4.Forms namespace Lab4.Forms
{ {
public partial class FormMain : Form public partial class FormMain : Form
{ {
private readonly Dictionary<string, IPluginsConvention> _plugins; 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(IOptions<PluginsConfigurations> pluginsConfigs)
public FormMain(Func<Type, IPluginsConvention> getPluginObjectFunc)
{ {
InitializeComponent(); InitializeComponent();
_getPluginObjectFunc = getPluginObjectFunc; _pluginsConfigurations = pluginsConfigs.Value;
_plugins = LoadPlugins(); _plugins = LoadPlugins();
_selectedPlugin = string.Empty;
} }
private Dictionary<string, IPluginsConvention> LoadPlugins() private Dictionary<string, IPluginsConvention> LoadPlugins()
{ {
Dictionary<string, IPluginsConvention> result = []; Dictionary<string, IPluginsConvention> result = [];
var plurinType = typeof(IPluginsConvention); var plurinInterface = typeof(IPluginsConvention);
foreach (var type in AppDomain.CurrentDomain foreach (var type in Directory
.GetAssemblies() .GetFiles(_pluginsConfigurations.FolderPath, "*.dll", SearchOption.AllDirectories)
.Select(Assembly.LoadFrom)
.SelectMany(x => x.GetTypes()) .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; if (plugin == null)
result[key] = plugin; {
continue;
}
IPluginsConvention pluginObject = (plugin as IPluginsConvention)!;
string key = pluginObject.PluginName;
result[key] = pluginObject;
var item = new ToolStripMenuItem(key); var item = new ToolStripMenuItem(key);
item.Click += (s, e) => item.Click += (s, e) =>

View File

@ -1,4 +1,4 @@
using Lab4.Models; using Lab4.Plugins.Models;
namespace Lab4.Interfaces namespace Lab4.Interfaces
{ {

View File

@ -9,7 +9,9 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <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>
<ItemGroup> <ItemGroup>

View File

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

View File

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

View File

@ -1,5 +1,3 @@
using Lab3.Extensions;
using Lab3.Forms;
using Lab4.Extensions; using Lab4.Extensions;
using Lab4.Forms; using Lab4.Forms;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
@ -31,15 +29,9 @@ namespace Lab4
.ConfigureAppConfiguration(c .ConfigureAppConfiguration(c
=> c.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)) => c.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true))
.ConfigureServices((context, services) => { .ConfigureServices((context, services) => {
services.AddForms();
services.ConfigureDAL(context.Configuration); services.Configure<PluginsConfigurations>(
context.Configuration.GetSection(nameof(PluginsConfigurations)));
services.AddMapping();
services.AddLab3Forms();
services.AddLab4Forms();
services.AddScopes();
}); });
} }
} }

View File

@ -1,4 +1,7 @@
{ {
"PluginsConfigurations": {
"FolderPath": "C:\\data\\Plugins"
},
"ConnectionStrings": { "ConnectionStrings": {
"COPDataBase": "Host=localhost;Username=postgres;Password=postgres;Database=COP" "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, // To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration. // see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize(); ApplicationConfiguration.Initialize();
Application.Run(new Form2()); Application.Run(new Form3());
} }
} }
} }

View File

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