8 laba complicated

This commit is contained in:
Timourka 2024-05-19 00:34:32 +05:00
parent d4f781674c
commit d7ac4d7627
19 changed files with 176 additions and 135 deletions

View File

@ -17,4 +17,8 @@
<ProjectReference Include="..\AutomobilePlantContracts\AutomobilePlantContracts.csproj" /> <ProjectReference Include="..\AutomobilePlantContracts\AutomobilePlantContracts.csproj" />
</ItemGroup> </ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="copy /Y &quot;$(TargetDir)*.dll&quot; &quot;$(SolutionDir)BusinessLogicExtensions\*.dll&quot;" />
</Target>
</Project> </Project>

View File

@ -0,0 +1,36 @@
using AutomobilePlantBusinessLogic.BusinessLogics;
using AutomobilePlantBusinessLogic.MailWorker;
using AutomobilePlantBusinessLogic.OfficePackage.Implements;
using AutomobilePlantBusinessLogic.OfficePackage;
using AutomobilePlantContracts.BusinessLogicsContracts;
using AutomobilePlantContracts.DI;
namespace AutomobilePlantBusinessLogic
{
public class BusinessLogicExtension : IBusinessLogicExtension
{
public int Priority => 0;
public void RegisterServices()
{
DependencyManager.Instance.RegisterType<IClientLogic, ClientLogic>();
DependencyManager.Instance.RegisterType<IComponentLogic, ComponentLogic>();
DependencyManager.Instance.RegisterType<IOrderLogic, OrderLogic>();
DependencyManager.Instance.RegisterType<ICarLogic, CarLogic>();
DependencyManager.Instance.RegisterType<IReportLogic, ReportLogic>();
DependencyManager.Instance.RegisterType<IImplementerLogic, ImplementerLogic>();
DependencyManager.Instance.RegisterType<IMessageInfoLogic, MessageInfoLogic>();
DependencyManager.Instance.RegisterType<IBackUpLogic, BackUpLogic>();
DependencyManager.Instance.RegisterType<IShopLogic, ShopLogic>();
DependencyManager.Instance.RegisterType<AbstractSaveToWord, SaveToWord>();
DependencyManager.Instance.RegisterType<AbstractSaveToExcel, SaveToExcel>();
DependencyManager.Instance.RegisterType<AbstractSaveToPdf, SaveToPdf>();
DependencyManager.Instance.RegisterType<AbstractMailWorker, MailKitWorker>(true);
DependencyManager.Instance.RegisterType<IWorkProcess, WorkModeling>();
}
}
}

View File

@ -3,18 +3,20 @@
[AttributeUsage(AttributeTargets.Property)] [AttributeUsage(AttributeTargets.Property)]
public class ColumnAttribute : Attribute public class ColumnAttribute : Attribute
{ {
public ColumnAttribute(string title = "", bool visible = true, int width = 0, GridViewAutoSize gridViewAutoSize = GridViewAutoSize.None, bool isUseAutoSize = false) public ColumnAttribute(string title = "", bool visible = true, int width = 0, GridViewAutoSize gridViewAutoSize = GridViewAutoSize.None, bool isUseAutoSize = false, string format = "")
{ {
Title = title; Title = title;
Visible = visible; Visible = visible;
Width = width; Width = width;
GridViewAutoSize = gridViewAutoSize; GridViewAutoSize = gridViewAutoSize;
IsUseAutoSize = isUseAutoSize; IsUseAutoSize = isUseAutoSize;
Format = format;
} }
public string Title { get; private set; } public string Title { get; private set; }
public bool Visible { get; private set; } public bool Visible { get; private set; }
public int Width { get; private set; } public int Width { get; private set; }
public GridViewAutoSize GridViewAutoSize { get; private set; } public GridViewAutoSize GridViewAutoSize { get; private set; }
public bool IsUseAutoSize { get; private set; } public bool IsUseAutoSize { get; private set; }
public string Format { get; private set; }
} }
} }

