diff --git a/AbstractShop/AbstractShopBusinessLogic/BusinessLogics/BackUpLogic.cs b/AbstractShop/AbstractShopBusinessLogic/BusinessLogics/BackUpLogic.cs new file mode 100644 index 0000000..9653f44 --- /dev/null +++ b/AbstractShop/AbstractShopBusinessLogic/BusinessLogics/BackUpLogic.cs @@ -0,0 +1,98 @@ +using AbstractShopContracts.BindingModels; +using AbstractShopContracts.BusinessLogicsContracts; +using AbstractShopContracts.StoragesContracts; +using AbstractShopDataModels; +using Microsoft.Extensions.Logging; +using System.IO.Compression; +using System.Reflection; +using System.Runtime.Serialization.Json; + +namespace AbstractShopBusinessLogic.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); + } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopContracts/AbstractShopContracts.csproj b/AbstractShop/AbstractShopContracts/AbstractShopContracts.csproj index e652bca..9a4e1c1 100644 --- a/AbstractShop/AbstractShopContracts/AbstractShopContracts.csproj +++ b/AbstractShop/AbstractShopContracts/AbstractShopContracts.csproj @@ -6,6 +6,10 @@ enable + + + + diff --git a/AbstractShop/AbstractShopContracts/Attributes/ColumnAttribute.cs b/AbstractShop/AbstractShopContracts/Attributes/ColumnAttribute.cs new file mode 100644 index 0000000..ee2962f --- /dev/null +++ b/AbstractShop/AbstractShopContracts/Attributes/ColumnAttribute.cs @@ -0,0 +1,25 @@ +namespace AbstractShopContracts.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; + } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopContracts/Attributes/GridViewAutoSize.cs b/AbstractShop/AbstractShopContracts/Attributes/GridViewAutoSize.cs new file mode 100644 index 0000000..83bd453 --- /dev/null +++ b/AbstractShop/AbstractShopContracts/Attributes/GridViewAutoSize.cs @@ -0,0 +1,21 @@ +namespace AbstractShopContracts.Attributes +{ + public enum GridViewAutoSize + { + NotSet = 0, + + None = 1, + + ColumnHeader = 2, + + AllCellsExceptHeader = 4, + + AllCells = 6, + + DisplayedCellsExceptHeader = 8, + + DisplayedCells = 10, + + Fill = 16 + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopContracts/BindingModels/BackUpSaveBinidngModel.cs b/AbstractShop/AbstractShopContracts/BindingModels/BackUpSaveBinidngModel.cs new file mode 100644 index 0000000..9fe504f --- /dev/null +++ b/AbstractShop/AbstractShopContracts/BindingModels/BackUpSaveBinidngModel.cs @@ -0,0 +1,7 @@ +namespace AbstractShopContracts.BindingModels +{ + public class BackUpSaveBinidngModel + { + public string FolderName { get; set; } = string.Empty; + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopContracts/BindingModels/MessageInfoBindingModel.cs b/AbstractShop/AbstractShopContracts/BindingModels/MessageInfoBindingModel.cs index 16397de..568c037 100644 --- a/AbstractShop/AbstractShopContracts/BindingModels/MessageInfoBindingModel.cs +++ b/AbstractShop/AbstractShopContracts/BindingModels/MessageInfoBindingModel.cs @@ -15,5 +15,7 @@ namespace AbstractShopContracts.BindingModels public string Body { get; set; } = string.Empty; public DateTime DateDelivery { get; set; } + + public int Id => throw new NotImplementedException(); } } \ No newline at end of file diff --git a/AbstractShop/AbstractShopContracts/BusinessLogicsContracts/IBackUpLogic.cs b/AbstractShop/AbstractShopContracts/BusinessLogicsContracts/IBackUpLogic.cs new file mode 100644 index 0000000..8f36e95 --- /dev/null +++ b/AbstractShop/AbstractShopContracts/BusinessLogicsContracts/IBackUpLogic.cs @@ -0,0 +1,9 @@ +using AbstractShopContracts.BindingModels; + +namespace AbstractShopContracts.BusinessLogicsContracts +{ + public interface IBackUpLogic + { + void CreateBackUp(BackUpSaveBinidngModel model); + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopContracts/DI/DependencyManager.cs b/AbstractShop/AbstractShopContracts/DI/DependencyManager.cs new file mode 100644 index 0000000..33366e6 --- /dev/null +++ b/AbstractShop/AbstractShopContracts/DI/DependencyManager.cs @@ -0,0 +1,61 @@ +using Microsoft.Extensions.Logging; + +namespace AbstractShopContracts.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(); + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopContracts/DI/IDependencyContainer.cs b/AbstractShop/AbstractShopContracts/DI/IDependencyContainer.cs new file mode 100644 index 0000000..c6f4acc --- /dev/null +++ b/AbstractShop/AbstractShopContracts/DI/IDependencyContainer.cs @@ -0,0 +1,35 @@ +using Microsoft.Extensions.Logging; + +namespace AbstractShopContracts.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(); + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopContracts/DI/IImplementationExtension.cs b/AbstractShop/AbstractShopContracts/DI/IImplementationExtension.cs new file mode 100644 index 0000000..8a40af2 --- /dev/null +++ b/AbstractShop/AbstractShopContracts/DI/IImplementationExtension.cs @@ -0,0 +1,11 @@ +namespace AbstractShopContracts.DI +{ + public interface IImplementationExtension + { + public int Priority { get; } + /// + /// Регистрация сервисов + /// + public void RegisterServices(); + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopContracts/DI/ServiceDependencyContainer.cs b/AbstractShop/AbstractShopContracts/DI/ServiceDependencyContainer.cs new file mode 100644 index 0000000..f7817fd --- /dev/null +++ b/AbstractShop/AbstractShopContracts/DI/ServiceDependencyContainer.cs @@ -0,0 +1,57 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace AbstractShopContracts.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()!; + } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopContracts/DI/ServiceProviderLoader.cs b/AbstractShop/AbstractShopContracts/DI/ServiceProviderLoader.cs new file mode 100644 index 0000000..7600ca7 --- /dev/null +++ b/AbstractShop/AbstractShopContracts/DI/ServiceProviderLoader.cs @@ -0,0 +1,50 @@ +using System.Reflection; + +namespace AbstractShopContracts.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"; + } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopContracts/StoragesContracts/IBackUpInfo.cs b/AbstractShop/AbstractShopContracts/StoragesContracts/IBackUpInfo.cs new file mode 100644 index 0000000..712c644 --- /dev/null +++ b/AbstractShop/AbstractShopContracts/StoragesContracts/IBackUpInfo.cs @@ -0,0 +1,9 @@ +namespace AbstractShopContracts.StoragesContracts +{ + public interface IBackUpInfo + { + List? GetList() where T : class, new(); + + Type? GetTypeByModelInterface(string modelInterfaceName); + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopContracts/ViewModels/ClientViewModel.cs b/AbstractShop/AbstractShopContracts/ViewModels/ClientViewModel.cs index 9843f5d..e2507e8 100644 --- a/AbstractShop/AbstractShopContracts/ViewModels/ClientViewModel.cs +++ b/AbstractShop/AbstractShopContracts/ViewModels/ClientViewModel.cs @@ -1,19 +1,20 @@ -using AbstractShopDataModels.Models; -using System.ComponentModel; +using AbstractShopContracts.Attributes; +using AbstractShopDataModels.Models; namespace AbstractShopContracts.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; } } \ No newline at end of file diff --git a/AbstractShop/AbstractShopContracts/ViewModels/MessageInfoViewModel.cs b/AbstractShop/AbstractShopContracts/ViewModels/MessageInfoViewModel.cs index d84d6d5..f3419e2 100644 --- a/AbstractShop/AbstractShopContracts/ViewModels/MessageInfoViewModel.cs +++ b/AbstractShop/AbstractShopContracts/ViewModels/MessageInfoViewModel.cs @@ -20,5 +20,7 @@ namespace AbstractShopContracts.ViewModels [DisplayName("Текст")] public string Body { get; set; } = string.Empty; + + public int Id => throw new NotImplementedException(); } } \ No newline at end of file diff --git a/AbstractShop/AbstractShopDataModels/Models/IMessageInfoModel.cs b/AbstractShop/AbstractShopDataModels/Models/IMessageInfoModel.cs index e51db32..d8a1dc9 100644 --- a/AbstractShop/AbstractShopDataModels/Models/IMessageInfoModel.cs +++ b/AbstractShop/AbstractShopDataModels/Models/IMessageInfoModel.cs @@ -1,6 +1,6 @@ namespace AbstractShopDataModels.Models { - public interface IMessageInfoModel + public interface IMessageInfoModel : IId { string MessageId { get; } diff --git a/AbstractShop/AbstractShopDatabaseImplement/Implements/BackUpInfo.cs b/AbstractShop/AbstractShopDatabaseImplement/Implements/BackUpInfo.cs new file mode 100644 index 0000000..afca497 --- /dev/null +++ b/AbstractShop/AbstractShopDatabaseImplement/Implements/BackUpInfo.cs @@ -0,0 +1,27 @@ +using AbstractShopContracts.StoragesContracts; + +namespace AbstractShopDatabaseImplement.Implements +{ + public class BackUpInfo : IBackUpInfo + { + public List? GetList() where T : class, new() + { + using var context = new AbstractShopDatabase(); + 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; + } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopListImplement/AbstractShopListImplement.csproj b/AbstractShop/AbstractShopListImplement/AbstractShopListImplement.csproj index 81fc427..ba40e9e 100644 --- a/AbstractShop/AbstractShopListImplement/AbstractShopListImplement.csproj +++ b/AbstractShop/AbstractShopListImplement/AbstractShopListImplement.csproj @@ -11,4 +11,8 @@ + + + + diff --git a/AbstractShop/AbstractShopListImplement/Implements/BackUpInfo.cs b/AbstractShop/AbstractShopListImplement/Implements/BackUpInfo.cs new file mode 100644 index 0000000..bf948d2 --- /dev/null +++ b/AbstractShop/AbstractShopListImplement/Implements/BackUpInfo.cs @@ -0,0 +1,17 @@ +using AbstractShopContracts.StoragesContracts; + +namespace AbstractShopListImplement.Implements +{ + public class BackUpInfo : IBackUpInfo + { + public List? GetList() where T : class, new() + { + throw new NotImplementedException(); + } + + public Type? GetTypeByModelInterface(string modelInterfaceName) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopListImplement/ListImplementationExtension.cs b/AbstractShop/AbstractShopListImplement/ListImplementationExtension.cs new file mode 100644 index 0000000..0dde1e9 --- /dev/null +++ b/AbstractShop/AbstractShopListImplement/ListImplementationExtension.cs @@ -0,0 +1,22 @@ +using AbstractShopContracts.DI; +using AbstractShopContracts.StoragesContracts; +using AbstractShopListImplement.Implements; + +namespace AbstractShopListImplement +{ + 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(); + } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopView/DataGridViewExtension.cs b/AbstractShop/AbstractShopView/DataGridViewExtension.cs new file mode 100644 index 0000000..98ec60e --- /dev/null +++ b/AbstractShop/AbstractShopView/DataGridViewExtension.cs @@ -0,0 +1,46 @@ +using AbstractShopContracts.Attributes; + +namespace AbstractShopView +{ + 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; + } + } + } + } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopView/FormClients.cs b/AbstractShop/AbstractShopView/FormClients.cs index 72e0c97..438a6ef 100644 --- a/AbstractShop/AbstractShopView/FormClients.cs +++ b/AbstractShop/AbstractShopView/FormClients.cs @@ -26,13 +26,7 @@ namespace AbstractShopView { 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/AbstractShop/AbstractShopView/FormComponents.cs b/AbstractShop/AbstractShopView/FormComponents.cs index 883aa37..0788486 100644 --- a/AbstractShop/AbstractShopView/FormComponents.cs +++ b/AbstractShop/AbstractShopView/FormComponents.cs @@ -1,5 +1,6 @@ using AbstractShopContracts.BindingModels; using AbstractShopContracts.BusinessLogicsContracts; +using AbstractShopContracts.DI; using Microsoft.Extensions.Logging; namespace AbstractShopView @@ -44,13 +45,10 @@ namespace AbstractShopView 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(); } } @@ -58,7 +56,7 @@ namespace AbstractShopView { 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/AbstractShop/AbstractShopView/Program.cs b/AbstractShop/AbstractShopView/Program.cs index b662e57..347d51b 100644 --- a/AbstractShop/AbstractShopView/Program.cs +++ b/AbstractShop/AbstractShopView/Program.cs @@ -4,9 +4,7 @@ using AbstractShopBusinessLogic.OfficePackage; using AbstractShopBusinessLogic.OfficePackage.Implements; using AbstractShopContracts.BindingModels; using AbstractShopContracts.BusinessLogicsContracts; -using AbstractShopContracts.StoragesContracts; -using AbstractShopDatabaseImplement.Implements; -using Microsoft.Extensions.DependencyInjection; +using AbstractShopContracts.DI; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; @@ -14,9 +12,6 @@ namespace AbstractShopView { internal static class Program { - private static ServiceProvider? _serviceProvider; - public static ServiceProvider? ServiceProvider => _serviceProvider; - /// /// The main entry point for the application. /// @@ -26,13 +21,11 @@ namespace AbstractShopView // 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, @@ -48,53 +41,49 @@ namespace AbstractShopView } catch (Exception ex) { - var logger = _serviceProvider.GetService(); + var logger = DependencyManager.Instance.Resolve(); logger?.LogError(ex, " "); } // Application.Run(new Form1()); } - private static void ConfigureServices(ServiceCollection services) + private static void InitDependency() { - services.AddLogging(option => + DependencyManager.InitDependency(); + + DependencyManager.Instance.AddLogging(option => { option.SetMinimumLevel(LogLevel.Information); option.AddNLog("nlog.config"); }); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); - services.AddTransient(); - services.AddSingleton(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(true); + DependencyManager.Instance.RegisterType(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - - services.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(); } - 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 diff --git a/AbstractShop/ImplementationExtensions/AbstractShopContracts.dll b/AbstractShop/ImplementationExtensions/AbstractShopContracts.dll new file mode 100644 index 0000000..9c010c2 Binary files /dev/null and b/AbstractShop/ImplementationExtensions/AbstractShopContracts.dll differ diff --git a/AbstractShop/ImplementationExtensions/AbstractShopDataModels.dll b/AbstractShop/ImplementationExtensions/AbstractShopDataModels.dll new file mode 100644 index 0000000..795ff95 Binary files /dev/null and b/AbstractShop/ImplementationExtensions/AbstractShopDataModels.dll differ diff --git a/AbstractShop/ImplementationExtensions/AbstractShopListImplement.dll b/AbstractShop/ImplementationExtensions/AbstractShopListImplement.dll new file mode 100644 index 0000000..9382f70 Binary files /dev/null and b/AbstractShop/ImplementationExtensions/AbstractShopListImplement.dll differ