From c554b0dbecaa2e6ad31922e0384e18e195187cc8 Mon Sep 17 00:00:00 2001 From: Pavel_Sorokin Date: Fri, 28 Apr 2023 16:13:32 +0400 Subject: [PATCH] =?UTF-8?q?8=20=D0=9B=D0=B0=D0=B1=D0=B0=20=D0=B8=D0=B7=20?= =?UTF-8?q?=D0=B2=D0=B8=D0=B4=D0=B5=D0=BE=20(=D0=B2=D1=80=D0=BE=D0=B4?= =?UTF-8?q?=D0=B5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + Shipyard/Shipyard/DataGridViewExtension.cs | 51 +++++++++ Shipyard/Shipyard/FormClients.cs | 8 +- Shipyard/Shipyard/FormDetails.cs | 21 ++-- Shipyard/Shipyard/FormImplementers.cs | 21 ++-- Shipyard/Shipyard/FormMain.cs | 61 ++++------- Shipyard/Shipyard/FormShip.cs | 55 +++++----- Shipyard/Shipyard/FormShips.cs | 21 ++-- Shipyard/Shipyard/Program.cs | 82 +++++++------- Shipyard/ShipyardBusinessLogic/BackUpLogic.cs | 103 ++++++++++++++++++ .../Attributes/ColumnAttribute.cs | 31 ++++++ .../Attributes/GridViewAutoSize.cs | 27 +++++ .../BindingModels/BackUpSaveBinidngModel.cs | 13 +++ .../BindingModels/MessageInfoBindingModel.cs | 1 + .../BusinessLogicsContracts/IBackUpLogic.cs | 14 +++ .../ShipyardContracts/DI/DependencyManager.cs | 66 +++++++++++ .../DI/IDependencyContainer.cs | 40 +++++++ .../DI/IImplementationExtension.cs | 17 +++ .../DI/ServiceDependencyContainer.cs | 62 +++++++++++ .../DI/ServiceProviderLoader.cs | 55 ++++++++++ .../StoragesContracts/IBackUpInfo.cs | 15 +++ .../ViewModels/ClientViewModel.cs | 10 +- .../ViewModels/MessageInfoViewModel.cs | 2 + .../Implements/BackUpInfo.cs | 32 ++++++ .../Models/Message.cs | 1 + .../Models/IMessageInfoModel.cs | 2 +- .../ShipyardFileImplement/Models/Message.cs | 1 + .../Implements/BackUpInfo.cs | 22 ++++ .../ListImplementationExtension.cs | 28 +++++ .../ShipyardListImplement»/Models/Message.cs | 1 + .../ShipyardListImplement.csproj | 4 + 31 files changed, 701 insertions(+), 168 deletions(-) create mode 100644 Shipyard/Shipyard/DataGridViewExtension.cs create mode 100644 Shipyard/ShipyardBusinessLogic/BackUpLogic.cs create mode 100644 Shipyard/ShipyardContracts/Attributes/ColumnAttribute.cs create mode 100644 Shipyard/ShipyardContracts/Attributes/GridViewAutoSize.cs create mode 100644 Shipyard/ShipyardContracts/BindingModels/BackUpSaveBinidngModel.cs create mode 100644 Shipyard/ShipyardContracts/BusinessLogicsContracts/IBackUpLogic.cs create mode 100644 Shipyard/ShipyardContracts/DI/DependencyManager.cs create mode 100644 Shipyard/ShipyardContracts/DI/IDependencyContainer.cs create mode 100644 Shipyard/ShipyardContracts/DI/IImplementationExtension.cs create mode 100644 Shipyard/ShipyardContracts/DI/ServiceDependencyContainer.cs create mode 100644 Shipyard/ShipyardContracts/DI/ServiceProviderLoader.cs create mode 100644 Shipyard/ShipyardContracts/StoragesContracts/IBackUpInfo.cs create mode 100644 Shipyard/ShipyardDataBaseImplement/Implements/BackUpInfo.cs create mode 100644 Shipyard/ShipyardListImplement»/Implements/BackUpInfo.cs create mode 100644 Shipyard/ShipyardListImplement»/ListImplementationExtension.cs diff --git a/.gitignore b/.gitignore index ca1c7a3..a743e0a 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,8 @@ # User-specific files (MonoDevelop/Xamarin Studio) *.userprefs +ImplementationExtensions + # Mono auto generated files mono_crash.* diff --git a/Shipyard/Shipyard/DataGridViewExtension.cs b/Shipyard/Shipyard/DataGridViewExtension.cs new file mode 100644 index 0000000..00fdab4 --- /dev/null +++ b/Shipyard/Shipyard/DataGridViewExtension.cs @@ -0,0 +1,51 @@ +using ShipyardContracts.Attributes; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShipyardView +{ + internal static class DataGridViewExtension + { + public static void FillAndConfigGrid(this DataGridView grid, List? data) + { + if (data == null) + { + return; + } + grid.DataSource = data; + + var type = typeof(T); + var properties = type.GetProperties(); + foreach (DataGridViewColumn column in grid.Columns) + { + var property = properties.FirstOrDefault(x => x.Name == column.Name); + if (property == null) + { + throw new InvalidOperationException($"В типе {type.Name} не найдено свойство с именем {column.Name}"); + } + var attribute = property.GetCustomAttributes(typeof(ColumnAttribute), true)?.SingleOrDefault(); + if (attribute == null) + { + throw new InvalidOperationException($"Не найден атрибут типа ColumnAttribute для свойства {property.Name}"); + } + // ищем нужный нам атрибут + if (attribute is ColumnAttribute columnAttr) + { + column.HeaderText = columnAttr.Title; + column.Visible = columnAttr.Visible; + if (columnAttr.IsUseAutoSize) + { + column.AutoSizeMode = (DataGridViewAutoSizeColumnMode)Enum.Parse(typeof(DataGridViewAutoSizeColumnMode), columnAttr.GridViewAutoSize.ToString()); + } + else + { + column.Width = columnAttr.Width; + } + } + } + } + } +} diff --git a/Shipyard/Shipyard/FormClients.cs b/Shipyard/Shipyard/FormClients.cs index 46f8a93..99e19d9 100644 --- a/Shipyard/Shipyard/FormClients.cs +++ b/Shipyard/Shipyard/FormClients.cs @@ -35,13 +35,7 @@ namespace ShipyardView { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["ClientFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } + dataGridView.FillAndConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Загрузка клиентов"); } catch (Exception ex) diff --git a/Shipyard/Shipyard/FormDetails.cs b/Shipyard/Shipyard/FormDetails.cs index 63faa96..b73a912 100644 --- a/Shipyard/Shipyard/FormDetails.cs +++ b/Shipyard/Shipyard/FormDetails.cs @@ -2,6 +2,7 @@ using Microsoft.VisualBasic.Logging; using ShipyardContracts.BindingModels; using ShipyardContracts.BusinessLogicsContracts; +using ShipyardContracts.DI; using System; using System.Collections.Generic; using System.ComponentModel; @@ -51,13 +52,10 @@ namespace ShipyardView private void ButtonAdd_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormDetail)); - if (service is FormDetail form) + var form = DependencyManager.Instance.Resolve(); + if (form.ShowDialog() == DialogResult.OK) { - if (form.ShowDialog() == DialogResult.OK) - { - LoadData(); - } + LoadData(); } } @@ -65,14 +63,11 @@ namespace ShipyardView { if (dataGridView.SelectedRows.Count == 1) { - var service = Program.ServiceProvider?.GetService(typeof(FormDetail)); - if (service is FormDetail form) + var form = DependencyManager.Instance.Resolve(); + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) { - form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - if (form.ShowDialog() == DialogResult.OK) - { - LoadData(); - } + LoadData(); } } } diff --git a/Shipyard/Shipyard/FormImplementers.cs b/Shipyard/Shipyard/FormImplementers.cs index 83903a1..d5b33fc 100644 --- a/Shipyard/Shipyard/FormImplementers.cs +++ b/Shipyard/Shipyard/FormImplementers.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Logging; using ShipyardContracts.BindingModels; using ShipyardContracts.BusinessLogicsContracts; +using ShipyardContracts.DI; using System; using System.Collections.Generic; using System.ComponentModel; @@ -53,13 +54,10 @@ namespace ShipyardView private void ButtonAdd_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormImplementer)); - if (service is FormImplementer form) + var form = DependencyManager.Instance.Resolve(); + if (form.ShowDialog() == DialogResult.OK) { - if (form.ShowDialog() == DialogResult.OK) - { - LoadData(); - } + LoadData(); } } @@ -67,14 +65,11 @@ namespace ShipyardView { if (dataGridView.SelectedRows.Count == 1) { - var service = Program.ServiceProvider?.GetService(typeof(FormImplementer)); - if (service is FormImplementer form) + var form = DependencyManager.Instance.Resolve(); + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) { - form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - if (form.ShowDialog() == DialogResult.OK) - { - LoadData(); - } + LoadData(); } } } diff --git a/Shipyard/Shipyard/FormMain.cs b/Shipyard/Shipyard/FormMain.cs index 40e39df..9e54e17 100644 --- a/Shipyard/Shipyard/FormMain.cs +++ b/Shipyard/Shipyard/FormMain.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Logging; using ShipyardContracts.BindingModels; using ShipyardContracts.BusinessLogicsContracts; +using ShipyardContracts.DI; using ShipyardDataModels.Enums; using System; using System.Collections.Generic; @@ -57,30 +58,21 @@ namespace ShipyardView private void ДеталиToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormDetails)); - if (service is FormDetails form) - { - form.ShowDialog(); - } + var form = DependencyManager.Instance.Resolve(); + form.ShowDialog(); } private void КораблиToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormShips)); - if (service is FormShips form) - { - form.ShowDialog(); - } + var form = DependencyManager.Instance.Resolve(); + form.ShowDialog(); } private void ButtonCreateOrder_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormCreateOrder)); - if (service is FormCreateOrder form) - { - form.ShowDialog(); - LoadData(); - } + var form = DependencyManager.Instance.Resolve(); + form.ShowDialog(); + LoadData(); } private void ButtonTakeOrderInWork_Click(object sender, EventArgs e) @@ -172,44 +164,32 @@ namespace ShipyardView private void shipDetailsToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormReportShipDetails)); - if (service is FormReportShipDetails form) - { - form.ShowDialog(); - } + var form = DependencyManager.Instance.Resolve(); + form.ShowDialog(); } private void ordersToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormReportOrders)); - if (service is FormReportOrders form) - { - form.ShowDialog(); - } + var form = DependencyManager.Instance.Resolve(); + form.ShowDialog(); } private void КлиентыToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormClients)); - if (service is FormClients form) - { - form.ShowDialog(); - } + var form = DependencyManager.Instance.Resolve(); + form.ShowDialog(); } private void ИсполнителиToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormImplementers)); - if (service is FormImplementers form) - { - form.ShowDialog(); - } + var form = DependencyManager.Instance.Resolve(); + form.ShowDialog(); } private void ЗапускРаботToolStripMenuItem_Click(object sender, EventArgs e) { _workProcess.DoWork(( - Program.ServiceProvider?.GetService(typeof(IImplementerLogic)) as IImplementerLogic)!, + DependencyManager.Instance.Resolve())!, _orderLogic); MessageBox.Show("Передано на работу","произведен запуск", MessageBoxButtons.OK, MessageBoxIcon.Information); @@ -217,11 +197,8 @@ namespace ShipyardView private void письмаToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormMails)); - if (service is FormMails form) - { - form.ShowDialog(); - } + var form = DependencyManager.Instance.Resolve(); + form.ShowDialog(); } } } diff --git a/Shipyard/Shipyard/FormShip.cs b/Shipyard/Shipyard/FormShip.cs index 0bf1f1f..52c5e68 100644 --- a/Shipyard/Shipyard/FormShip.cs +++ b/Shipyard/Shipyard/FormShip.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Logging; using ShipyardContracts.BindingModels; using ShipyardContracts.BusinessLogicsContracts; +using ShipyardContracts.DI; using ShipyardContracts.SearchModels; using ShipyardDataModels.Models; using System; @@ -81,26 +82,23 @@ namespace ShipyardView private void ButtonAdd_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormShipDetail)); - if (service is FormShipDetail form) + var form = DependencyManager.Instance.Resolve(); + if (form.ShowDialog() == DialogResult.OK) { - if (form.ShowDialog() == DialogResult.OK) + if (form.DetailModel == null) { - if (form.DetailModel == null) - { - return; - } - _logger.LogInformation("Добавление нового детали:{ DetailName} - { Count}", form.DetailModel.DetailName, form.Count); - if (_shipDetails.ContainsKey(form.Id)) - { - _shipDetails[form.Id] = (form.DetailModel,form.Count); - } - else - { - _shipDetails.Add(form.Id, (form.DetailModel,form.Count)); - } - LoadData(); + return; } + _logger.LogInformation("Добавление нового детали:{ DetailName} - { Count}", form.DetailModel.DetailName, form.Count); + if (_shipDetails.ContainsKey(form.Id)) + { + _shipDetails[form.Id] = (form.DetailModel,form.Count); + } + else + { + _shipDetails.Add(form.Id, (form.DetailModel,form.Count)); + } + LoadData(); } } @@ -108,22 +106,19 @@ namespace ShipyardView { if (dataGridView.SelectedRows.Count == 1) { - var service = Program.ServiceProvider?.GetService(typeof(FormShipDetail)); - if (service is FormShipDetail form) + var form = DependencyManager.Instance.Resolve(); + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value); + form.Id = id; + form.Count = _shipDetails[id].Item2; + if (form.ShowDialog() == DialogResult.OK) { - int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value); - form.Id = id; - form.Count = _shipDetails[id].Item2; - if (form.ShowDialog() == DialogResult.OK) + if (form.DetailModel == null) { - if (form.DetailModel == null) - { - return; - } - _logger.LogInformation("Изменение детали: { DetailName} - { Count}", form.DetailModel.DetailName, form.Count); - _shipDetails[form.Id] = (form.DetailModel,form.Count); - LoadData(); + return; } + _logger.LogInformation("Изменение детали: { DetailName} - { Count}", form.DetailModel.DetailName, form.Count); + _shipDetails[form.Id] = (form.DetailModel,form.Count); + LoadData(); } } } diff --git a/Shipyard/Shipyard/FormShips.cs b/Shipyard/Shipyard/FormShips.cs index 4b04caf..c2bc1a0 100644 --- a/Shipyard/Shipyard/FormShips.cs +++ b/Shipyard/Shipyard/FormShips.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Logging; using ShipyardContracts.BindingModels; using ShipyardContracts.BusinessLogicsContracts; +using ShipyardContracts.DI; using System; using System.Collections.Generic; using System.ComponentModel; @@ -51,13 +52,10 @@ namespace ShipyardView private void ButtonAdd_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormShip)); - if (service is FormShip form) + var form = DependencyManager.Instance.Resolve(); + if (form.ShowDialog() == DialogResult.OK) { - if (form.ShowDialog() == DialogResult.OK) - { - LoadData(); - } + LoadData(); } } @@ -65,14 +63,11 @@ namespace ShipyardView { if (dataGridView.SelectedRows.Count == 1) { - var service = Program.ServiceProvider?.GetService(typeof(FormShip)); - if (service is FormShip form) + var form = DependencyManager.Instance.Resolve(); + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) { - form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - if (form.ShowDialog() == DialogResult.OK) - { - LoadData(); - } + LoadData(); } } } diff --git a/Shipyard/Shipyard/Program.cs b/Shipyard/Shipyard/Program.cs index bc00fda..3f4e070 100644 --- a/Shipyard/Shipyard/Program.cs +++ b/Shipyard/Shipyard/Program.cs @@ -5,17 +5,15 @@ using ShipyardBusinessLogic.BusinessLogics; using ShipyardBusinessLogic.OfficePackage.Implements; using ShipyardBusinessLogic.OfficePackage; using ShipyardContracts.BusinessLogicsContracts; -using ShipyardContracts.StoragesContracts; -using ShipyardDataBaseImplement.Implements; using ShipyardBusinessLogic.MailWorker; using ShipyardContracts.BindingModels; +using ShipyardBusinessLogic; +using ShipyardContracts.DI; namespace ShipyardView { internal static class Program { - private static ServiceProvider? _serviceProvider; - public static ServiceProvider? ServiceProvider => _serviceProvider; /// /// The main entry point for the application. /// @@ -26,12 +24,10 @@ namespace ShipyardView // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - var services = new ServiceCollection(); - ConfigureServices(services); - _serviceProvider = services.BuildServiceProvider(); + InitDependency(); try { - var mailSender = _serviceProvider.GetService(); + var mailSender = DependencyManager.Instance.Resolve(); mailSender?.MailConfig(new MailConfigBindingModel { MailLogin = System.Configuration.ConfigurationManager.AppSettings["MailLogin"] ?? string.Empty, @@ -46,56 +42,52 @@ namespace ShipyardView } catch (Exception ex) { - var logger = _serviceProvider.GetService(); + var logger = DependencyManager.Instance.Resolve(); logger?.LogError(ex, " "); } - Application.Run(_serviceProvider.GetRequiredService()); + Application.Run(DependencyManager.Instance.Resolve()); } - private static void ConfigureServices(ServiceCollection services) + private static void InitDependency() { - services.AddLogging(option => + DependencyManager.InitDependency(); + + DependencyManager.Instance.AddLogging(option => { option.SetMinimumLevel(LogLevel.Information); option.AddNLog("nlog.config"); }); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(true); + DependencyManager.Instance.RegisterType(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddSingleton(); - - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); } - private static void MailCheck(object obj) => ServiceProvider?.GetService()?.MailCheck(); + private static void MailCheck(object obj) => DependencyManager.Instance?.Resolve()?.MailCheck(); } diff --git a/Shipyard/ShipyardBusinessLogic/BackUpLogic.cs b/Shipyard/ShipyardBusinessLogic/BackUpLogic.cs new file mode 100644 index 0000000..fec2360 --- /dev/null +++ b/Shipyard/ShipyardBusinessLogic/BackUpLogic.cs @@ -0,0 +1,103 @@ +using Microsoft.Extensions.Logging; +using ShipyardContracts.BindingModels; +using ShipyardContracts.BusinessLogicsContracts; +using ShipyardContracts.StoragesContracts; +using ShipyardDataModels; +using System; +using System.Collections.Generic; +using System.IO.Compression; +using System.Linq; +using System.Reflection; +using System.Runtime.Serialization.Json; +using System.Text; +using System.Threading.Tasks; + +namespace ShipyardBusinessLogic +{ + public class BackUpLogic : IBackUpLogic + { + private readonly ILogger _logger; + + private readonly IBackUpInfo _backUpInfo; + + public BackUpLogic(ILogger logger, IBackUpInfo backUpInfo) + { + _logger = logger; + _backUpInfo = backUpInfo; + } + + public void CreateBackUp(BackUpSaveBinidngModel model) + { + if (_backUpInfo == null) + { + return; + } + try + { + _logger.LogDebug("Clear folder"); + // зачистка папки и удаление старого архива + var dirInfo = new DirectoryInfo(model.FolderName); + if (dirInfo.Exists) + { + foreach (var file in dirInfo.GetFiles()) + { + file.Delete(); + } + } + _logger.LogDebug("Delete archive"); + string fileName = $"{model.FolderName}.zip"; + if (File.Exists(fileName)) + { + File.Delete(fileName); + } + // берем метод для сохранения + _logger.LogDebug("Get assembly"); + var typeIId = typeof(IId); + var assembly = typeIId.Assembly; + if (assembly == null) + { + throw new ArgumentNullException("Сборка не найдена", nameof(assembly)); + } + var types = assembly.GetTypes(); + var method = GetType().GetMethod("SaveToFile", BindingFlags.NonPublic | BindingFlags.Instance); + _logger.LogDebug("Find {count} types", types.Length); + foreach (var type in types) + { + if (type.IsInterface && type.GetInterface(typeIId.Name) != null) + { + var modelType = _backUpInfo.GetTypeByModelInterface(type.Name); + if (modelType == null) + { + throw new InvalidOperationException($"Не найден класс-модель для {type.Name}"); + } + _logger.LogDebug("Call SaveToFile method for {name} type", type.Name); + // вызываем метод на выполнение + method?.MakeGenericMethod(modelType).Invoke(this, new object[] { model.FolderName }); + } + } + _logger.LogDebug("Create zip and remove folder"); + // архивируем + ZipFile.CreateFromDirectory(model.FolderName, fileName); + // удаляем папку + dirInfo.Delete(true); + } + catch (Exception) + { + throw; + } + } + + private void SaveToFile(string folderName) where T : class, new() + { + var records = _backUpInfo.GetList(); + if (records == null) + { + _logger.LogWarning("{type} type get null list", typeof(T).Name); + return; + } + var jsonFormatter = new DataContractJsonSerializer(typeof(List)); + using var fs = new FileStream(string.Format("{0}/{1}.json", folderName, typeof(T).Name), FileMode.OpenOrCreate); + jsonFormatter.WriteObject(fs, records); + } + } +} diff --git a/Shipyard/ShipyardContracts/Attributes/ColumnAttribute.cs b/Shipyard/ShipyardContracts/Attributes/ColumnAttribute.cs new file mode 100644 index 0000000..b799c0f --- /dev/null +++ b/Shipyard/ShipyardContracts/Attributes/ColumnAttribute.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShipyardContracts.Attributes +{ + [AttributeUsage(AttributeTargets.Property)] + public class ColumnAttribute : Attribute + { + public string Title { get; private set; } + + public bool Visible { get; private set; } + + public int Width { get; private set; } + + public GridViewAutoSize GridViewAutoSize { get; private set; } + + public bool IsUseAutoSize { get; private set; } + + public ColumnAttribute(string title = "", bool visible = true, int width = 0, GridViewAutoSize gridViewAutoSize = GridViewAutoSize.None, bool isUseAutoSize = false) + { + Title = title; + Visible = visible; + Width = width; + GridViewAutoSize = gridViewAutoSize; + IsUseAutoSize = isUseAutoSize; + } + } +} diff --git a/Shipyard/ShipyardContracts/Attributes/GridViewAutoSize.cs b/Shipyard/ShipyardContracts/Attributes/GridViewAutoSize.cs new file mode 100644 index 0000000..e27b49e --- /dev/null +++ b/Shipyard/ShipyardContracts/Attributes/GridViewAutoSize.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShipyardContracts.Attributes +{ + public enum GridViewAutoSize + { + NotSet = 0, + + None = 1, + + ColumnHeader = 2, + + AllCellsExceptHeader = 4, + + AllCells = 6, + + DisplayedCellsExceptHeader = 8, + + DisplayedCells = 10, + + Fill = 16 + } +} diff --git a/Shipyard/ShipyardContracts/BindingModels/BackUpSaveBinidngModel.cs b/Shipyard/ShipyardContracts/BindingModels/BackUpSaveBinidngModel.cs new file mode 100644 index 0000000..e8b0b8d --- /dev/null +++ b/Shipyard/ShipyardContracts/BindingModels/BackUpSaveBinidngModel.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShipyardContracts.BindingModels +{ + public class BackUpSaveBinidngModel + { + public string FolderName { get; set; } = string.Empty; + } +} diff --git a/Shipyard/ShipyardContracts/BindingModels/MessageInfoBindingModel.cs b/Shipyard/ShipyardContracts/BindingModels/MessageInfoBindingModel.cs index 8a0e5f1..ce103c7 100644 --- a/Shipyard/ShipyardContracts/BindingModels/MessageInfoBindingModel.cs +++ b/Shipyard/ShipyardContracts/BindingModels/MessageInfoBindingModel.cs @@ -20,5 +20,6 @@ namespace ShipyardContracts.BindingModels public string Body { get; set; } = string.Empty; public DateTime DateDelivery { get; set; } + public int Id => throw new NotImplementedException(); } } diff --git a/Shipyard/ShipyardContracts/BusinessLogicsContracts/IBackUpLogic.cs b/Shipyard/ShipyardContracts/BusinessLogicsContracts/IBackUpLogic.cs new file mode 100644 index 0000000..26bd2a2 --- /dev/null +++ b/Shipyard/ShipyardContracts/BusinessLogicsContracts/IBackUpLogic.cs @@ -0,0 +1,14 @@ +using ShipyardContracts.BindingModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShipyardContracts.BusinessLogicsContracts +{ + public interface IBackUpLogic + { + void CreateBackUp(BackUpSaveBinidngModel model); + } +} diff --git a/Shipyard/ShipyardContracts/DI/DependencyManager.cs b/Shipyard/ShipyardContracts/DI/DependencyManager.cs new file mode 100644 index 0000000..6e34b3f --- /dev/null +++ b/Shipyard/ShipyardContracts/DI/DependencyManager.cs @@ -0,0 +1,66 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShipyardContracts.DI +{ + public class DependencyManager + { + private readonly IDependencyContainer _dependencyManager; + + private static DependencyManager? _manager; + + private static readonly object _locjObject = new(); + + private DependencyManager() + { + _dependencyManager = new ServiceDependencyContainer(); + } + + public static DependencyManager Instance { get { if (_manager == null) { lock (_locjObject) { _manager = new DependencyManager(); } } return _manager; } } + + /// + /// Иницализация библиотек, в которых идут установки зависомстей + /// + public static void InitDependency() + { + var ext = ServiceProviderLoader.GetImplementationExtensions(); + if (ext == null) + { + throw new ArgumentNullException("Отсутствуют компоненты для загрузки зависимостей по модулям"); + } + // регистрируем зависимости + ext.RegisterServices(); + } + + /// + /// Регистрация логгера + /// + /// + public void AddLogging(Action configure) => _dependencyManager.AddLogging(configure); + + /// + /// Добавление зависимости + /// + /// + /// + public void RegisterType(bool isSingle = false) where U : class, T where T : class => _dependencyManager.RegisterType(isSingle); + + /// + /// Добавление зависимости + /// + /// + /// + public void RegisterType(bool isSingle = false) where T : class => _dependencyManager.RegisterType(isSingle); + + /// + /// Получение класса со всеми зависмостями + /// + /// + /// + public T Resolve() => _dependencyManager.Resolve(); + } +} diff --git a/Shipyard/ShipyardContracts/DI/IDependencyContainer.cs b/Shipyard/ShipyardContracts/DI/IDependencyContainer.cs new file mode 100644 index 0000000..9b98d42 --- /dev/null +++ b/Shipyard/ShipyardContracts/DI/IDependencyContainer.cs @@ -0,0 +1,40 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShipyardContracts.DI +{ + public interface IDependencyContainer + { + /// + /// Регистрация логгера + /// + /// + void AddLogging(Action configure); + + /// + /// Добавление зависимости + /// + /// + /// + /// + void RegisterType(bool isSingle) where U : class, T where T : class; + + /// + /// Добавление зависимости + /// + /// + /// + void RegisterType(bool isSingle) where T : class; + + /// + /// Получение класса со всеми зависмостями + /// + /// + /// + T Resolve(); + } +} diff --git a/Shipyard/ShipyardContracts/DI/IImplementationExtension.cs b/Shipyard/ShipyardContracts/DI/IImplementationExtension.cs new file mode 100644 index 0000000..cedf2a4 --- /dev/null +++ b/Shipyard/ShipyardContracts/DI/IImplementationExtension.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShipyardContracts.DI +{ + public interface IImplementationExtension + { + public int Priority { get; } + /// + /// Регистрация сервисов + /// + public void RegisterServices(); + } +} diff --git a/Shipyard/ShipyardContracts/DI/ServiceDependencyContainer.cs b/Shipyard/ShipyardContracts/DI/ServiceDependencyContainer.cs new file mode 100644 index 0000000..f910fc8 --- /dev/null +++ b/Shipyard/ShipyardContracts/DI/ServiceDependencyContainer.cs @@ -0,0 +1,62 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShipyardContracts.DI +{ + public class ServiceDependencyContainer : IDependencyContainer + { + private ServiceProvider? _serviceProvider; + + private readonly ServiceCollection _serviceCollection; + + public ServiceDependencyContainer() + { + _serviceCollection = new ServiceCollection(); + } + + public void AddLogging(Action configure) + { + _serviceCollection.AddLogging(configure); + } + + public void RegisterType(bool isSingle) where U : class, T where T : class + { + if (isSingle) + { + _serviceCollection.AddSingleton(); + } + else + { + _serviceCollection.AddTransient(); + } + _serviceProvider = null; + } + + public void RegisterType(bool isSingle) where T : class + { + if (isSingle) + { + _serviceCollection.AddSingleton(); + } + else + { + _serviceCollection.AddTransient(); + } + _serviceProvider = null; + } + + public T Resolve() + { + if (_serviceProvider == null) + { + _serviceProvider = _serviceCollection.BuildServiceProvider(); + } + return _serviceProvider.GetService()!; + } + } +} diff --git a/Shipyard/ShipyardContracts/DI/ServiceProviderLoader.cs b/Shipyard/ShipyardContracts/DI/ServiceProviderLoader.cs new file mode 100644 index 0000000..3b57b4a --- /dev/null +++ b/Shipyard/ShipyardContracts/DI/ServiceProviderLoader.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace ShipyardContracts.DI +{ + public class ServiceProviderLoader + { + /// + /// Загрузка всех классов-реализаций IImplementationExtension + /// + /// + public static IImplementationExtension? GetImplementationExtensions() + { + IImplementationExtension? source = null; + var files = Directory.GetFiles(TryGetImplementationExtensionsFolder(), "*.dll", SearchOption.AllDirectories); + foreach (var file in files.Distinct()) + { + Assembly asm = Assembly.LoadFrom(file); + foreach (var t in asm.GetExportedTypes()) + { + if (t.IsClass && typeof(IImplementationExtension).IsAssignableFrom(t)) + { + if (source == null) + { + source = (IImplementationExtension)Activator.CreateInstance(t)!; + } + else + { + var newSource = (IImplementationExtension)Activator.CreateInstance(t)!; + if (newSource.Priority > source.Priority) + { + source = newSource; + } + } + } + } + } + return source; + } + + private static string TryGetImplementationExtensionsFolder() + { + var directory = new DirectoryInfo(Directory.GetCurrentDirectory()); + while (directory != null && !directory.GetDirectories("ImplementationExtensions", SearchOption.AllDirectories).Any(x => x.Name == "ImplementationExtensions")) + { + directory = directory.Parent; + } + return $"{directory?.FullName}\\ImplementationExtensions"; + } + } +} diff --git a/Shipyard/ShipyardContracts/StoragesContracts/IBackUpInfo.cs b/Shipyard/ShipyardContracts/StoragesContracts/IBackUpInfo.cs new file mode 100644 index 0000000..2083567 --- /dev/null +++ b/Shipyard/ShipyardContracts/StoragesContracts/IBackUpInfo.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShipyardContracts.StoragesContracts +{ + public interface IBackUpInfo + { + List? GetList() where T : class, new(); + + Type? GetTypeByModelInterface(string modelInterfaceName); + } +} diff --git a/Shipyard/ShipyardContracts/ViewModels/ClientViewModel.cs b/Shipyard/ShipyardContracts/ViewModels/ClientViewModel.cs index ba52890..2e45bd6 100644 --- a/Shipyard/ShipyardContracts/ViewModels/ClientViewModel.cs +++ b/Shipyard/ShipyardContracts/ViewModels/ClientViewModel.cs @@ -1,4 +1,5 @@ -using ShipyardDataModels.Models; +using ShipyardContracts.Attributes; +using ShipyardDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel; @@ -10,15 +11,16 @@ namespace ShipyardContracts.ViewModels { public class ClientViewModel:IClientModel { + [Column(visible: false)] public int Id { get; set; } - [DisplayName("ФИО клиента")] + [Column(title: "ФИО клиента", width: 150)] public string ClientFIO { get; set; } = string.Empty; - [DisplayName("Логин (эл. почта)")] + [Column(title: "Логин (эл. почта)", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string Email { get; set; } = string.Empty; - [DisplayName("Пароль")] + [Column(title: "Пароль", width: 150)] public string Password { get; set; } = string.Empty; } } diff --git a/Shipyard/ShipyardContracts/ViewModels/MessageInfoViewModel.cs b/Shipyard/ShipyardContracts/ViewModels/MessageInfoViewModel.cs index 416f0d9..401abad 100644 --- a/Shipyard/ShipyardContracts/ViewModels/MessageInfoViewModel.cs +++ b/Shipyard/ShipyardContracts/ViewModels/MessageInfoViewModel.cs @@ -25,5 +25,7 @@ namespace ShipyardContracts.ViewModels [DisplayName("Текст")] public string Body { get; set; } = string.Empty; + + public int Id => throw new NotImplementedException(); } } diff --git a/Shipyard/ShipyardDataBaseImplement/Implements/BackUpInfo.cs b/Shipyard/ShipyardDataBaseImplement/Implements/BackUpInfo.cs new file mode 100644 index 0000000..9a6b4e0 --- /dev/null +++ b/Shipyard/ShipyardDataBaseImplement/Implements/BackUpInfo.cs @@ -0,0 +1,32 @@ +using ShipyardContracts.StoragesContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShipyardDataBaseImplement.Implements +{ + public class BackUpInfo : IBackUpInfo + { + public List? GetList() where T : class, new() + { + using var context = new ShipyardDataBase(); + return context.Set().ToList(); + } + + public Type? GetTypeByModelInterface(string modelInterfaceName) + { + var assembly = typeof(BackUpInfo).Assembly; + var types = assembly.GetTypes(); + foreach (var type in types) + { + if (type.IsClass && type.GetInterface(modelInterfaceName) != null) + { + return type; + } + } + return null; + } + } +} diff --git a/Shipyard/ShipyardDataBaseImplement/Models/Message.cs b/Shipyard/ShipyardDataBaseImplement/Models/Message.cs index 28ee70f..7ca9063 100644 --- a/Shipyard/ShipyardDataBaseImplement/Models/Message.cs +++ b/Shipyard/ShipyardDataBaseImplement/Models/Message.cs @@ -24,6 +24,7 @@ namespace ShipyardDataBaseImplement.Models public string Subject { get; private set; } = string.Empty; public string Body { get; private set; } = string.Empty; + public int Id => throw new NotImplementedException(); public static Message? Create(MessageInfoBindingModel model) { diff --git a/Shipyard/ShipyardDataModels/Models/IMessageInfoModel.cs b/Shipyard/ShipyardDataModels/Models/IMessageInfoModel.cs index 02063dd..2e61010 100644 --- a/Shipyard/ShipyardDataModels/Models/IMessageInfoModel.cs +++ b/Shipyard/ShipyardDataModels/Models/IMessageInfoModel.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace ShipyardDataModels.Models { - public interface IMessageInfoModel + public interface IMessageInfoModel : IId { string MessageId { get; } diff --git a/Shipyard/ShipyardFileImplement/Models/Message.cs b/Shipyard/ShipyardFileImplement/Models/Message.cs index 6804164..2f56845 100644 --- a/Shipyard/ShipyardFileImplement/Models/Message.cs +++ b/Shipyard/ShipyardFileImplement/Models/Message.cs @@ -23,6 +23,7 @@ namespace ShipyardFileImplement.Models public string Subject { get; private set; } = string.Empty; public string Body { get; private set; } = string.Empty; + public int Id => throw new NotImplementedException(); public static Message? Create(MessageInfoBindingModel model) { if (model == null) diff --git a/Shipyard/ShipyardListImplement»/Implements/BackUpInfo.cs b/Shipyard/ShipyardListImplement»/Implements/BackUpInfo.cs new file mode 100644 index 0000000..1886374 --- /dev/null +++ b/Shipyard/ShipyardListImplement»/Implements/BackUpInfo.cs @@ -0,0 +1,22 @@ +using ShipyardContracts.StoragesContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShipyardListImplement_.Implements +{ + public class BackUpInfo : IBackUpInfo + { + public List? GetList() where T : class, new() + { + throw new NotImplementedException(); + } + + public Type? GetTypeByModelInterface(string modelInterfaceName) + { + throw new NotImplementedException(); + } + } +} diff --git a/Shipyard/ShipyardListImplement»/ListImplementationExtension.cs b/Shipyard/ShipyardListImplement»/ListImplementationExtension.cs new file mode 100644 index 0000000..2a2697f --- /dev/null +++ b/Shipyard/ShipyardListImplement»/ListImplementationExtension.cs @@ -0,0 +1,28 @@ +using ShipyardContracts.DI; +using ShipyardContracts.StoragesContracts; +using ShipyardListImplement.Implements; +using ShipyardListImplement_.Implements; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShipyardListImplement_ +{ + public class ListImplementationExtension : IImplementationExtension + { + public int Priority => 0; + + public void RegisterServices() + { + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + } + } +} diff --git a/Shipyard/ShipyardListImplement»/Models/Message.cs b/Shipyard/ShipyardListImplement»/Models/Message.cs index 99d8e1c..5908058 100644 --- a/Shipyard/ShipyardListImplement»/Models/Message.cs +++ b/Shipyard/ShipyardListImplement»/Models/Message.cs @@ -22,6 +22,7 @@ namespace ShipyardListImplement_.Models public string Subject { get; private set; } = string.Empty; public string Body { get; private set; } = string.Empty; + public int Id => throw new NotImplementedException(); public static Message? Create(MessageInfoBindingModel model) { diff --git a/Shipyard/ShipyardListImplement»/ShipyardListImplement.csproj b/Shipyard/ShipyardListImplement»/ShipyardListImplement.csproj index 22ef310..2a2ea2e 100644 --- a/Shipyard/ShipyardListImplement»/ShipyardListImplement.csproj +++ b/Shipyard/ShipyardListImplement»/ShipyardListImplement.csproj @@ -24,4 +24,8 @@ + + + +