View File

@ -17,7 +17,6 @@ namespace AutomobilePlantContracts.BindingModels
public string Body { get; set; } = string.Empty; public string Body { get; set; } = string.Empty;
public bool IsReaded { get; set; } public bool IsReaded { get; set; }
public string? Reply { get; set; } public string? Reply { get; set; }
}
public int Id => throw new NotImplementedException(); public int Id => throw new NotImplementedException();
} }

View File

@ -25,13 +25,15 @@ namespace AutomobilePlantContracts.DI
/// </summary> /// </summary>
public static void InitDependency() public static void InitDependency()
{ {
var ext = ServiceProviderLoader.GetImplementationExtensions(); var extDB = ServiceProviderLoader.GetImplementationExtensions();
if (ext == null) var extBL = ServiceProviderLoader.GetBusinessLogicExtensions();
if (extDB == null || extBL == null)
{ {
throw new ArgumentNullException("Отсутствуют компоненты для загрузки зависимостей по модулям"); throw new ArgumentNullException("Отсутствуют компоненты для загрузки зависимостей по модулям");
} }
// регистрируем зависимости // регистрируем зависимости
ext.RegisterServices(); extDB.RegisterServices();
extBL.RegisterServices();
} }
/// <summary> /// <summary>

View File

@ -0,0 +1,9 @@
namespace AutomobilePlantContracts.DI
{
public interface IBusinessLogicExtension
{
public int Priority { get; }
public void RegisterServices();
}
}

View File

@ -40,6 +40,35 @@ namespace AutomobilePlantContracts.DI
return source; return source;
} }
public static IBusinessLogicExtension? GetBusinessLogicExtensions()
{
IBusinessLogicExtension? source = null;
var files = Directory.GetFiles(TryGetBusinessLogicExtensionsFolder(), "*.dll", SearchOption.AllDirectories);
foreach (var file in files.Distinct())
{
Assembly asm = Assembly.LoadFrom(file);
foreach (var t in asm.GetExportedTypes())
{
if (t.IsClass && typeof(IBusinessLogicExtension).IsAssignableFrom(t))
{
if (source == null)
{
source = (IBusinessLogicExtension)Activator.CreateInstance(t)!;
}
else
{
var newSource = (IBusinessLogicExtension)Activator.CreateInstance(t)!;
if (newSource.Priority > source.Priority)
{
source = newSource;
}
}
}
}
}
return source;
}
private static string TryGetImplementationExtensionsFolder() private static string TryGetImplementationExtensionsFolder()
{ {
var directory = new DirectoryInfo(Directory.GetCurrentDirectory()); var directory = new DirectoryInfo(Directory.GetCurrentDirectory());
@ -49,5 +78,15 @@ namespace AutomobilePlantContracts.DI
} }
return $"{directory?.FullName}\\ImplementationExtensions"; return $"{directory?.FullName}\\ImplementationExtensions";
} }
private static string TryGetBusinessLogicExtensionsFolder()
{
var directory = new DirectoryInfo(Directory.GetCurrentDirectory());
while (directory != null && !directory.GetDirectories("BusinessLogicExtensions", SearchOption.AllDirectories).Any(x => x.Name == "BusinessLogicExtensions"))
{
directory = directory.Parent;
}
return $"{directory?.FullName}\\BusinessLogicExtensions";
}
} }
} }

View File

