diff --git a/.gitignore b/.gitignore index ca1c7a3..45479d0 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,11 @@ # User-specific files (MonoDevelop/Xamarin Studio) *.userprefs +# dll файлы +*.dll + +/CarpentryWorkshop/ImplementationExtensions + # Mono auto generated files mono_crash.* diff --git a/CarpentryWorkshop/CarpentryWorkshop.sln b/CarpentryWorkshop/CarpentryWorkshop.sln index cecbcc8..503ff36 100644 --- a/CarpentryWorkshop/CarpentryWorkshop.sln +++ b/CarpentryWorkshop/CarpentryWorkshop.sln @@ -15,12 +15,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarpentryWorkshopListImplem EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarpentryWorkshopFileImplement", "CarpentryWorkshopFileImplement\CarpentryWorkshopFileImplement.csproj", "{C9BA55BB-A2E0-413E-8F93-74A0F455C682}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarpentryWorkshopDatabaseImplement", "CarpentryWorkshopDatabaseImplement\CarpentryWorkshopDatabaseImplement.csproj", "{E8A783E3-7330-4F9A-B4CB-913C70A0B08F}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarpentryWorkshopRestApi", "CarpentryWorkshopRestApi\CarpentryWorkshopRestApi.csproj", "{5B3828AF-D9D0-475D-9860-DF7A7BF139D3}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarpentryWorkshopClientApp", "CarpentryWorkshopClientApp\CarpentryWorkshopClientApp.csproj", "{9F21487D-B5B2-43DD-952D-BAAD1A416353}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarpentryWorkshopDatabaseImplement", "CarpentryWorkshopDatabaseImplement\CarpentryWorkshopDatabaseImplement.csproj", "{407DB813-B569-497D-9437-61B6B838D606}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -51,10 +51,6 @@ Global {C9BA55BB-A2E0-413E-8F93-74A0F455C682}.Debug|Any CPU.Build.0 = Debug|Any CPU {C9BA55BB-A2E0-413E-8F93-74A0F455C682}.Release|Any CPU.ActiveCfg = Release|Any CPU {C9BA55BB-A2E0-413E-8F93-74A0F455C682}.Release|Any CPU.Build.0 = Release|Any CPU - {E8A783E3-7330-4F9A-B4CB-913C70A0B08F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {E8A783E3-7330-4F9A-B4CB-913C70A0B08F}.Debug|Any CPU.Build.0 = Debug|Any CPU - {E8A783E3-7330-4F9A-B4CB-913C70A0B08F}.Release|Any CPU.ActiveCfg = Release|Any CPU - {E8A783E3-7330-4F9A-B4CB-913C70A0B08F}.Release|Any CPU.Build.0 = Release|Any CPU {5B3828AF-D9D0-475D-9860-DF7A7BF139D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {5B3828AF-D9D0-475D-9860-DF7A7BF139D3}.Debug|Any CPU.Build.0 = Debug|Any CPU {5B3828AF-D9D0-475D-9860-DF7A7BF139D3}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -63,6 +59,10 @@ Global {9F21487D-B5B2-43DD-952D-BAAD1A416353}.Debug|Any CPU.Build.0 = Debug|Any CPU {9F21487D-B5B2-43DD-952D-BAAD1A416353}.Release|Any CPU.ActiveCfg = Release|Any CPU {9F21487D-B5B2-43DD-952D-BAAD1A416353}.Release|Any CPU.Build.0 = Release|Any CPU + {407DB813-B569-497D-9437-61B6B838D606}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {407DB813-B569-497D-9437-61B6B838D606}.Debug|Any CPU.Build.0 = Debug|Any CPU + {407DB813-B569-497D-9437-61B6B838D606}.Release|Any CPU.ActiveCfg = Release|Any CPU + {407DB813-B569-497D-9437-61B6B838D606}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/BackUpLogic.cs b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/BackUpLogic.cs new file mode 100644 index 0000000..526bedf --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/BackUpLogic.cs @@ -0,0 +1,93 @@ +using Microsoft.Extensions.Logging; +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.BusinessLogicsContracts; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopDataModels; +using System.IO.Compression; +using System.Reflection; +using System.Runtime.Serialization.Json; + +namespace CarpentryWorkshopBusinessLogic.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/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/CarpentryWorkshopBusinessLogic.csproj b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/CarpentryWorkshopBusinessLogic.csproj index 9f95c2a..0e51f19 100644 --- a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/CarpentryWorkshopBusinessLogic.csproj +++ b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/CarpentryWorkshopBusinessLogic.csproj @@ -15,6 +15,7 @@ + diff --git a/CarpentryWorkshop/CarpentryWorkshopClientApp/CarpentryWorkshopClientApp.csproj b/CarpentryWorkshop/CarpentryWorkshopClientApp/CarpentryWorkshopClientApp.csproj index 76ce9c2..efd0b43 100644 --- a/CarpentryWorkshop/CarpentryWorkshopClientApp/CarpentryWorkshopClientApp.csproj +++ b/CarpentryWorkshop/CarpentryWorkshopClientApp/CarpentryWorkshopClientApp.csproj @@ -13,7 +13,6 @@ - diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/Attributes/ColumnAttribute.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/Attributes/ColumnAttribute.cs new file mode 100644 index 0000000..d3bd2bf --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/Attributes/ColumnAttribute.cs @@ -0,0 +1,25 @@ +namespace CarpentryWorkshopContracts.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/CarpentryWorkshop/CarpentryWorkshopContracts/Attributes/GridViewAutoSize.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/Attributes/GridViewAutoSize.cs new file mode 100644 index 0000000..d1deaaa --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/Attributes/GridViewAutoSize.cs @@ -0,0 +1,21 @@ +namespace CarpentryWorkshopContracts.Attributes +{ + public enum GridViewAutoSize + { + NotSet = 0, + + None = 1, + + ColumnHeader = 2, + + AllCellsExceptHeader = 4, + + AllCells = 6, + + DisplayedCellsExceptHeader = 8, + + DisplayedCells = 10, + + Fill = 16 + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/BackUpSaveBinidngModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/BackUpSaveBinidngModel.cs new file mode 100644 index 0000000..fb069d9 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/BackUpSaveBinidngModel.cs @@ -0,0 +1,7 @@ +namespace CarpentryWorkshopContracts.BindingModels +{ + public class BackUpSaveBinidngModel + { + public string FolderName { get; set; } = string.Empty; + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/MessageInfoBindingModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/MessageInfoBindingModel.cs index 4d31cfc..348ff02 100644 --- a/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/MessageInfoBindingModel.cs +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/MessageInfoBindingModel.cs @@ -15,5 +15,6 @@ namespace CarpentryWorkshopContracts.BindingModels public string Subject { get; set; } = string.Empty; public string Body { get; set; } = string.Empty; public DateTime DateDelivery { get; set; } - } + public int Id => throw new NotImplementedException(); + } } diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IBackUpLogic.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IBackUpLogic.cs new file mode 100644 index 0000000..94da600 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IBackUpLogic.cs @@ -0,0 +1,9 @@ +using CarpentryWorkshopContracts.BindingModels; + +namespace CarpentryWorkshopContracts.BusinessLogicsContracts +{ + public interface IBackUpLogic + { + void CreateBackUp(BackUpSaveBinidngModel model); + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/CarpentryWorkshopContracts.csproj b/CarpentryWorkshop/CarpentryWorkshopContracts/CarpentryWorkshopContracts.csproj index 35e5221..5df25ea 100644 --- a/CarpentryWorkshop/CarpentryWorkshopContracts/CarpentryWorkshopContracts.csproj +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/CarpentryWorkshopContracts.csproj @@ -6,6 +6,10 @@ enable + + + + diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/DI/DependencyManager.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/DI/DependencyManager.cs new file mode 100644 index 0000000..d14f511 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/DI/DependencyManager.cs @@ -0,0 +1,61 @@ +using Microsoft.Extensions.Logging; + +namespace CarpentryWorkshopContracts.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/CarpentryWorkshop/CarpentryWorkshopContracts/DI/IDependencyContainer.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/DI/IDependencyContainer.cs new file mode 100644 index 0000000..6d2c21c --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/DI/IDependencyContainer.cs @@ -0,0 +1,35 @@ +using Microsoft.Extensions.Logging; + +namespace CarpentryWorkshopContracts.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/CarpentryWorkshop/CarpentryWorkshopContracts/DI/IImplementationExtension.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/DI/IImplementationExtension.cs new file mode 100644 index 0000000..d24701c --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/DI/IImplementationExtension.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarpentryWorkshopContracts.DI +{ + public interface IImplementationExtension + { + public int Priority { get; } + /// + /// Регистрация сервисов + /// + public void RegisterServices(); + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/DI/ServiceDependencyContainer.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/DI/ServiceDependencyContainer.cs new file mode 100644 index 0000000..a8e43bb --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/DI/ServiceDependencyContainer.cs @@ -0,0 +1,57 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace CarpentryWorkshopContracts.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/CarpentryWorkshop/CarpentryWorkshopContracts/DI/ServiceProviderLoader.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/DI/ServiceProviderLoader.cs new file mode 100644 index 0000000..9345d03 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/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 CarpentryWorkshopContracts.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/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IBackUpInfo.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IBackUpInfo.cs new file mode 100644 index 0000000..321d51e --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IBackUpInfo.cs @@ -0,0 +1,9 @@ +namespace CarpentryWorkshopContracts.StoragesContracts +{ + public interface IBackUpInfo + { + List? GetList() where T : class, new(); + + Type? GetTypeByModelInterface(string modelInterfaceName); + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ClientViewModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ClientViewModel.cs index a38c6c5..a63a205 100644 --- a/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ClientViewModel.cs +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ClientViewModel.cs @@ -1,4 +1,5 @@ using CarpentryWorkshopDataModels.Models; +using CarpentryWorkshopContracts.Attributes; using System; using System.Collections.Generic; using System.ComponentModel; @@ -11,15 +12,16 @@ namespace CarpentryWorkshopContracts.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/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ComponentViewModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ComponentViewModel.cs index 0c17489..a4ba9aa 100644 --- a/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ComponentViewModel.cs +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ComponentViewModel.cs @@ -1,4 +1,5 @@ -using CarpentryWorkshopDataModels.Models; +using CarpentryWorkshopContracts.Attributes; +using CarpentryWorkshopDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel; @@ -10,10 +11,11 @@ namespace CarpentryWorkshopContracts.ViewModels { public class ComponentViewModel : IComponentModel { + [Column(visible: false)] public int Id { get; set; } - [DisplayName("Название компонента")] + [Column(title: "Название компонента", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string ComponentName { get; set; } = string.Empty; - [DisplayName("Цена")] + [Column(title: "Цена", width: 150)] public double Cost { get; set; } } diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ImplementerViewModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ImplementerViewModel.cs index 5b0073d..b18a1a1 100644 --- a/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ImplementerViewModel.cs +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ImplementerViewModel.cs @@ -1,4 +1,5 @@ -using System; +using CarpentryWorkshopContracts.Attributes; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; @@ -9,18 +10,19 @@ namespace CarpentryWorkshopContracts.ViewModels { public class ImplementerViewModel { - public int Id { get; set; } + [Column(visible: false)] + public int Id { get; set; } - [DisplayName("ФИО исполнителя")] - public string ImplementerFIO { get; set; } = string.Empty; + [Column(title: "ФИО исполнителя", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] + public string ImplementerFIO { get; set; } = string.Empty; - [DisplayName("Пароль")] - public string Password { get; set; } = string.Empty; + [Column(title: "Пароль", width: 100)] + public string Password { get; set; } = string.Empty; - [DisplayName("Стаж работы")] - public int WorkExperience { get; set; } + [Column(title: "Стаж работы", width: 60)] + public int WorkExperience { get; set; } - [DisplayName("Квалификация")] - public int Qualification { get; set; } + [Column(title: "Квалификация", width: 60)] + public int Qualification { get; set; } } } diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/MessageInfoViewModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/MessageInfoViewModel.cs index 597eed5..84a4da6 100644 --- a/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/MessageInfoViewModel.cs +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/MessageInfoViewModel.cs @@ -1,24 +1,29 @@ -using CarpentryWorkshopDataModels.Models; +using CarpentryWorkshopContracts.Attributes; +using CarpentryWorkshopDataModels.Models; using System.ComponentModel; namespace CarpentryWorkshopContracts.ViewModels { public class MessageInfoViewModel : IMessageInfoModel { + [Column(visible: false)] + public int Id { get; set; } + [Column(visible: false)] public string MessageId { get; set; } = string.Empty; + [Column(visible: false)] public int? ClientId { get; set; } - [DisplayName("Отправитель")] + [Column(title: "Отправитель", width: 150)] public string SenderName { get; set; } = string.Empty; - [DisplayName("Дата письма")] + [Column(title: "Дата письма", width: 120)] public DateTime DateDelivery { get; set; } - [DisplayName("Заголовок")] + [Column(title: "Заголовок", width: 120)] public string Subject { get; set; } = string.Empty; - [DisplayName("Текст")] + [Column(title: "Текст", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string Body { get; set; } = string.Empty; } } \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/OrderViewModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/OrderViewModel.cs index 4b19e26..f7dd4bd 100644 --- a/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/OrderViewModel.cs +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/OrderViewModel.cs @@ -1,4 +1,5 @@ -using CarpentryWorkshopDataModels.Enums; +using CarpentryWorkshopContracts.Attributes; +using CarpentryWorkshopDataModels.Enums; using CarpentryWorkshopDataModels.Models; using System; using System.Collections.Generic; @@ -12,28 +13,32 @@ namespace CarpentryWorkshopContracts.ViewModels { public class OrderViewModel : IOrderModel { - [DisplayName("Номер")] + [Column(title: "Номер", width: 90)] public int Id { get; set; } + [Column(visible: false)] public int WoodId { get; set; } + [Column(visible: false)] public int ClientId { get; set; } + [Column(visible: false)] public int? ImplementerId { get; set; } - [DisplayName("Фамилия клиента")] + [Column(title: "Имя клиента", width: 190)] public string ClientFIO { get; set; } = string.Empty; - [DisplayName("Фамилия исполнителя")] + [Column(title: "Исполнитель", width: 150)] public string ImplementerFIO { get; set; } = string.Empty; - [DisplayName("Изделие")] + [Column(title: "Изделие", width: 150)] public string WoodName { get; set; } = string.Empty; - [DisplayName("Количество")] + + [Column(title: "Количество", width: 100)] public int Count { get; set; } - [DisplayName("Сумма")] + [Column(title: "Сумма", width: 120)] public double Sum { get; set; } - [DisplayName("Статус")] + [Column(title: "Статус", width: 70)] public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - [DisplayName("Дата создания")] + [Column(title: "Дата создания", width: 120)] public DateTime DateCreate { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); - [DisplayName("Дата выполнения")] + [Column(title: "Дата выполнения", width: 120)] public DateTime? DateImplement { get; set; } } } diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/WoodViewModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/WoodViewModel.cs index 6c8ce4e..c3fba9c 100644 --- a/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/WoodViewModel.cs +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/WoodViewModel.cs @@ -1,4 +1,5 @@ -using CarpentryWorkshopDataModels.Models; +using CarpentryWorkshopContracts.Attributes; +using CarpentryWorkshopDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel; @@ -10,11 +11,16 @@ namespace CarpentryWorkshopContracts.ViewModels { public class WoodViewModel : IWoodModel { + [Column(visible: false)] public int Id { get; set; } - [DisplayName("Название изделия")] + + [Column(title: "Название изделия", gridViewAutoSize: GridViewAutoSize.Fill, isUseAutoSize: true)] public string WoodName { get; set; } = string.Empty; - [DisplayName("Цена")] + + [Column(title: "Цена", width: 70)] public double Price { get; set; } + + [Column(visible: false)] public Dictionary WoodComponents { get; diff --git a/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IMessageInfoModel.cs b/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IMessageInfoModel.cs index fec8b6a..9e4141e 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IMessageInfoModel.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IMessageInfoModel.cs @@ -1,6 +1,6 @@ namespace CarpentryWorkshopDataModels.Models { - public interface IMessageInfoModel + public interface IMessageInfoModel : IId { string MessageId { get; } int? ClientId { get; } diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabaseImplement.csproj b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabaseImplement.csproj index 1ffdf41..51101d7 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabaseImplement.csproj +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabaseImplement.csproj @@ -20,4 +20,8 @@ + + + + diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/ImplementationExtension.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/ImplementationExtension.cs new file mode 100644 index 0000000..b8ee962 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/ImplementationExtension.cs @@ -0,0 +1,22 @@ +using CarpentryWorkshopContracts.DI; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopDatabaseImplement.Implements; + +namespace CarpentryWorkshopDatabaseImplement +{ + public class ImplementationExtension : IImplementationExtension + { + public int Priority => 3; + + 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/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/BackUpInfo.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/BackUpInfo.cs new file mode 100644 index 0000000..00a366c --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/BackUpInfo.cs @@ -0,0 +1,27 @@ +using CarpentryWorkshopContracts.StoragesContracts; + +namespace CarpentryWorkshopDatabaseImplement.Implements +{ + public class BackUpInfo : IBackUpInfo + { + public List? GetList() where T : class, new() + { + using var context = new CarpentryWorkshopDatabase(); + 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/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Client.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Client.cs index 3eaef60..045c116 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Client.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Client.cs @@ -8,19 +8,25 @@ using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Runtime.Serialization; namespace CarpentryWorkshopDatabaseImplement.Models { + [DataContract] public class Client : IClientModel { + [DataMember] public int Id { get; set; } + [DataMember] [Required] public string ClientFIO { get; set; } = string.Empty; + [DataMember] [Required] public string Email { get; set; } = string.Empty; + [DataMember] [Required] public string Password { get; set; } = string.Empty; diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Component.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Component.cs index 58551a4..4a38480 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Component.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Component.cs @@ -3,16 +3,21 @@ using CarpentryWorkshopContracts.ViewModels; using CarpentryWorkshopDataModels.Models; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using System.Runtime.Serialization; namespace CarpentryWorkshopDatabaseImplement.Models { + [DataContract] public class Component : IComponentModel { + [DataMember] public int Id { get; private set; } + [DataMember] [Required] public string ComponentName { get; private set; } = string.Empty; + [DataMember] [Required] public double Cost { get; set; } diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Implementer.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Implementer.cs index b3d362b..63d5573 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Implementer.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Implementer.cs @@ -8,23 +8,30 @@ using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Runtime.Serialization; namespace CarpentryWorkshopDatabaseImplement.Models { - public class Implementer : IImplementerModel + [DataContract] + public class Implementer : IImplementerModel { - public int Id { get; set; } + [DataMember] + public int Id { get; set; } - [Required] + [DataMember] + [Required] public string ImplementerFIO { get; set; } = string.Empty; - [Required] + [DataMember] + [Required] public string Password { get; set; } = string.Empty; - [Required] + [DataMember] + [Required] public int WorkExperience { get; set; } - [Required] + [DataMember] + [Required] public int Qualification { get; set; } [ForeignKey("ImplementerId")] diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/MessageInfo.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/MessageInfo.cs index bdfa859..e911615 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/MessageInfo.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/MessageInfo.cs @@ -6,27 +6,42 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; +using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; namespace CarpentryWorkshopDatabaseImplement.Models { - public class MessageInfo : IMessageInfoModel + [DataContract] + public class MessageInfo : IMessageInfoModel { - [Key] + [NotMapped] + public int Id { get; private set; } + + [DataMember] + [Key] public string MessageId { get; set; } = string.Empty; - public int? ClientId { get; set; } + [DataMember] + public int? ClientId { get; set; } public virtual Client? Client { get; set; } - public string SenderName { get; set; } = string.Empty; + [DataMember] + [Required] + public string SenderName { get; set; } = string.Empty; - public DateTime DateDelivery { get; set; } + [DataMember] + [Required] + public DateTime DateDelivery { get; set; } - public string Subject { get; set; } = string.Empty; + [DataMember] + [Required] + public string Subject { get; set; } = string.Empty; - public string Body { get; set; } = string.Empty; + [DataMember] + [Required] + public string Body { get; set; } = string.Empty; public static MessageInfo? Create(MessageInfoBindingModel? model) { diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Order.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Order.cs index 4344258..1547ac0 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Order.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Order.cs @@ -6,32 +6,44 @@ using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Reflection.Metadata; +using System.Runtime.Serialization; namespace CarpentryWorkshopDatabaseImplement.Models { + [DataContract] public class Order : IOrderModel { + [DataMember] public int Id { get; private set; } - [ForeignKey("WoodId")] + + [DataMember] + [Required] public int WoodId { get; private set; } - [ForeignKey("ImplementerId")] + + [DataMember] public int? ImplementerId { get; private set; } - [ForeignKey("ClientId")] + + [DataMember] [Required] public int ClientId { get; set; } + [DataMember] [Required] public int Count { get; private set; } + [DataMember] [Required] public double Sum { get; private set; } + [DataMember] [Required] public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + [DataMember] [Required] public DateTime DateCreate { get; private set; } + [DataMember] public DateTime? DateImplement { get; private set; } public virtual Wood Wood { get; set; } diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Wood.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Wood.cs index 6d01520..ef61822 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Wood.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Wood.cs @@ -3,14 +3,21 @@ using CarpentryWorkshopContracts.ViewModels; using CarpentryWorkshopDataModels.Models; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using System.Runtime.Serialization; namespace CarpentryWorkshopDatabaseImplement.Models { + [DataContract] public class Wood : IWoodModel { + [DataMember] public int Id { get; set; } + + [DataMember] [Required] public string WoodName { get; set; } = string.Empty; + + [DataMember] [Required] public double Price { get; set; } private Dictionary? _woodComponents = null; diff --git a/CarpentryWorkshop/CarpentryWorkshopFileImplement/CarpentryWorkshopFileImplement.csproj b/CarpentryWorkshop/CarpentryWorkshopFileImplement/CarpentryWorkshopFileImplement.csproj index 3e364d0..48468e3 100644 --- a/CarpentryWorkshop/CarpentryWorkshopFileImplement/CarpentryWorkshopFileImplement.csproj +++ b/CarpentryWorkshop/CarpentryWorkshopFileImplement/CarpentryWorkshopFileImplement.csproj @@ -11,4 +11,8 @@ + + + + diff --git a/CarpentryWorkshop/CarpentryWorkshopFileImplement/ImplementationExtension.cs b/CarpentryWorkshop/CarpentryWorkshopFileImplement/ImplementationExtension.cs new file mode 100644 index 0000000..ad3948a --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopFileImplement/ImplementationExtension.cs @@ -0,0 +1,22 @@ +using CarpentryWorkshopContracts.DI; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopFileImplement.Implements; + +namespace CarpentryWorkshopFileImplement +{ + public class ImplementationExtension : 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/CarpentryWorkshop/CarpentryWorkshopFileImplement/Implements/BackUpInfo.cs b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Implements/BackUpInfo.cs new file mode 100644 index 0000000..b034300 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Implements/BackUpInfo.cs @@ -0,0 +1,39 @@ +using CarpentryWorkshopContracts.StoragesContracts; +using System.Reflection; + +namespace CarpentryWorkshopFileImplement.Implements +{ + public class BackUpInfo : IBackUpInfo + { + private readonly DataFileSingleton source; + private readonly PropertyInfo[] sourceProperties; + + public BackUpInfo() + { + source = DataFileSingleton.GetInstance(); + sourceProperties = source.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); + } + + public List? GetList() where T : class, new() + { + var requredType = typeof(T); + return (List?)sourceProperties.FirstOrDefault(x => x.PropertyType.IsGenericType && x.PropertyType.GetGenericArguments()[0] == requredType) + ?.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/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Client.cs b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Client.cs index 27f976b..3baa0f1 100644 --- a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Client.cs +++ b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Client.cs @@ -4,17 +4,26 @@ using CarpentryWorkshopDataModels.Models; using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; namespace CarpentryWorkshopFileImplement.Models { + [DataContract] public class Client : IClientModel { + [DataMember] public int Id { get; private set; } + + [DataMember] public string ClientFIO { get; private set; } = string.Empty; + + [DataMember] public string Email { get; private set; } = string.Empty; + + [DataMember] public string Password { get; private set; } = string.Empty; public static Client? Create(ClientBindingModel model) diff --git a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Component.cs b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Component.cs index 5fa39c1..c3494b7 100644 --- a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Component.cs +++ b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Component.cs @@ -5,16 +5,23 @@ using CarpentryWorkshopDataModels.Models; using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; namespace CarpentryWorkshopFileImplement.Models { + [DataContract] public class Component : IComponentModel { + [DataMember] public int Id { get; private set; } + + [DataMember] public string ComponentName { get; private set; } = string.Empty; + + [DataMember] public double Cost { get; set; } public static Component? Create(ComponentBindingModel model) { diff --git a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Implementer.cs b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Implementer.cs index f72f531..6070936 100644 --- a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Implementer.cs +++ b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Implementer.cs @@ -4,23 +4,30 @@ using CarpentryWorkshopDataModels.Models; using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; namespace CarpentryWorkshopFileImplement.Models { - public class Implementer : IImplementerModel + [DataContract] + public class Implementer : IImplementerModel { - public int Id { get; private set; } + [DataMember] + public int Id { get; private set; } - public string ImplementerFIO { get; private set; } = string.Empty; + [DataMember] + public string ImplementerFIO { get; private set; } = string.Empty; - public string Password { get; private set; } = string.Empty; + [DataMember] + public string Password { get; private set; } = string.Empty; - public int WorkExperience { get; private set; } + [DataMember] + public int WorkExperience { get; private set; } - public int Qualification { get; private set; } + [DataMember] + public int Qualification { get; private set; } public static Implementer? Create(ImplementerBindingModel model) { diff --git a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Message.cs b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Message.cs index 7c5271b..b1c94c5 100644 --- a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Message.cs +++ b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Message.cs @@ -1,22 +1,33 @@ using CarpentryWorkshopContracts.BindingModels; using CarpentryWorkshopContracts.ViewModels; using CarpentryWorkshopDataModels.Models; +using System.Runtime.Serialization; using System.Xml.Linq; namespace CarpentryWorkshopFileImplement.Models { + [DataContract] public class Message : IMessageInfoModel { + [DataMember] + public int Id { get; private set; } + + [DataMember] public string MessageId { get; private set; } = string.Empty; + [DataMember] public int? ClientId { get; private set; } + [DataMember] public string SenderName { get; private set; } = string.Empty; + [DataMember] public DateTime DateDelivery { get; private set; } + [DataMember] public string Subject { get; private set; } = string.Empty; + [DataMember] public string Body { get; private set; } = string.Empty; public static Message? Create(MessageInfoBindingModel model) diff --git a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Order.cs b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Order.cs index b572582..208f00f 100644 --- a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Order.cs +++ b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Order.cs @@ -11,27 +11,38 @@ using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; +using System.Runtime.Serialization; namespace CarpentryWorkshopFileImplement.Models { + [DataContract] public class Order : IOrderModel { + [DataMember] public int WoodId { get; private set; } + [DataMember] public int? ImplementerId { get; set; } + [DataMember] public int ClientId { get; private set; } + [DataMember] public int Count { get; private set; } + [DataMember] public double Sum { get; private set; } + [DataMember] public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + [DataMember] public DateTime DateCreate { get; private set; } = DateTime.Now; + [DataMember] public DateTime? DateImplement { get; private set; } + [DataMember] public int Id { get; private set; } public static Order? Create(OrderBindingModel model) diff --git a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Wood.cs b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Wood.cs index 1fe898c..73e5a09 100644 --- a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Wood.cs +++ b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Wood.cs @@ -4,19 +4,28 @@ using CarpentryWorkshopDataModels.Models; using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; namespace CarpentryWorkshopFileImplement.Models { + [DataContract] public class Wood : IWoodModel { + [DataMember] public int Id { get; private set; } + + [DataMember] public string WoodName { get; private set; } = string.Empty; + + [DataMember] public double Price { get; private set; } public Dictionary Components { get; private set; } = new(); private Dictionary? _woodComponents = null; + + [DataMember] public Dictionary WoodComponents { get diff --git a/CarpentryWorkshop/CarpentryWorkshopListImplement/CarpentryWorkshopListImplement.csproj b/CarpentryWorkshop/CarpentryWorkshopListImplement/CarpentryWorkshopListImplement.csproj index af423c7..611cdad 100644 --- a/CarpentryWorkshop/CarpentryWorkshopListImplement/CarpentryWorkshopListImplement.csproj +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/CarpentryWorkshopListImplement.csproj @@ -11,4 +11,8 @@ + + + + diff --git a/CarpentryWorkshop/CarpentryWorkshopListImplement/ImplementationExtension.cs b/CarpentryWorkshop/CarpentryWorkshopListImplement/ImplementationExtension.cs new file mode 100644 index 0000000..33ac3f2 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/ImplementationExtension.cs @@ -0,0 +1,27 @@ +using CarpentryWorkshopContracts.DI; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopListImplement.Implements; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarpentryWorkshopListImplement +{ + internal class ImplementationExtension : 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/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/BackUpInfo.cs b/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/BackUpInfo.cs new file mode 100644 index 0000000..8ff23f4 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/BackUpInfo.cs @@ -0,0 +1,22 @@ +using CarpentryWorkshopContracts.StoragesContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarpentryWorkshopListImplement.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/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Message.cs b/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Message.cs index a785872..4700999 100644 --- a/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Message.cs +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Message.cs @@ -6,6 +6,7 @@ namespace CarpentryWorkshopListImplement.Models { public class Message : IMessageInfoModel { + public int Id => throw new NotImplementedException(); public string MessageId { get; private set; } = string.Empty; public int? ClientId { get; private set; } diff --git a/CarpentryWorkshop/CarpentryWorkshopRestApi/CarpentryWorkshopRestApi.csproj b/CarpentryWorkshop/CarpentryWorkshopRestApi/CarpentryWorkshopRestApi.csproj index faca581..5c11c3b 100644 --- a/CarpentryWorkshop/CarpentryWorkshopRestApi/CarpentryWorkshopRestApi.csproj +++ b/CarpentryWorkshop/CarpentryWorkshopRestApi/CarpentryWorkshopRestApi.csproj @@ -13,6 +13,7 @@ + diff --git a/CarpentryWorkshop/CarpentryWorkshopView/CarpentryWorkshopView.csproj b/CarpentryWorkshop/CarpentryWorkshopView/CarpentryWorkshopView.csproj index b51aefe..0dd5705 100644 --- a/CarpentryWorkshop/CarpentryWorkshopView/CarpentryWorkshopView.csproj +++ b/CarpentryWorkshop/CarpentryWorkshopView/CarpentryWorkshopView.csproj @@ -13,6 +13,9 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + + + @@ -23,6 +26,8 @@ + + \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopView/DataGridViewExtension.cs b/CarpentryWorkshop/CarpentryWorkshopView/DataGridViewExtension.cs new file mode 100644 index 0000000..d9e9d7b --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopView/DataGridViewExtension.cs @@ -0,0 +1,46 @@ +using CarpentryWorkshopContracts.Attributes; + +namespace CarpentryWorkshopView +{ + 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/CarpentryWorkshop/CarpentryWorkshopView/FormClients.cs b/CarpentryWorkshop/CarpentryWorkshopView/FormClients.cs index 12d146c..b5a14af 100644 --- a/CarpentryWorkshop/CarpentryWorkshopView/FormClients.cs +++ b/CarpentryWorkshop/CarpentryWorkshopView/FormClients.cs @@ -30,15 +30,7 @@ namespace CarpentryWorkshopView { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns[0].Visible = false; - dataGridView.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - dataGridView.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - dataGridView.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } + dataGridView.FillAndConfigGrid(_logic.ReadList(null)); } catch (Exception ex) { diff --git a/CarpentryWorkshop/CarpentryWorkshopView/FormComponents.cs b/CarpentryWorkshop/CarpentryWorkshopView/FormComponents.cs index b6fe4f3..d58b842 100644 --- a/CarpentryWorkshop/CarpentryWorkshopView/FormComponents.cs +++ b/CarpentryWorkshop/CarpentryWorkshopView/FormComponents.cs @@ -1,5 +1,6 @@ using CarpentryWorkshopContracts.BindingModels; using CarpentryWorkshopContracts.BusinessLogicsContracts; +using CarpentryWorkshopContracts.DI; using CarpentryWorkshopContracts.SearchModels; using CarpentryWorkshopDataModels.Models; using Microsoft.Extensions.Logging; @@ -51,27 +52,22 @@ namespace CarpentryWorkshopView } private void ButtonAdd_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormComponent)); - if (service is FormComponent form) + var form = DependencyManager.Instance.Resolve(); + if (form.ShowDialog() == DialogResult.OK) { - if (form.ShowDialog() == DialogResult.OK) - { - LoadData(); - } + LoadData(); } } private void ButtonUpd_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count == 1) { - var service = Program.ServiceProvider?.GetService(typeof(FormComponent)); - if (service is FormComponent 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/CarpentryWorkshop/CarpentryWorkshopView/FormImplementers.cs b/CarpentryWorkshop/CarpentryWorkshopView/FormImplementers.cs index f2c9230..6438d67 100644 --- a/CarpentryWorkshop/CarpentryWorkshopView/FormImplementers.cs +++ b/CarpentryWorkshop/CarpentryWorkshopView/FormImplementers.cs @@ -1,5 +1,6 @@ using CarpentryWorkshopContracts.BindingModels; using CarpentryWorkshopContracts.BusinessLogicsContracts; +using CarpentryWorkshopContracts.DI; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -34,13 +35,7 @@ namespace CarpentryWorkshopView { try { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridViewImplementers.DataSource = list; - dataGridViewImplementers.Columns["Id"].Visible = false; - dataGridViewImplementers.Columns["ImplementerFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } + dataGridViewImplementers.FillAndConfigGrid(_logic.ReadList(null)); _logger.LogInformation("Загрузка исполнителей"); } catch (Exception ex) @@ -52,13 +47,10 @@ namespace CarpentryWorkshopView 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(); } } @@ -66,14 +58,11 @@ namespace CarpentryWorkshopView { if (dataGridViewImplementers.SelectedRows.Count == 1) { - var service = Program.ServiceProvider?.GetService(typeof(FormImplementer)); - if (service is FormImplementer form) + var form = DependencyManager.Instance.Resolve(); + form.Id = Convert.ToInt32(dataGridViewImplementers.SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) { - form.Id = Convert.ToInt32(dataGridViewImplementers.SelectedRows[0].Cells["Id"].Value); - if (form.ShowDialog() == DialogResult.OK) - { - LoadData(); - } + LoadData(); } } } diff --git a/CarpentryWorkshop/CarpentryWorkshopView/FormMail.cs b/CarpentryWorkshop/CarpentryWorkshopView/FormMail.cs index 29ed5dd..5b42d6a 100644 --- a/CarpentryWorkshop/CarpentryWorkshopView/FormMail.cs +++ b/CarpentryWorkshop/CarpentryWorkshopView/FormMail.cs @@ -29,14 +29,7 @@ namespace CarpentryWorkshopView { 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/CarpentryWorkshop/CarpentryWorkshopView/FormMain.Designer.cs b/CarpentryWorkshop/CarpentryWorkshopView/FormMain.Designer.cs index 8befce7..2bf4e39 100644 --- a/CarpentryWorkshop/CarpentryWorkshopView/FormMain.Designer.cs +++ b/CarpentryWorkshop/CarpentryWorkshopView/FormMain.Designer.cs @@ -39,20 +39,21 @@ списокЗаказовToolStripMenuItem = new ToolStripMenuItem(); запускРаботToolStripMenuItem = new ToolStripMenuItem(); исполнителиToolStripMenuItem = new ToolStripMenuItem(); + письмаToolStripMenuItem = new ToolStripMenuItem(); dataGridView = new DataGridView(); ButtonCreateOrder = new Button(); ButtonTakeOrderInWork = new Button(); ButtonOrderReady = new Button(); ButtonIssuedOrder = new Button(); ButtonRef = new Button(); - письмаToolStripMenuItem = new ToolStripMenuItem(); + создатьБэкапToolStripMenuItem = new ToolStripMenuItem(); menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); // // menuStrip1 // - menuStrip1.Items.AddRange(new ToolStripItem[] { справочникиToolStripMenuItem, отчетыToolStripMenuItem, запускРаботToolStripMenuItem, исполнителиToolStripMenuItem, письмаToolStripMenuItem }); + menuStrip1.Items.AddRange(new ToolStripItem[] { справочникиToolStripMenuItem, отчетыToolStripMenuItem, запускРаботToolStripMenuItem, исполнителиToolStripMenuItem, письмаToolStripMenuItem, создатьБэкапToolStripMenuItem }); menuStrip1.Location = new Point(0, 0); menuStrip1.Name = "menuStrip1"; menuStrip1.Size = new Size(1174, 24); @@ -129,6 +130,13 @@ исполнителиToolStripMenuItem.Text = "Исполнители"; исполнителиToolStripMenuItem.Click += исполнителиToolStripMenuItem_Click; // + // письмаToolStripMenuItem + // + письмаToolStripMenuItem.Name = "письмаToolStripMenuItem"; + письмаToolStripMenuItem.Size = new Size(62, 20); + письмаToolStripMenuItem.Text = "Письма"; + письмаToolStripMenuItem.Click += письмаToolStripMenuItem_Click; + // // dataGridView // dataGridView.BackgroundColor = SystemColors.ButtonHighlight; @@ -190,12 +198,12 @@ ButtonRef.UseVisualStyleBackColor = true; ButtonRef.Click += ButtonRef_Click; // - // письмаToolStripMenuItem + // создатьБэкапToolStripMenuItem // - письмаToolStripMenuItem.Name = "письмаToolStripMenuItem"; - письмаToolStripMenuItem.Size = new Size(62, 20); - письмаToolStripMenuItem.Text = "Письма"; - письмаToolStripMenuItem.Click += письмаToolStripMenuItem_Click; + создатьБэкапToolStripMenuItem.Name = "создатьБэкапToolStripMenuItem"; + создатьБэкапToolStripMenuItem.Size = new Size(97, 20); + создатьБэкапToolStripMenuItem.Text = "Создать Бэкап"; + создатьБэкапToolStripMenuItem.Click += createBackUpToolStripMenuItem_Click; // // FormMain // @@ -239,5 +247,6 @@ private ToolStripMenuItem запускРаботToolStripMenuItem; private ToolStripMenuItem исполнителиToolStripMenuItem; private ToolStripMenuItem письмаToolStripMenuItem; + private ToolStripMenuItem создатьБэкапToolStripMenuItem; } } \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopView/FormMain.cs b/CarpentryWorkshop/CarpentryWorkshopView/FormMain.cs index d4c1c41..974aa40 100644 --- a/CarpentryWorkshop/CarpentryWorkshopView/FormMain.cs +++ b/CarpentryWorkshop/CarpentryWorkshopView/FormMain.cs @@ -1,6 +1,7 @@ using CarpentryWorkshopBusinessLogic.BusinessLogics; using CarpentryWorkshopContracts.BindingModels; using CarpentryWorkshopContracts.BusinessLogicsContracts; +using CarpentryWorkshopContracts.DI; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -20,13 +21,15 @@ namespace CarpentryWorkshopView private readonly IOrderLogic _orderLogic; private readonly IReportLogic _reportLogic; private readonly IWorkProcess _workProcess; - public FormMain(ILogger logger, IOrderLogic orderLogic, IReportLogic reportLogic, IWorkProcess workProcess) + private readonly IBackUpLogic _backUpLogic; + 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) { @@ -38,14 +41,7 @@ namespace CarpentryWorkshopView // прописать логику try { - var list = _orderLogic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns[1].Visible = false; - dataGridView.Columns[2].Visible = false; - dataGridView.Columns[3].Visible = false; - } + dataGridView.FillAndConfigGrid(_orderLogic.ReadList(null)); _logger.LogInformation("Загрузка продуктов"); } catch (Exception ex) @@ -56,29 +52,19 @@ namespace CarpentryWorkshopView } private void КомпонентыToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormComponents)); - if (service is FormComponents form) - { - form.ShowDialog(); - } + var form = DependencyManager.Instance.Resolve(); + form.ShowDialog(); } private void ИзделияToolStripMenuItem_Click(object sender, EventArgs e) { - // прописать логику - var service = Program.ServiceProvider?.GetService(typeof(FormWoods)); - if (service is FormWoods 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) { @@ -162,48 +148,57 @@ namespace CarpentryWorkshopView private void изделияПоКомпонентамToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormReportWoodComponents)); - if (service is FormReportWoodComponents form) - { - form.ShowDialog(); - } + var form = DependencyManager.Instance.Resolve(); + form.ShowDialog(); } private void списокЗаказовToolStripMenuItem_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 clientsToolStripMenuItem_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) { - _workProcess.DoWork((Program.ServiceProvider?.GetService(typeof(IImplementerLogic)) as IImplementerLogic)!, _orderLogic); + _workProcess.DoWork(DependencyManager.Instance.Resolve(), _orderLogic); MessageBox.Show("Процесс обработки запущен", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); } 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) { - var service = Program.ServiceProvider?.GetService(typeof(FormMail)); - if (service is FormMail form) + var form = DependencyManager.Instance.Resolve(); + form.ShowDialog(); + } + private void createBackUpToolStripMenuItem_Click(object sender, EventArgs e) + { + try { - form.ShowDialog(); + 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/CarpentryWorkshop/CarpentryWorkshopView/FormWood.cs b/CarpentryWorkshop/CarpentryWorkshopView/FormWood.cs index 6032584..ac23465 100644 --- a/CarpentryWorkshop/CarpentryWorkshopView/FormWood.cs +++ b/CarpentryWorkshop/CarpentryWorkshopView/FormWood.cs @@ -1,5 +1,6 @@ using CarpentryWorkshopContracts.BindingModels; using CarpentryWorkshopContracts.BusinessLogicsContracts; +using CarpentryWorkshopContracts.DI; using CarpentryWorkshopContracts.SearchModels; using CarpentryWorkshopDataModels.Models; using Microsoft.Extensions.Logging; @@ -75,51 +76,44 @@ namespace CarpentryWorkshopView } private void ButtonAdd_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormWoodComponent)); - if (service is FormWoodComponent form) + var form = DependencyManager.Instance.Resolve(); + if (form.ShowDialog() == DialogResult.OK) { + return; + } + _logger.LogInformation("Добавление нового компонента:{ ComponentName}-{ Count}", form.ComponentModel.ComponentName, form.Count); + if (_woodComponents.ContainsKey(form.Id)) + { + _woodComponents[form.Id] = (form.ComponentModel, + form.Count); + } + else + { + _woodComponents.Add(form.Id, (form.ComponentModel, + form.Count)); + } + LoadData(); + } + private void ButtonUpd_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var form = DependencyManager.Instance.Resolve(); + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value); + form.Id = id; + form.Count = _woodComponents[id].Item2; if (form.ShowDialog() == DialogResult.OK) { if (form.ComponentModel == null) { return; } - _logger.LogInformation("Добавление нового компонента: {ComponentName} - {Count}", form.ComponentModel.ComponentName, form.Count); - if (_woodComponents.ContainsKey(form.Id)) - { - _woodComponents[form.Id] = (form.ComponentModel, form.Count); - } - else - { - _woodComponents.Add(form.Id, (form.ComponentModel, form.Count)); - } + _logger.LogInformation("Изменение компонента:{ ComponentName}-{ Count}", form.ComponentModel.ComponentName, form.Count); + _woodComponents[form.Id] = (form.ComponentModel, form.Count); LoadData(); } } } - private void ButtonUpd_Click(object sender, EventArgs e) - { - if (dataGridView.SelectedRows.Count == 1) - { - var service = Program.ServiceProvider?.GetService(typeof(FormWoodComponent)); - if (service is FormWoodComponent form) - { - int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value); - form.Id = id; - form.Count = _woodComponents[id].Item2; - if (form.ShowDialog() == DialogResult.OK) - { - if (form.ComponentModel == null) - { - return; - } - _logger.LogInformation("Изменение компонента: {ComponentName} - {Count}", form.ComponentModel.ComponentName, form.Count); - _woodComponents[form.Id] = (form.ComponentModel, form.Count); - LoadData(); - } - } - } - } private void ButtonDel_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count == 1) diff --git a/CarpentryWorkshop/CarpentryWorkshopView/FormWoods.cs b/CarpentryWorkshop/CarpentryWorkshopView/FormWoods.cs index 4c96810..f99aca9 100644 --- a/CarpentryWorkshop/CarpentryWorkshopView/FormWoods.cs +++ b/CarpentryWorkshop/CarpentryWorkshopView/FormWoods.cs @@ -1,5 +1,6 @@ using CarpentryWorkshopContracts.BindingModels; using CarpentryWorkshopContracts.BusinessLogicsContracts; +using CarpentryWorkshopContracts.DI; using CarpentryWorkshopContracts.SearchModels; using CarpentryWorkshopDataModels.Models; using Microsoft.Extensions.Logging; @@ -37,43 +38,34 @@ namespace CarpentryWorkshopView var list = _logic.ReadList(null); if (list != null) { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["WoodName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - dataGridView.Columns[3].Visible = false; + dataGridView.FillAndConfigGrid(_logic.ReadList(null)); } - _logger.LogInformation("Загрузка продуктов"); + _logger.LogInformation("Загрузка изделий"); } catch (Exception ex) { - _logger.LogError(ex, "Ошибка загрузки продуктов"); + _logger.LogError(ex, "Ошибка загрузки изделий"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void ButtonAdd_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormWood)); - if (service is FormWood form) + var form = DependencyManager.Instance.Resolve(); + if (form.ShowDialog() == DialogResult.OK) { - if (form.ShowDialog() == DialogResult.OK) - { - LoadData(); - } + LoadData(); } } private void ButtonUpd_Click(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count == 1) { - var service = Program.ServiceProvider?.GetService(typeof(FormWood)); - if (service is FormWood 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/CarpentryWorkshop/CarpentryWorkshopView/Program.cs b/CarpentryWorkshop/CarpentryWorkshopView/Program.cs index 486b301..dc7c1b7 100644 --- a/CarpentryWorkshop/CarpentryWorkshopView/Program.cs +++ b/CarpentryWorkshop/CarpentryWorkshopView/Program.cs @@ -9,14 +9,13 @@ using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; using CarpentryWorkshopBusinessLogic.MailWorker; using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.DI; namespace CarpentryWorkshopView { internal static class Program { - private static ServiceProvider? _serviceProvider; - public static ServiceProvider? ServiceProvider => _serviceProvider; /// /// The main entry point for the application. /// @@ -26,12 +25,10 @@ namespace CarpentryWorkshopView // 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, @@ -46,53 +43,56 @@ namespace CarpentryWorkshopView } 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 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(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddSingleton(); + 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); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); - 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(); } } \ No newline at end of file