From 30d108205849cd40dc2cad8aa48bcd460b0e99f0 Mon Sep 17 00:00:00 2001 From: maxnes3 <112558334+maxnes3@users.noreply.github.com> Date: Thu, 4 May 2023 19:33:09 +0400 Subject: [PATCH] =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=B3=D0=BD?= =?UTF-8?q?=D0=B8=D0=B4=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/BackUpLogic.cs | 103 ++++++++++++++++++ .../Attributes/ColumnAttribute.cs | 26 +++++ .../Attributes/GridViewAutoSize.cs | 20 ++++ .../BindingModels/BackUpSaveBinidngModel.cs | 13 +++ .../BindingModels/MessageInfoBindingModel.cs | 4 +- .../BusinessLogicContracts/IBackUpLogic.cs | 14 +++ .../ComputersShopContracts.csproj | 6 + .../DI/DependencyManager.cs | 44 ++++++++ .../DI/IDependencyContainer.cs | 17 +++ .../DI/IImplementationExtension.cs | 14 +++ .../DI/ServiceDependencyContainer.cs | 62 +++++++++++ .../DI/ServiceProviderLoader.cs | 59 ++++++++++ .../DI/UnityDependencyContainer.cs | 43 ++++++++ .../StoragesContracts/IBackUpInfo.cs | 14 +++ .../ViewModels/ClientViewModel.cs | 13 +-- .../ViewModels/ComponentViewModel.cs | 8 +- .../ViewModels/ComputerViewModel.cs | 9 +- .../ViewModels/ImplementerViewModel.cs | 22 ++-- .../ViewModels/MessageInfoViewModel.cs | 30 ++--- .../ViewModels/OrderViewModel.cs | 28 +++-- .../ComputersShopDataBaseImplement.csproj | 6 +- .../DatabaseImplementationExtension.cs | 27 +++++ .../Implements/BackUpInfo.cs | 32 ++++++ .../Models/Message.cs | 6 +- .../Models/IMessageInfoModel.cs | 2 +- .../ComputersShopFileImplement.csproj | 6 +- .../FileImplementationExtension.cs | 27 +++++ .../Implements/BackUpInfo.cs | 35 ++++++ .../Models/Message.cs | 2 + .../ComputersShopListImplement.csproj | 4 + .../Implements/BackUpInfo.cs | 22 ++++ .../Implements/OrderStorage.cs | 2 +- .../ListImplementationExtension.cs | 26 +++++ .../Models/Message.cs | 2 + .../DataGridViewExtension.cs | 52 +++++++++ .../ComputersShopView/FormClients.cs | 11 +- .../ComputersShopView/FormComponents.cs | 16 +-- .../ComputersShopView/FormComputer.cs | 5 +- .../ComputersShopView/FormComputers.cs | 17 +-- .../ComputersShopView/FormImplementers.cs | 25 ++--- ComputersShop/ComputersShopView/FormMails.cs | 12 +- .../ComputersShopView/FormMain.Designer.cs | 11 +- ComputersShop/ComputersShopView/FormMain.cs | 62 +++++++---- ComputersShop/ComputersShopView/Program.cs | 57 ++++++++-- 44 files changed, 873 insertions(+), 143 deletions(-) create mode 100644 ComputersShop/ComputersShopBusinessLogic/BusinessLogics/BackUpLogic.cs create mode 100644 ComputersShop/ComputersShopContracts/Attributes/ColumnAttribute.cs create mode 100644 ComputersShop/ComputersShopContracts/Attributes/GridViewAutoSize.cs create mode 100644 ComputersShop/ComputersShopContracts/BindingModels/BackUpSaveBinidngModel.cs create mode 100644 ComputersShop/ComputersShopContracts/BusinessLogicContracts/IBackUpLogic.cs create mode 100644 ComputersShop/ComputersShopContracts/DI/DependencyManager.cs create mode 100644 ComputersShop/ComputersShopContracts/DI/IDependencyContainer.cs create mode 100644 ComputersShop/ComputersShopContracts/DI/IImplementationExtension.cs create mode 100644 ComputersShop/ComputersShopContracts/DI/ServiceDependencyContainer.cs create mode 100644 ComputersShop/ComputersShopContracts/DI/ServiceProviderLoader.cs create mode 100644 ComputersShop/ComputersShopContracts/DI/UnityDependencyContainer.cs create mode 100644 ComputersShop/ComputersShopContracts/StoragesContracts/IBackUpInfo.cs create mode 100644 ComputersShop/ComputersShopDataBaseImplement/DatabaseImplementationExtension.cs create mode 100644 ComputersShop/ComputersShopDataBaseImplement/Implements/BackUpInfo.cs create mode 100644 ComputersShop/ComputersShopFileImplement/FileImplementationExtension.cs create mode 100644 ComputersShop/ComputersShopFileImplement/Implements/BackUpInfo.cs create mode 100644 ComputersShop/ComputersShopListImplement/Implements/BackUpInfo.cs create mode 100644 ComputersShop/ComputersShopListImplement/ListImplementationExtension.cs create mode 100644 ComputersShop/ComputersShopView/DataGridViewExtension.cs diff --git a/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/BackUpLogic.cs b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/BackUpLogic.cs new file mode 100644 index 0000000..ace5ae0 --- /dev/null +++ b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/BackUpLogic.cs @@ -0,0 +1,103 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.BusinessLogicContracts; +using ComputersShopContracts.StoragesContracts; +using ComputersShopDataModels; +using Microsoft.Extensions.Logging; +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 ComputersShopBusinessLogic.BusinessLogics +{ + 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/ComputersShop/ComputersShopContracts/Attributes/ColumnAttribute.cs b/ComputersShop/ComputersShopContracts/Attributes/ColumnAttribute.cs new file mode 100644 index 0000000..7281412 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/Attributes/ColumnAttribute.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.Attributes +{ + [AttributeUsage(AttributeTargets.Property)] + public class ColumnAttribute : Attribute + { + 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; + } + 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; } + } +} diff --git a/ComputersShop/ComputersShopContracts/Attributes/GridViewAutoSize.cs b/ComputersShop/ComputersShopContracts/Attributes/GridViewAutoSize.cs new file mode 100644 index 0000000..00b1a99 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/Attributes/GridViewAutoSize.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.Attributes +{ + public enum GridViewAutoSize + { + NotSet = 0, + None = 1, + ColumnHeader = 2, + AllCellsExceptHeader = 4, + AllCells = 6, + DisplayedCellsExceptHeader = 8, + DisplayedCells = 10, + Fill = 16 + } +} diff --git a/ComputersShop/ComputersShopContracts/BindingModels/BackUpSaveBinidngModel.cs b/ComputersShop/ComputersShopContracts/BindingModels/BackUpSaveBinidngModel.cs new file mode 100644 index 0000000..07b97d3 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/BindingModels/BackUpSaveBinidngModel.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.BindingModels +{ + public class BackUpSaveBinidngModel + { + public string FolderName { get; set; } = string.Empty; + } +} diff --git a/ComputersShop/ComputersShopContracts/BindingModels/MessageInfoBindingModel.cs b/ComputersShop/ComputersShopContracts/BindingModels/MessageInfoBindingModel.cs index 2966e42..aa3402a 100644 --- a/ComputersShop/ComputersShopContracts/BindingModels/MessageInfoBindingModel.cs +++ b/ComputersShop/ComputersShopContracts/BindingModels/MessageInfoBindingModel.cs @@ -20,5 +20,7 @@ namespace ComputersShopContracts.BindingModels public string Body { get; set; } = string.Empty; public DateTime DateDelivery { get; set; } - } + + public int Id => throw new NotImplementedException(); + } } diff --git a/ComputersShop/ComputersShopContracts/BusinessLogicContracts/IBackUpLogic.cs b/ComputersShop/ComputersShopContracts/BusinessLogicContracts/IBackUpLogic.cs new file mode 100644 index 0000000..e2af5f1 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/BusinessLogicContracts/IBackUpLogic.cs @@ -0,0 +1,14 @@ +using ComputersShopContracts.BindingModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.BusinessLogicContracts +{ + public interface IBackUpLogic + { + void CreateBackUp(BackUpSaveBinidngModel model); + } +} diff --git a/ComputersShop/ComputersShopContracts/ComputersShopContracts.csproj b/ComputersShop/ComputersShopContracts/ComputersShopContracts.csproj index 9c95599..6cee27f 100644 --- a/ComputersShop/ComputersShopContracts/ComputersShopContracts.csproj +++ b/ComputersShop/ComputersShopContracts/ComputersShopContracts.csproj @@ -6,6 +6,12 @@ enable + + + + + + diff --git a/ComputersShop/ComputersShopContracts/DI/DependencyManager.cs b/ComputersShop/ComputersShopContracts/DI/DependencyManager.cs new file mode 100644 index 0000000..4be1173 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/DI/DependencyManager.cs @@ -0,0 +1,44 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.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/ComputersShop/ComputersShopContracts/DI/IDependencyContainer.cs b/ComputersShop/ComputersShopContracts/DI/IDependencyContainer.cs new file mode 100644 index 0000000..2875a5b --- /dev/null +++ b/ComputersShop/ComputersShopContracts/DI/IDependencyContainer.cs @@ -0,0 +1,17 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.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/ComputersShop/ComputersShopContracts/DI/IImplementationExtension.cs b/ComputersShop/ComputersShopContracts/DI/IImplementationExtension.cs new file mode 100644 index 0000000..4f459ea --- /dev/null +++ b/ComputersShop/ComputersShopContracts/DI/IImplementationExtension.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.DI +{ + public interface IImplementationExtension + { + public int Priority { get; } + public void RegisterServices(); + } +} diff --git a/ComputersShop/ComputersShopContracts/DI/ServiceDependencyContainer.cs b/ComputersShop/ComputersShopContracts/DI/ServiceDependencyContainer.cs new file mode 100644 index 0000000..cd5508f --- /dev/null +++ b/ComputersShop/ComputersShopContracts/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 ComputersShopContracts.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/ComputersShop/ComputersShopContracts/DI/ServiceProviderLoader.cs b/ComputersShop/ComputersShopContracts/DI/ServiceProviderLoader.cs new file mode 100644 index 0000000..5b82ca9 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/DI/ServiceProviderLoader.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.DI +{ + public class ServiceProviderLoader + { + 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/ComputersShop/ComputersShopContracts/DI/UnityDependencyContainer.cs b/ComputersShop/ComputersShopContracts/DI/UnityDependencyContainer.cs new file mode 100644 index 0000000..0886159 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/DI/UnityDependencyContainer.cs @@ -0,0 +1,43 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Unity; +using Unity.Microsoft.Logging; + +namespace ComputersShopContracts.DI +{ + public class UnityDependencyContainer : IDependencyContainer + { + private readonly IUnityContainer _container; + + public UnityDependencyContainer() + { + _container = new UnityContainer(); + } + + public void AddLogging(Action configure) + { + var factory = LoggerFactory.Create(configure); + _container.AddExtension(new LoggingExtension(factory)); + } + + public void RegisterType(bool isSingle) where T : class + { + _container.RegisterType(isSingle ? TypeLifetime.Singleton : TypeLifetime.Transient); + + } + + public T Resolve() + { + return _container.Resolve(); + } + + void IDependencyContainer.RegisterType(bool isSingle) + { + _container.RegisterType(isSingle ? TypeLifetime.Singleton : TypeLifetime.Transient); + } + } +} diff --git a/ComputersShop/ComputersShopContracts/StoragesContracts/IBackUpInfo.cs b/ComputersShop/ComputersShopContracts/StoragesContracts/IBackUpInfo.cs new file mode 100644 index 0000000..fb57dac --- /dev/null +++ b/ComputersShop/ComputersShopContracts/StoragesContracts/IBackUpInfo.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.StoragesContracts +{ + public interface IBackUpInfo + { + List? GetList() where T : class, new(); + Type? GetTypeByModelInterface(string modelInterfaceName); + } +} diff --git a/ComputersShop/ComputersShopContracts/ViewModels/ClientViewModel.cs b/ComputersShop/ComputersShopContracts/ViewModels/ClientViewModel.cs index e355967..0959f7f 100644 --- a/ComputersShop/ComputersShopContracts/ViewModels/ClientViewModel.cs +++ b/ComputersShop/ComputersShopContracts/ViewModels/ClientViewModel.cs @@ -1,4 +1,5 @@ -using ComputersShopDataModels.Models; +using ComputersShopContracts.Attributes; +using ComputersShopDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel; @@ -10,15 +11,13 @@ namespace ComputersShopContracts.ViewModels { public class ClientViewModel : IClientModel { + [Column(visible: false)] public int Id { get; set; } - - [DisplayName("ФИО клиента")] + [Column("ФИО клиента", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string ClientFIO { get; set; } = string.Empty; - - [DisplayName("Логин (эл. почта)")] + [Column("Логин (эл. почта)", width: 150)] public string Email { get; set; } = string.Empty; - - [DisplayName("Пароль")] + [Column("Пароль", width: 150)] public string Password { get; set; } = string.Empty; } } diff --git a/ComputersShop/ComputersShopContracts/ViewModels/ComponentViewModel.cs b/ComputersShop/ComputersShopContracts/ViewModels/ComponentViewModel.cs index b0925a7..231461a 100644 --- a/ComputersShop/ComputersShopContracts/ViewModels/ComponentViewModel.cs +++ b/ComputersShop/ComputersShopContracts/ViewModels/ComponentViewModel.cs @@ -1,4 +1,5 @@ -using ComputersShopDataModels.Models; +using ComputersShopContracts.Attributes; +using ComputersShopDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel; @@ -10,10 +11,11 @@ namespace ComputersShopContracts.ViewModels { public class ComponentViewModel : IComponentModel { + [Column(visible: false)] public int Id { get; set; } - [DisplayName("Название компонента")] + [Column("Название комплектующего", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string ComponentName { get; set; } = string.Empty; - [DisplayName("Цена")] + [Column("Цена", width: 80)] public double Cost { get; set; } } } diff --git a/ComputersShop/ComputersShopContracts/ViewModels/ComputerViewModel.cs b/ComputersShop/ComputersShopContracts/ViewModels/ComputerViewModel.cs index aeb3348..0e628c1 100644 --- a/ComputersShop/ComputersShopContracts/ViewModels/ComputerViewModel.cs +++ b/ComputersShop/ComputersShopContracts/ViewModels/ComputerViewModel.cs @@ -1,4 +1,5 @@ -using ComputersShopDataModels.Models; +using ComputersShopContracts.Attributes; +using ComputersShopDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel; @@ -10,14 +11,16 @@ namespace ComputersShopContracts.ViewModels { public class ComputerViewModel : IComputerModel { + [Column(visible: false)] public int Id { get; set; } - [DisplayName("Название компьютера")] + [Column("Название компьютера", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string ComputerName { get; set; } = string.Empty; - [DisplayName("Цена")] + [Column("Цена", width: 100)] public double Price { get; set; } + [Column(visible: false)] public Dictionary ComputerComponents { get; diff --git a/ComputersShop/ComputersShopContracts/ViewModels/ImplementerViewModel.cs b/ComputersShop/ComputersShopContracts/ViewModels/ImplementerViewModel.cs index 919a96d..75c13e6 100644 --- a/ComputersShop/ComputersShopContracts/ViewModels/ImplementerViewModel.cs +++ b/ComputersShop/ComputersShopContracts/ViewModels/ImplementerViewModel.cs @@ -1,4 +1,5 @@ -using ComputersShopDataModels.Models; +using ComputersShopContracts.Attributes; +using ComputersShopDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel; @@ -10,18 +11,19 @@ namespace ComputersShopContracts.ViewModels { public class ImplementerViewModel : IImplementerModel { - public int Id { get; set; } + [Column(visible: false)] + public int Id { get; set; } - [DisplayName("ФИО исполнителя")] - public string ImplementerFIO { get; set; } = string.Empty; + [Column("ФИО исполнителя", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] + public string ImplementerFIO { get; set; } = string.Empty; - [DisplayName("Пароль")] - public string Password { get; set; } = string.Empty; + [Column("Пароль", width: 150)] + public string Password { get; set; } = string.Empty; - [DisplayName("Стаж работы")] - public int WorkExperience { get; set; } + [Column("Стаж работы", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)] + public int WorkExperience { get; set; } - [DisplayName("Квалификация")] - public int Qualification { get; set; } + [Column("Квалификация", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)] + public int Qualification { get; set; } } } diff --git a/ComputersShop/ComputersShopContracts/ViewModels/MessageInfoViewModel.cs b/ComputersShop/ComputersShopContracts/ViewModels/MessageInfoViewModel.cs index e9f7ce7..13817e9 100644 --- a/ComputersShop/ComputersShopContracts/ViewModels/MessageInfoViewModel.cs +++ b/ComputersShop/ComputersShopContracts/ViewModels/MessageInfoViewModel.cs @@ -1,4 +1,5 @@ -using ComputersShopDataModels.Models; +using ComputersShopContracts.Attributes; +using ComputersShopDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel; @@ -10,20 +11,23 @@ namespace ComputersShopContracts.ViewModels { public class MessageInfoViewModel : IMessageInfoModel { - public string MessageId { get; set; } = string.Empty; + [Column(visible: false)] + public string MessageId { get; set; } = string.Empty; + [Column(visible: false)] + public int? ClientId { get; set; } - public int? ClientId { get; set; } + [Column("Отправитель", gridViewAutoSize: GridViewAutoSize.DisplayedCells, isUseAutoSize: true)] + public string SenderName { get; set; } = string.Empty; - [DisplayName("Отправитель")] - public string SenderName { get; set; } = string.Empty; + [Column("Дата письма", width: 100)] + public DateTime DateDelivery { get; set; } - [DisplayName("Дата письма")] - public DateTime DateDelivery { get; set; } + [Column("Заголовок", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)] + public string Subject { get; set; } = string.Empty; - [DisplayName("Заголовок")] - public string Subject { get; set; } = string.Empty; - - [DisplayName("Текст")] - public string Body { get; set; } = string.Empty; - } + [Column("Текст", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)] + public string Body { get; set; } = string.Empty; + [Column(visible: false)] + public int Id => throw new NotImplementedException(); + } } diff --git a/ComputersShop/ComputersShopContracts/ViewModels/OrderViewModel.cs b/ComputersShop/ComputersShopContracts/ViewModels/OrderViewModel.cs index f7c439e..a07eda3 100644 --- a/ComputersShop/ComputersShopContracts/ViewModels/OrderViewModel.cs +++ b/ComputersShop/ComputersShopContracts/ViewModels/OrderViewModel.cs @@ -1,4 +1,5 @@ -using ComputersShopDataModels.Enums; +using ComputersShopContracts.Attributes; +using ComputersShopDataModels.Enums; using ComputersShopDataModels.Models; using System; using System.Collections.Generic; @@ -11,26 +12,29 @@ namespace ComputersShopContracts.ViewModels { public class OrderViewModel : IOrderModel { - [DisplayName("Номер")] + [Column("Номер", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)] public int Id { get; set; } + [Column(visible: false)] public int ComputerId { get; set; } + [Column(visible: false)] public int ClientId { get; set; } - [DisplayName("Фамилия клиента")] + [Column("Данные клиента", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string ClientFIO { get; set; } = string.Empty; - public int? ImplementerId { get; set; } - [DisplayName("Исполнитель")] - public string ImplementerFIO { get; set; } = string.Empty; - [DisplayName("Компьютер")] + [Column(visible: false)] + public int? ImplementerId { get; set; } + [Column("Данные исполнителя", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] + public string ImplementerFIO { get; set; } = string.Empty; + [Column("Компьютер", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)] public string ComputerName { get; set; } = string.Empty; - [DisplayName("Количество")] + [Column("Количество", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)] public int Count { get; set; } - [DisplayName("Сумма")] + [Column("Сумма", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)] public double Sum { get; set; } - [DisplayName("Статус")] + [Column("Статус", gridViewAutoSize: GridViewAutoSize.AllCells, isUseAutoSize: true)] public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - [DisplayName("Дата создания")] + [Column("Дата создания", width: 100)] public DateTime DateCreate { get; set; } = DateTime.Now; - [DisplayName("Дата выполнения")] + [Column("Дата выполнения", width: 100)] public DateTime? DateImplement { get; set; } } } diff --git a/ComputersShop/ComputersShopDataBaseImplement/ComputersShopDataBaseImplement.csproj b/ComputersShop/ComputersShopDataBaseImplement/ComputersShopDataBaseImplement.csproj index d567dd6..01056b4 100644 --- a/ComputersShop/ComputersShopDataBaseImplement/ComputersShopDataBaseImplement.csproj +++ b/ComputersShop/ComputersShopDataBaseImplement/ComputersShopDataBaseImplement.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -20,4 +20,8 @@ + + + + diff --git a/ComputersShop/ComputersShopDataBaseImplement/DatabaseImplementationExtension.cs b/ComputersShop/ComputersShopDataBaseImplement/DatabaseImplementationExtension.cs new file mode 100644 index 0000000..f8cd70a --- /dev/null +++ b/ComputersShop/ComputersShopDataBaseImplement/DatabaseImplementationExtension.cs @@ -0,0 +1,27 @@ +using ComputersShopContracts.DI; +using ComputersShopContracts.StoragesContracts; +using ComputersShopDataBaseImplement.Implements; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDataBaseImplement +{ + public class DatabaseImplementationExtension : IImplementationExtension + { + public int Priority => 2; + + 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/ComputersShop/ComputersShopDataBaseImplement/Implements/BackUpInfo.cs b/ComputersShop/ComputersShopDataBaseImplement/Implements/BackUpInfo.cs new file mode 100644 index 0000000..74fdf91 --- /dev/null +++ b/ComputersShop/ComputersShopDataBaseImplement/Implements/BackUpInfo.cs @@ -0,0 +1,32 @@ +using ComputersShopContracts.StoragesContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDataBaseImplement.Implements +{ + public class BackUpInfo : IBackUpInfo + { + public List? GetList() where T : class, new() + { + using var context = new ComputersShopDataBase(); + 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/ComputersShop/ComputersShopDataBaseImplement/Models/Message.cs b/ComputersShop/ComputersShopDataBaseImplement/Models/Message.cs index 88295d9..28e78c7 100644 --- a/ComputersShop/ComputersShopDataBaseImplement/Models/Message.cs +++ b/ComputersShop/ComputersShopDataBaseImplement/Models/Message.cs @@ -25,7 +25,9 @@ namespace ComputersShopDataBaseImplement.Models public string Body { get; private set; } = string.Empty; - public static Message? Create(MessageInfoBindingModel model) + public int Id => throw new NotImplementedException(); + + public static Message? Create(MessageInfoBindingModel model) { if (model == null) { @@ -51,5 +53,5 @@ namespace ComputersShopDataBaseImplement.Models SenderName = SenderName, DateDelivery = DateDelivery, }; - } + } } diff --git a/ComputersShop/ComputersShopDataModels/Models/IMessageInfoModel.cs b/ComputersShop/ComputersShopDataModels/Models/IMessageInfoModel.cs index 5985ff7..77f4620 100644 --- a/ComputersShop/ComputersShopDataModels/Models/IMessageInfoModel.cs +++ b/ComputersShop/ComputersShopDataModels/Models/IMessageInfoModel.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace ComputersShopDataModels.Models { - public interface IMessageInfoModel + public interface IMessageInfoModel : IId { string MessageId { get; } diff --git a/ComputersShop/ComputersShopFileImplement/ComputersShopFileImplement.csproj b/ComputersShop/ComputersShopFileImplement/ComputersShopFileImplement.csproj index 8eb14fb..d9043b4 100644 --- a/ComputersShop/ComputersShopFileImplement/ComputersShopFileImplement.csproj +++ b/ComputersShop/ComputersShopFileImplement/ComputersShopFileImplement.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -11,4 +11,8 @@ + + + + diff --git a/ComputersShop/ComputersShopFileImplement/FileImplementationExtension.cs b/ComputersShop/ComputersShopFileImplement/FileImplementationExtension.cs new file mode 100644 index 0000000..7968798 --- /dev/null +++ b/ComputersShop/ComputersShopFileImplement/FileImplementationExtension.cs @@ -0,0 +1,27 @@ +using ComputersShopContracts.DI; +using ComputersShopContracts.StoragesContracts; +using ComputersShopFileImplement.Implements; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopFileImplement +{ + public class FileImplementationExtension : IImplementationExtension + { + public int Priority => 1; + + 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/ComputersShop/ComputersShopFileImplement/Implements/BackUpInfo.cs b/ComputersShop/ComputersShopFileImplement/Implements/BackUpInfo.cs new file mode 100644 index 0000000..23c337c --- /dev/null +++ b/ComputersShop/ComputersShopFileImplement/Implements/BackUpInfo.cs @@ -0,0 +1,35 @@ +using ComputersShopContracts.StoragesContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopFileImplement.Implements +{ + public class BackUpInfo : IBackUpInfo + { + public List? GetList() where T : class, new() + { + // Получаем значения из singleton-объекта универсального свойства содержащее тип T + var source = DataFileSingleton.GetInstance(); + return (List?)source.GetType().GetProperties() + .FirstOrDefault(x => x.PropertyType.IsGenericType && x.PropertyType.GetGenericArguments()[0] == typeof(T)) + ?.GetValue(source); + } + + 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/ComputersShop/ComputersShopFileImplement/Models/Message.cs b/ComputersShop/ComputersShopFileImplement/Models/Message.cs index bef34fa..5a2676d 100644 --- a/ComputersShop/ComputersShopFileImplement/Models/Message.cs +++ b/ComputersShop/ComputersShopFileImplement/Models/Message.cs @@ -24,6 +24,8 @@ namespace ComputersShopFileImplement.Models 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/ComputersShop/ComputersShopListImplement/ComputersShopListImplement.csproj b/ComputersShop/ComputersShopListImplement/ComputersShopListImplement.csproj index b897a9c..d9043b4 100644 --- a/ComputersShop/ComputersShopListImplement/ComputersShopListImplement.csproj +++ b/ComputersShop/ComputersShopListImplement/ComputersShopListImplement.csproj @@ -11,4 +11,8 @@ + + + + diff --git a/ComputersShop/ComputersShopListImplement/Implements/BackUpInfo.cs b/ComputersShop/ComputersShopListImplement/Implements/BackUpInfo.cs new file mode 100644 index 0000000..42a8cb1 --- /dev/null +++ b/ComputersShop/ComputersShopListImplement/Implements/BackUpInfo.cs @@ -0,0 +1,22 @@ +using ComputersShopContracts.StoragesContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopListImplement.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/ComputersShop/ComputersShopListImplement/Implements/OrderStorage.cs b/ComputersShop/ComputersShopListImplement/Implements/OrderStorage.cs index d33cf81..6c36287 100644 --- a/ComputersShop/ComputersShopListImplement/Implements/OrderStorage.cs +++ b/ComputersShop/ComputersShopListImplement/Implements/OrderStorage.cs @@ -10,7 +10,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace OrdersShopListImplement.Implements +namespace ComputersShopListImplement.Implements { public class OrderStorage : IOrderStorage { diff --git a/ComputersShop/ComputersShopListImplement/ListImplementationExtension.cs b/ComputersShop/ComputersShopListImplement/ListImplementationExtension.cs new file mode 100644 index 0000000..03ba685 --- /dev/null +++ b/ComputersShop/ComputersShopListImplement/ListImplementationExtension.cs @@ -0,0 +1,26 @@ +using ComputersShopContracts.DI; +using ComputersShopContracts.StoragesContracts; +using ComputersShopListImplement.Implements; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopListImplement +{ + 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/ComputersShop/ComputersShopListImplement/Models/Message.cs b/ComputersShop/ComputersShopListImplement/Models/Message.cs index da24fe7..96e8380 100644 --- a/ComputersShop/ComputersShopListImplement/Models/Message.cs +++ b/ComputersShop/ComputersShopListImplement/Models/Message.cs @@ -23,6 +23,8 @@ namespace ComputersShopListImplement.Models 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/ComputersShop/ComputersShopView/DataGridViewExtension.cs b/ComputersShop/ComputersShopView/DataGridViewExtension.cs new file mode 100644 index 0000000..608b3e8 --- /dev/null +++ b/ComputersShop/ComputersShopView/DataGridViewExtension.cs @@ -0,0 +1,52 @@ +using ComputersShopContracts.Attributes; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopView +{ + public 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/ComputersShop/ComputersShopView/FormClients.cs b/ComputersShop/ComputersShopView/FormClients.cs index 685b170..5e7ec9c 100644 --- a/ComputersShop/ComputersShopView/FormClients.cs +++ b/ComputersShop/ComputersShopView/FormClients.cs @@ -1,4 +1,5 @@ -using ComputersShopContracts.BindingModels; +using ComputersShopBusinessLogic.BusinessLogics; +using ComputersShopContracts.BindingModels; using ComputersShopContracts.BusinessLogicContracts; using Microsoft.Extensions.Logging; using System; @@ -35,13 +36,7 @@ namespace ComputersShopView { 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/ComputersShop/ComputersShopView/FormComponents.cs b/ComputersShop/ComputersShopView/FormComponents.cs index 7ce60da..796810d 100644 --- a/ComputersShop/ComputersShopView/FormComponents.cs +++ b/ComputersShop/ComputersShopView/FormComponents.cs @@ -1,5 +1,7 @@ -using ComputersShopContracts.BindingModels; +using ComputersShopBusinessLogic.BusinessLogics; +using ComputersShopContracts.BindingModels; using ComputersShopContracts.BusinessLogicContracts; +using ComputersShopContracts.DI; using Microsoft.Extensions.Logging; using Microsoft.VisualBasic.Logging; using System; @@ -36,13 +38,7 @@ namespace ComputersShopView { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["ComponentName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } + dataGridView.FillandConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Загрузка компонентов"); } catch (Exception ex) @@ -54,7 +50,7 @@ namespace ComputersShopView private void ButtonAdd_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormComponent)); + var service = DependencyManager.Instance.Resolve(); if (service is FormComponent form) { if (form.ShowDialog() == DialogResult.OK) @@ -68,7 +64,7 @@ namespace ComputersShopView { if (dataGridView.SelectedRows.Count == 1) { - var service = Program.ServiceProvider?.GetService(typeof(FormComponent)); + var service = DependencyManager.Instance.Resolve(); if (service is FormComponent form) { form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); diff --git a/ComputersShop/ComputersShopView/FormComputer.cs b/ComputersShop/ComputersShopView/FormComputer.cs index 687ffbb..57bb142 100644 --- a/ComputersShop/ComputersShopView/FormComputer.cs +++ b/ComputersShop/ComputersShopView/FormComputer.cs @@ -12,6 +12,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using ComputersShopContracts.DI; namespace ComputersShopView { @@ -81,7 +82,7 @@ namespace ComputersShopView } private void ButtonAdd_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormComputerComponent)); + var service = DependencyManager.Instance.Resolve(); if (service is FormComputerComponent form) { if (form.ShowDialog() == DialogResult.OK) @@ -109,7 +110,7 @@ namespace ComputersShopView { if (dataGridView.SelectedRows.Count == 1) { - var service = Program.ServiceProvider?.GetService(typeof(FormComputerComponent)); + var service = DependencyManager.Instance.Resolve(); if (service is FormComputerComponent form) { int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value); diff --git a/ComputersShop/ComputersShopView/FormComputers.cs b/ComputersShop/ComputersShopView/FormComputers.cs index 1b39ed7..1912035 100644 --- a/ComputersShop/ComputersShopView/FormComputers.cs +++ b/ComputersShop/ComputersShopView/FormComputers.cs @@ -1,5 +1,7 @@ -using ComputersShopContracts.BindingModels; +using ComputersShopBusinessLogic.BusinessLogics; +using ComputersShopContracts.BindingModels; using ComputersShopContracts.BusinessLogicContracts; +using ComputersShopContracts.DI; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -34,14 +36,7 @@ namespace ComputersShopView { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["ComputerName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - dataGridView.Columns["ComputerComponents"].Visible = false; - } + dataGridView.FillandConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Загрузка компьютеров"); } catch (Exception ex) @@ -53,7 +48,7 @@ namespace ComputersShopView private void ButtonAdd_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormComputer)); + var service = DependencyManager.Instance.Resolve(); if (service is FormComputer form) { if (form.ShowDialog() == DialogResult.OK) @@ -67,7 +62,7 @@ namespace ComputersShopView { if (dataGridView.SelectedRows.Count == 1) { - var service = Program.ServiceProvider?.GetService(typeof(FormComputer)); + var service = DependencyManager.Instance.Resolve(); if (service is FormComputer form) { form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); diff --git a/ComputersShop/ComputersShopView/FormImplementers.cs b/ComputersShop/ComputersShopView/FormImplementers.cs index b42b534..7a161e2 100644 --- a/ComputersShop/ComputersShopView/FormImplementers.cs +++ b/ComputersShop/ComputersShopView/FormImplementers.cs @@ -1,5 +1,7 @@ -using ComputersShopContracts.BindingModels; +using ComputersShopBusinessLogic.BusinessLogics; +using ComputersShopContracts.BindingModels; using ComputersShopContracts.BusinessLogicContracts; +using ComputersShopContracts.DI; using ComputersShopView; using Microsoft.Extensions.Logging; using System; @@ -30,20 +32,11 @@ namespace ComputersShopView LoadData(); } private void LoadData() - { - try + { + try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["ImplementerFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - dataGridView.Columns["Password"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - dataGridView.Columns["WorkExperience"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - dataGridView.Columns["Qualification"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } - _logger.LogInformation("Загрузка исполнителей"); + dataGridView.FillandConfigGrid(_logic.ReadList(null)); + _logger.LogInformation("Загрузка исполнителей"); } catch (Exception ex) { @@ -54,7 +47,7 @@ namespace ComputersShopView private void ButtonAdd_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormImplementer)); + var service = DependencyManager.Instance.Resolve(); if (service is FormImplementer form) { if (form.ShowDialog() == DialogResult.OK) @@ -68,7 +61,7 @@ namespace ComputersShopView { if (dataGridView.SelectedRows.Count == 1) { - var service = Program.ServiceProvider?.GetService(typeof(FormImplementer)); + var service = DependencyManager.Instance.Resolve(); if (service is FormImplementer form) { form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); diff --git a/ComputersShop/ComputersShopView/FormMails.cs b/ComputersShop/ComputersShopView/FormMails.cs index 4baad03..66b4da5 100644 --- a/ComputersShop/ComputersShopView/FormMails.cs +++ b/ComputersShop/ComputersShopView/FormMails.cs @@ -1,4 +1,5 @@ -using ComputersShopContracts.BusinessLogicContracts; +using ComputersShopBusinessLogic.BusinessLogics; +using ComputersShopContracts.BusinessLogicContracts; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -28,14 +29,7 @@ namespace ComputersShopView { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["ClientId"].Visible = false; - dataGridView.Columns["MessageId"].Visible = false; - dataGridView.Columns["Body"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } + dataGridView.FillandConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Загрузка писем"); } catch (Exception ex) diff --git a/ComputersShop/ComputersShopView/FormMain.Designer.cs b/ComputersShop/ComputersShopView/FormMain.Designer.cs index 5018c62..22a4bf6 100644 --- a/ComputersShop/ComputersShopView/FormMain.Designer.cs +++ b/ComputersShop/ComputersShopView/FormMain.Designer.cs @@ -40,6 +40,7 @@ списокЗаказовToolStripMenuItem = new ToolStripMenuItem(); запускРаботToolStripMenuItem = new ToolStripMenuItem(); письмаToolStripMenuItem = new ToolStripMenuItem(); + создатьBackupToolStripMenuItem = new ToolStripMenuItem(); dataGridView = new DataGridView(); buttonCreateOrder = new Button(); buttonIssuedOrder = new Button(); @@ -50,7 +51,7 @@ // // menuStrip // - menuStrip.Items.AddRange(new ToolStripItem[] { справочникToolStripMenuItem, отчётыToolStripMenuItem, запускРаботToolStripMenuItem, письмаToolStripMenuItem }); + menuStrip.Items.AddRange(new ToolStripItem[] { справочникToolStripMenuItem, отчётыToolStripMenuItem, запускРаботToolStripMenuItem, письмаToolStripMenuItem, создатьBackupToolStripMenuItem }); menuStrip.Location = new Point(0, 0); menuStrip.Name = "menuStrip"; menuStrip.Size = new Size(1047, 24); @@ -134,6 +135,13 @@ письмаToolStripMenuItem.Text = "письма"; письмаToolStripMenuItem.Click += MailToolStripMenuItem_Click; // + // создатьBackupToolStripMenuItem + // + создатьBackupToolStripMenuItem.Name = "создатьBackupToolStripMenuItem"; + создатьBackupToolStripMenuItem.Size = new Size(102, 20); + создатьBackupToolStripMenuItem.Text = "создать backup"; + создатьBackupToolStripMenuItem.Click += CreateBackupToolStripMenuItem_Click; + // // dataGridView // dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; @@ -212,5 +220,6 @@ private ToolStripMenuItem исполнителиToolStripMenuItem; private ToolStripMenuItem запускРаботToolStripMenuItem; private ToolStripMenuItem письмаToolStripMenuItem; + private ToolStripMenuItem создатьBackupToolStripMenuItem; } } \ No newline at end of file diff --git a/ComputersShop/ComputersShopView/FormMain.cs b/ComputersShop/ComputersShopView/FormMain.cs index 43350d7..9cc907c 100644 --- a/ComputersShop/ComputersShopView/FormMain.cs +++ b/ComputersShop/ComputersShopView/FormMain.cs @@ -1,6 +1,7 @@ using ComputersShopBusinessLogic.BusinessLogics; using ComputersShopContracts.BindingModels; using ComputersShopContracts.BusinessLogicContracts; +using ComputersShopContracts.DI; using ComputersShopDataModels.Enums; using Microsoft.Extensions.Logging; using System; @@ -21,14 +22,16 @@ namespace ComputersShopView private readonly IOrderLogic _orderLogic; private readonly IReportLogic _reportLogic; private readonly IWorkProcess _workProcess; + private readonly IBackUpLogic _backUpLogic; - public FormMain(ILogger logger, IOrderLogic orderLogic, IReportLogic reportlogic, IWorkProcess workProcess) + public FormMain(ILogger logger, IOrderLogic orderLogic, IReportLogic reportlogic, IWorkProcess workProcess, IBackUpLogic backUpLogic) { InitializeComponent(); _logger = logger; _orderLogic = orderLogic; _reportLogic = reportlogic; _workProcess = workProcess; + _backUpLogic = backUpLogic; } private void FormMain_Load(object sender, EventArgs e) @@ -40,14 +43,7 @@ namespace ComputersShopView _logger.LogInformation("Загрузка заказов"); try { - var list = _orderLogic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["ComputerId"].Visible = false; - dataGridView.Columns["ClientId"].Visible = false; - dataGridView.Columns["ImplementerId"].Visible = false; - } + dataGridView.FillandConfigGrid(_orderLogic.ReadList(null)); _logger.LogInformation("Загрузка заказов"); } catch (Exception ex) @@ -58,7 +54,7 @@ namespace ComputersShopView } private void ComponentsToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormComponents)); + var service = DependencyManager.Instance.Resolve(); if (service is FormComponents form) { form.ShowDialog(); @@ -66,7 +62,7 @@ namespace ComputersShopView } private void ComputersToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormComputers)); + var service = DependencyManager.Instance.Resolve(); if (service is FormComputers form) { form.ShowDialog(); @@ -74,7 +70,7 @@ namespace ComputersShopView } private void ButtonCreateOrder_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormCreateOrder)); + var service = DependencyManager.Instance.Resolve(); if (service is FormCreateOrder form) { form.ShowDialog(); @@ -186,7 +182,7 @@ namespace ComputersShopView private void ComputerComponentsToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormReportComputerComponents)); + var service = DependencyManager.Instance.Resolve(); if (service is FormReportComputerComponents form) { form.ShowDialog(); @@ -195,7 +191,7 @@ namespace ComputersShopView private void OrdersToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormReportOrders)); + var service = DependencyManager.Instance.Resolve(); if (service is FormReportOrders form) { form.ShowDialog(); @@ -204,7 +200,7 @@ namespace ComputersShopView private void ClientsToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormClients)); + var service = DependencyManager.Instance.Resolve(); if (service is FormClients form) { form.ShowDialog(); @@ -213,7 +209,7 @@ namespace ComputersShopView private void ImplementersToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormImplementers)); + var service = DependencyManager.Instance.Resolve(); if (service is FormImplementers form) { form.ShowDialog(); @@ -222,16 +218,16 @@ namespace ComputersShopView private void DoWorkToolStripMenuItem_Click(object sender, EventArgs e) { - _workProcess.DoWork(( - Program.ServiceProvider?.GetService(typeof(IImplementerLogic)) as IImplementerLogic)!, + _workProcess.DoWork( + DependencyManager.Instance.Resolve(), _orderLogic); MessageBox.Show("Процесс обработки запущен", "Сообщение", - MessageBoxButtons.OK, MessageBoxIcon.Information); + MessageBoxButtons.OK, MessageBoxIcon.Information); } private void MailToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormMails)); + var service = DependencyManager.Instance.Resolve(); if (service is FormMails form) { form.ShowDialog(); @@ -242,5 +238,31 @@ namespace ComputersShopView { LoadData(); } + + private void CreateBackupToolStripMenuItem_Click(object sender, EventArgs e) + { + try + { + if (_backUpLogic != null) + { + var fbd = new FolderBrowserDialog(); + if (fbd.ShowDialog() == DialogResult.OK) + { + _backUpLogic.CreateBackUp(new BackUpSaveBinidngModel + { + FolderName = fbd.SelectedPath + }); + MessageBox.Show("Бекап создан", "Сообщение", + MessageBoxButtons.OK, MessageBoxIcon.Information); + } + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + + } } } diff --git a/ComputersShop/ComputersShopView/Program.cs b/ComputersShop/ComputersShopView/Program.cs index 3fd8355..7cb021a 100644 --- a/ComputersShop/ComputersShopView/Program.cs +++ b/ComputersShop/ComputersShopView/Program.cs @@ -9,13 +9,12 @@ using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; using ComputersShopBusinessLogic.MailWorker; using ComputersShopContracts.BindingModels; +using ComputersShopContracts.DI; namespace ComputersShopView { internal static class Program { - private static ServiceProvider? _serviceProvider; - public static ServiceProvider? ServiceProvider => _serviceProvider; /// /// The main entry point for the application. /// @@ -25,12 +24,10 @@ namespace ComputersShopView // To customize application configuration such as set high DPI settings or default font, // 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, @@ -45,13 +42,13 @@ namespace ComputersShopView } catch (Exception ex) { - var logger = _serviceProvider.GetService(); + var logger = DependencyManager.Instance.Resolve(); logger?.LogError(ex, "Error"); } - Application.Run(_serviceProvider.GetRequiredService()); + Application.Run(DependencyManager.Instance.Resolve()); } - private static void ConfigureServices(ServiceCollection services) + /*private static void ConfigureServices(ServiceCollection services) { services.AddLogging(option => { @@ -64,6 +61,7 @@ namespace ComputersShopView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -73,6 +71,7 @@ namespace ComputersShopView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -92,7 +91,43 @@ namespace ComputersShopView services.AddTransient(); services.AddTransient(); services.AddTransient(); + }*/ + + private static void InitDependency() + { + DependencyManager.InitDependency(); + DependencyManager.Instance.AddLogging(option => + { + option.SetMinimumLevel(LogLevel.Information); + option.AddNLog("nlog.config"); + }); + 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(); + 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(); + } } \ No newline at end of file