@ -1,31 +1,29 @@
using AutomobilePlantDataModels.Models; using AutomobilePlantContracts.Attributes;
using System; using AutomobilePlantDataModels.Models;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AutomobilePlantContracts.ViewModels namespace AutomobilePlantContracts.ViewModels
{ {
public class ShopViewModel : IShopModel public class ShopViewModel : IShopModel
{ {
public int Id { get; set; } [Column(visible: false)]
public int Id { get; set; }
[DisplayName("Shop's name")] [Column(title: "Shop's name", width: 200)]
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
[DisplayName("Address")] [Column(title: "Address", width: 100)]
public string Address { get; set; } = string.Empty; public string Address { get; set; } = string.Empty;
[DisplayName("Maximum cars' count in shop")] [Column(title: "Maximum cars' count in shop", width: 100)]
public int MaxCountCars { get; set; } public int MaxCountCars { get; set; }
[DisplayName("Opening date")] [Column(title: "Opening date", width: 100, format: "d")]
public DateTime OpeningDate { get; set; } public DateTime OpeningDate { get; set; }
public Dictionary<int, (ICarModel, int)> ShopCars { get; set; } = new(); [Column(visible: false)]
public Dictionary<int, (ICarModel, int)> ShopCars { get; set; } = new();
public List<Tuple<string, int>>? CarAndCount { get; set; } = new(); [Column(visible: false)]
public List<Tuple<string, int>>? CarAndCount { get; set; } = new();
} }
} }

View File

@ -17,6 +17,7 @@ namespace AutomobilePlantDatabaseImplement
DependencyManager.Instance.RegisterType<IOrderStorage, OrderStorage>(); DependencyManager.Instance.RegisterType<IOrderStorage, OrderStorage>();
DependencyManager.Instance.RegisterType<ICarStorage, CarStorage>(); DependencyManager.Instance.RegisterType<ICarStorage, CarStorage>();
DependencyManager.Instance.RegisterType<IBackUpInfo, BackUpInfo>(); DependencyManager.Instance.RegisterType<IBackUpInfo, BackUpInfo>();
DependencyManager.Instance.RegisterType<IShopStorage, ShopStorage>();
} }
} }
} }

View File

@ -1,27 +1,28 @@
using AutomobilePlantContracts.BindingModels; using AutomobilePlantContracts.BindingModels;
using AutomobilePlantContracts.ViewModels; using AutomobilePlantContracts.ViewModels;
using AutomobilePlantDataModels.Models; using AutomobilePlantDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Linq; using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace AutomobilePlantDatabaseImplement.Models namespace AutomobilePlantDatabaseImplement.Models
{ {
public class Shop : IShopModel [DataContract]
{ public class Shop : IShopModel
public int Id { get; set; } {
[DataMember]
public int Id { get; set; }
[Required] [DataMember]
[Required]
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
[Required] [DataMember]
[Required]
public string Address { get; set; } = string.Empty; public string Address { get; set; } = string.Empty;
[Required] [DataMember]
[Required]
public DateTime OpeningDate { get; set; } public DateTime OpeningDate { get; set; }
[ForeignKey("ShopId")] [ForeignKey("ShopId")]
@ -29,7 +30,8 @@ namespace AutomobilePlantDatabaseImplement.Models
private Dictionary<int, (ICarModel, int)>? _shopCars = null; private Dictionary<int, (ICarModel, int)>? _shopCars = null;
[NotMapped] [DataMember]
[NotMapped]
public Dictionary<int, (ICarModel, int)> ShopCars public Dictionary<int, (ICarModel, int)> ShopCars
{ {
get get
@ -42,7 +44,8 @@ namespace AutomobilePlantDatabaseImplement.Models
} }
} }
[Required] [DataMember]
[Required]
public int MaxCountCars { get; set; } public int MaxCountCars { get; set; }
public static Shop Create(AutomobilePlantDatabase context, ShopBindingModel model) public static Shop Create(AutomobilePlantDatabase context, ShopBindingModel model)

View File

@ -17,6 +17,7 @@ namespace AutomobilePlantFileImplement
DependencyManager.Instance.RegisterType<IOrderStorage, OrderStorage>(); DependencyManager.Instance.RegisterType<IOrderStorage, OrderStorage>();
DependencyManager.Instance.RegisterType<ICarStorage, CarStorage>(); DependencyManager.Instance.RegisterType<ICarStorage, CarStorage>();
DependencyManager.Instance.RegisterType<IBackUpInfo, BackUpInfo>(); DependencyManager.Instance.RegisterType<IBackUpInfo, BackUpInfo>();
DependencyManager.Instance.RegisterType<IShopStorage, ShopStorage>();
} }
} }
} }

View File

@ -1,26 +1,29 @@
using AutomobilePlantContracts.BindingModels; using AutomobilePlantContracts.BindingModels;
using AutomobilePlantContracts.ViewModels; using AutomobilePlantContracts.ViewModels;
using AutomobilePlantDataModels.Models; using AutomobilePlantDataModels.Models;
using System; using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq; using System.Xml.Linq;
namespace AutomobilePlantFileImplement.Models namespace AutomobilePlantFileImplement.Models
{ {
public class Shop : IShopModel [DataContract]
{ public class Shop : IShopModel
public int Id { get; private set; } {
public string Name { get; private set; } = string.Empty; [DataMember]
public string Address { get; private set; } = string.Empty; public int Id { get; private set; }
public int MaxCountCars { get; private set; } [DataMember]
public DateTime OpeningDate { get; private set; } public string Name { get; private set; } = string.Empty;
[DataMember]
public string Address { get; private set; } = string.Empty;
[DataMember]
public int MaxCountCars { get; private set; }
[DataMember]
public DateTime OpeningDate { get; private set; }
public Dictionary<int, int> Cars { get; private set; } = new(); public Dictionary<int, int> Cars { get; private set; } = new();
private Dictionary<int, (ICarModel, int)>? _shopCars = null; private Dictionary<int, (ICarModel, int)>? _shopCars = null;
public Dictionary<int, (ICarModel, int)> ShopCars [DataMember]
public Dictionary<int, (ICarModel, int)> ShopCars
{ {
get get
{ {

View File

@ -17,6 +17,7 @@ namespace AutomobilePlantListImplement
DependencyManager.Instance.RegisterType<IOrderStorage, OrderStorage>(); DependencyManager.Instance.RegisterType<IOrderStorage, OrderStorage>();
DependencyManager.Instance.RegisterType<ICarStorage, CarStorage>(); DependencyManager.Instance.RegisterType<ICarStorage, CarStorage>();
DependencyManager.Instance.RegisterType<IBackUpInfo, BackUpInfo>(); DependencyManager.Instance.RegisterType<IBackUpInfo, BackUpInfo>();
DependencyManager.Instance.RegisterType<IShopStorage, ShopStorage>();
} }
} }
} }

View File

@ -38,6 +38,7 @@ namespace AutomobilePlantView
{ {
column.Width = columnAttr.Width; column.Width = columnAttr.Width;
} }
column.DefaultCellStyle.Format = columnAttr.Format;
} }
} }
} }

View File

@ -1,4 +1,5 @@
using AutomobilePlantContracts.BusinessLogicsContracts; using AutomobilePlantContracts.BusinessLogicsContracts;
using AutomobilePlantContracts.DI;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace AutomobilePlantView namespace AutomobilePlantView
@ -49,13 +50,10 @@ namespace AutomobilePlantView
{ {
if (dataGridView.SelectedRows.Count == 1) if (dataGridView.SelectedRows.Count == 1)
{ {
var service = Program.ServiceProvider?.GetService(typeof(FormMail)); var form = DependencyManager.Instance.Resolve<FormMail>();
if (service is FormMail form) form.MessageId = (string)(dataGridView.SelectedRows[0].Cells["MessageId"].Value);
{ form.ShowDialog();
form.MessageId = (string)(dataGridView.SelectedRows[0].Cells["MessageId"].Value); LoadData();
form.ShowDialog();
LoadData();
}
} }
} }
} }

View File

@ -87,7 +87,7 @@
// //
// toolStripMenuItemCatalogs // toolStripMenuItemCatalogs
// //
toolStripMenuItemCatalogs.DropDownItems.AddRange(new ToolStripItem[] { toolStripMenuItemComponents, toolStripMenuItemCars, clientsToolStripMenuItem, implementersToolStripMenuItem, mailsToolStripMenuItem }); toolStripMenuItemCatalogs.DropDownItems.AddRange(new ToolStripItem[] { toolStripMenuItemComponents, toolStripMenuItemCars, clientsToolStripMenuItem, shopsToolStripMenuItem, shopsSupplyToolStripMenuItem, sellCarsToolStripMenuItem, implementersToolStripMenuItem, mailsToolStripMenuItem });
toolStripMenuItemCatalogs.Name = "toolStripMenuItemCatalogs"; toolStripMenuItemCatalogs.Name = "toolStripMenuItemCatalogs";
toolStripMenuItemCatalogs.Size = new Size(65, 20); toolStripMenuItemCatalogs.Size = new Size(65, 20);
toolStripMenuItemCatalogs.Text = "Catalogs"; toolStripMenuItemCatalogs.Text = "Catalogs";

View File

@ -53,20 +53,14 @@ namespace AutomobilePlantView
private void shopsToolStripMenuItem_Click(object sender, EventArgs e) private void shopsToolStripMenuItem_Click(object sender, EventArgs e)
{ {
var service = Program.ServiceProvider?.GetService(typeof(FormShops)); var form = DependencyManager.Instance.Resolve<FormShops>();
if (service is FormShops form) form.ShowDialog();
{
form.ShowDialog();
}
} }
private void shopsSupplyToolStripMenuItem_Click(object sender, EventArgs e) private void shopsSupplyToolStripMenuItem_Click(object sender, EventArgs e)
{ {
var service = Program.ServiceProvider?.GetService(typeof(FormShopSupply)); var form = DependencyManager.Instance.Resolve<FormShopSupply>();
if (service is FormShopSupply form) form.ShowDialog();
{
form.ShowDialog();
}
} }
private void clientsToolStripMenuItem_Click(object sender, EventArgs e) private void clientsToolStripMenuItem_Click(object sender, EventArgs e)
@ -114,11 +108,8 @@ namespace AutomobilePlantView
private void sellCarsToolStripMenuItem_Click(object sender, EventArgs e) private void sellCarsToolStripMenuItem_Click(object sender, EventArgs e)
{ {
var service = Program.ServiceProvider?.GetService(typeof(FormShopSell)); var form = DependencyManager.Instance.Resolve<FormShopSell>();
if (service is FormShopSell form) form.ShowDialog();
{
form.ShowDialog();
}
} }
private void shopsListToolStripMenuItem_Click(object sender, EventArgs e) private void shopsListToolStripMenuItem_Click(object sender, EventArgs e)
@ -136,11 +127,8 @@ namespace AutomobilePlantView
private void storeCongestionToolStripMenuItem_Click(object sender, EventArgs e) private void storeCongestionToolStripMenuItem_Click(object sender, EventArgs e)
{ {
var service = Program.ServiceProvider?.GetService(typeof(FormReportShopCars)); var form = DependencyManager.Instance.Resolve<FormReportShopCars>();
if (service is FormReportShopCars form) form.ShowDialog();
{
form.ShowDialog();
}
} }
private void startWorkingsToolStripMenuItem_Click(object sender, EventArgs e) private void startWorkingsToolStripMenuItem_Click(object sender, EventArgs e)
@ -151,11 +139,8 @@ namespace AutomobilePlantView
private void listOdOrdersByDatesToolStripMenuItem_Click(object sender, EventArgs e) private void listOdOrdersByDatesToolStripMenuItem_Click(object sender, EventArgs e)
{ {
var service = Program.ServiceProvider?.GetService(typeof(FormReportDateOrders)); var form = DependencyManager.Instance.Resolve<FormReportDateOrders>();
if (service is FormReportDateOrders form) form.ShowDialog();
{
form.ShowDialog();
}
} }
private void createBackupToolStripMenuItem_Click(object sender, EventArgs e) private void createBackupToolStripMenuItem_Click(object sender, EventArgs e)

View File

@ -1,15 +1,7 @@
using AutomobilePlantContracts.BindingModels; using AutomobilePlantContracts.BindingModels;
using AutomobilePlantContracts.BusinessLogicsContracts; using AutomobilePlantContracts.BusinessLogicsContracts;
using AutomobilePlantContracts.DI;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
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 AutomobilePlantView namespace AutomobilePlantView
{ {
@ -33,18 +25,9 @@ namespace AutomobilePlantView
private void LoadData() private void LoadData()
{ {
try try
{ {
var list = _logic.ReadList(null); dataGridView.FillAndConfigGrid(_logic.ReadList(null));
_logger.LogInformation("Загрузка магазинов");
if (list != null)
{
dataGridView.DataSource = list;
dataGridView.Columns["Id"].Visible = false;
dataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["ShopCars"].Visible = false;
}
_logger.LogInformation("Загрузка магазинов");
} }
catch (Exception ex) catch (Exception ex)
@ -61,14 +44,10 @@ namespace AutomobilePlantView
private void buttonAdd_Click(object sender, EventArgs e) private void buttonAdd_Click(object sender, EventArgs e)
{ {
var service = Program.ServiceProvider?.GetService(typeof(FormShop)); var form = DependencyManager.Instance.Resolve<FormShop>();
if (form.ShowDialog() == DialogResult.OK)
if (service is FormShop form)
{ {
if (form.ShowDialog() == DialogResult.OK) LoadData();
{
LoadData();
}
} }
} }
@ -76,16 +55,11 @@ namespace AutomobilePlantView
{ {
if (dataGridView.SelectedRows.Count == 1) if (dataGridView.SelectedRows.Count == 1)
{ {
var service = Program.ServiceProvider?.GetService(typeof(FormShop)); var form = DependencyManager.Instance.Resolve<FormShop>();
form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
if (service is FormShop form) if (form.ShowDialog() == DialogResult.OK)
{ {
form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); LoadData();
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
} }
} }
} }

View File

@ -59,21 +59,6 @@ namespace AutomobilePlantView
option.SetMinimumLevel(LogLevel.Information); option.SetMinimumLevel(LogLevel.Information);
option.AddNLog("nlog.config"); option.AddNLog("nlog.config");
}); });
DependencyManager.Instance.RegisterType<IComponentLogic, ComponentLogic>();
DependencyManager.Instance.RegisterType<IOrderLogic, OrderLogic>();
DependencyManager.Instance.RegisterType<ICarLogic, CarLogic>();
DependencyManager.Instance.RegisterType<IReportLogic, ReportLogic>();
DependencyManager.Instance.RegisterType<IClientLogic, ClientLogic>();
DependencyManager.Instance.RegisterType<IBackUpLogic, BackUpLogic>();
DependencyManager.Instance.RegisterType<IImplementerLogic, ImplementerLogic>();
DependencyManager.Instance.RegisterType<IMessageInfoLogic, MessageInfoLogic>();
DependencyManager.Instance.RegisterType<IShopStorage, ShopStorage>();
DependencyManager.Instance.RegisterType<IShopLogic, ShopLogic>();
DependencyManager.Instance.RegisterType<AbstractSaveToWord, SaveToWord>();
DependencyManager.Instance.RegisterType<AbstractSaveToExcel, SaveToExcel>();
DependencyManager.Instance.RegisterType<AbstractSaveToPdf, SaveToPdf>();
DependencyManager.Instance.RegisterType<IWorkProcess, WorkModeling>();
DependencyManager.Instance.RegisterType<AbstractMailWorker, MailKitWorker>();
DependencyManager.Instance.RegisterType<FormMain>(); DependencyManager.Instance.RegisterType<FormMain>();
DependencyManager.Instance.RegisterType<FormComponent>(); DependencyManager.Instance.RegisterType<FormComponent>();
DependencyManager.Instance.RegisterType<FormComponents>(); DependencyManager.Instance.RegisterType<FormComponents>();