From 81ba4173fbdb23851e93d7d6d76d75673c12d703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=AF=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=B2?= Date: Sun, 19 May 2024 15:47:43 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=BE=D0=B2=D1=8B=D0=B9=20=D0=BC=D0=B5?= =?UTF-8?q?=D1=85=D0=B0=D0=BD=D0=B8=D0=B7=D0=BC=20=D0=BD=D0=B0=D1=81=D1=82?= =?UTF-8?q?=D1=80=D0=BE=D0=B9=D0=BA=D0=B8=20=D0=B7=D0=B0=D0=B2=D0=B8=D1=81?= =?UTF-8?q?=D0=B8=D0=BC=D0=BE=D1=81=D1=82=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CarRepairShopContracts.csproj | 6 ++ .../DI/DependencyManager.cs | 34 +++++++ .../DI/IDependencyContainer.cs | 13 +++ .../DI/IImplementationExtension.cs | 14 +++ .../DI/ServiceProviderLoader.cs | 51 +++++++++++ .../DI/UnityDependencyContainer.cs | 55 ++++++++++++ .../CarRepairShopDatabaseImplement.csproj | 4 + .../DatabaseImplementationExtension.cs | 27 ++++++ .../CarRepairShopFileImplement.csproj | 4 + .../FileImplementationExtension.cs | 27 ++++++ .../CarRepairShopListImplement.csproj | 4 + .../Implements/BackUpInfo.cs | 22 +++++ .../ListImplementationExtension.cs | 27 ++++++ .../CarRepairShopView/FormComponents.cs | 23 +++-- .../CarRepairShopView/FormImplementers.cs | 21 ++--- CarRepairShop/CarRepairShopView/FormMain.cs | 66 +++++--------- CarRepairShop/CarRepairShopView/FormRepair.cs | 56 ++++++------ .../CarRepairShopView/FormRepairs.cs | 21 ++--- CarRepairShop/CarRepairShopView/Program.cs | 88 ++++++++----------- 19 files changed, 402 insertions(+), 161 deletions(-) create mode 100644 CarRepairShop/CarRepairShopContracts/DI/DependencyManager.cs create mode 100644 CarRepairShop/CarRepairShopContracts/DI/IDependencyContainer.cs create mode 100644 CarRepairShop/CarRepairShopContracts/DI/IImplementationExtension.cs create mode 100644 CarRepairShop/CarRepairShopContracts/DI/ServiceProviderLoader.cs create mode 100644 CarRepairShop/CarRepairShopContracts/DI/UnityDependencyContainer.cs create mode 100644 CarRepairShop/CarRepairShopDatabaseImplement/DatabaseImplementationExtension.cs create mode 100644 CarRepairShop/CarRepairShopFileImplement/FileImplementationExtension.cs create mode 100644 CarRepairShop/CarRepairShopListImplement/Implements/BackUpInfo.cs create mode 100644 CarRepairShop/CarRepairShopListImplement/ListImplementationExtension.cs diff --git a/CarRepairShop/CarRepairShopContracts/CarRepairShopContracts.csproj b/CarRepairShop/CarRepairShopContracts/CarRepairShopContracts.csproj index 215400f..38d69ae 100644 --- a/CarRepairShop/CarRepairShopContracts/CarRepairShopContracts.csproj +++ b/CarRepairShop/CarRepairShopContracts/CarRepairShopContracts.csproj @@ -6,6 +6,12 @@ enable + + + + + + diff --git a/CarRepairShop/CarRepairShopContracts/DI/DependencyManager.cs b/CarRepairShop/CarRepairShopContracts/DI/DependencyManager.cs new file mode 100644 index 0000000..1b3ccbe --- /dev/null +++ b/CarRepairShop/CarRepairShopContracts/DI/DependencyManager.cs @@ -0,0 +1,34 @@ +using Microsoft.Extensions.Logging; + +namespace CarRepairShopContracts.DI +{ + public class DependencyManager + { + private readonly IDependencyContainer _dependencyManager; + private static DependencyManager? _manager; + private static readonly object _lockObject = new(); + private DependencyManager() + { + _dependencyManager = new UnityDependencyContainer(); + } + public static DependencyManager Instance { get { if (_manager == null) { lock (_lockObject) { _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/CarRepairShop/CarRepairShopContracts/DI/IDependencyContainer.cs b/CarRepairShop/CarRepairShopContracts/DI/IDependencyContainer.cs new file mode 100644 index 0000000..84c55c5 --- /dev/null +++ b/CarRepairShop/CarRepairShopContracts/DI/IDependencyContainer.cs @@ -0,0 +1,13 @@ +using Microsoft.Extensions.Logging; + + +namespace CarRepairShopContracts.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/CarRepairShop/CarRepairShopContracts/DI/IImplementationExtension.cs b/CarRepairShop/CarRepairShopContracts/DI/IImplementationExtension.cs new file mode 100644 index 0000000..2444219 --- /dev/null +++ b/CarRepairShop/CarRepairShopContracts/DI/IImplementationExtension.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarRepairShopContracts.DI +{ + public interface IImplementationExtension + { + public int Priority { get; } + public void RegisterServices(); + } +} diff --git a/CarRepairShop/CarRepairShopContracts/DI/ServiceProviderLoader.cs b/CarRepairShop/CarRepairShopContracts/DI/ServiceProviderLoader.cs new file mode 100644 index 0000000..b35247c --- /dev/null +++ b/CarRepairShop/CarRepairShopContracts/DI/ServiceProviderLoader.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace CarRepairShopContracts.DI +{ + public static partial class ServiceProviderLoader + { + public static IImplementationExtension? GetImplementationExtensions() + { + IImplementationExtension? source = null; + var files = Directory.GetFiles(TryGetImplementationExtensionsFolder(), "*.dll", SearchOption.AllDirectories); + foreach (var file in files.Distinct()) + { + Assembly asm = Assembly.LoadFrom(file); + foreach(var t in asm.GetExportedTypes()) + { + if(t.IsClass && typeof(IImplementationExtension).IsAssignableFrom(t)) + { + if(source == null) + { + source = (IImplementationExtension)Activator.CreateInstance(t)!; + } + else + { + var newSource = (IImplementationExtension)Activator.CreateInstance(t)!; + if(newSource.Priority > source.Priority) + { + source = newSource; + } + } + } + } + } + return source; + } + + private static string TryGetImplementationExtensionsFolder() + { + var directory = new DirectoryInfo(Directory.GetCurrentDirectory()); + while (directory != null && !directory.GetDirectories("ImplementationExtensions", SearchOption.AllDirectories).Any(x => x.Name == "ImplementationExtensions")) + { + directory = directory.Parent; + } + return $"{directory?.FullName}\\ImplementationExtensions"; + } + } +} diff --git a/CarRepairShop/CarRepairShopContracts/DI/UnityDependencyContainer.cs b/CarRepairShop/CarRepairShopContracts/DI/UnityDependencyContainer.cs new file mode 100644 index 0000000..369e64b --- /dev/null +++ b/CarRepairShop/CarRepairShopContracts/DI/UnityDependencyContainer.cs @@ -0,0 +1,55 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Unity; +using Unity.Microsoft.Logging; + +namespace CarRepairShopContracts.DI +{ + public class UnityDependencyContainer : IDependencyContainer + { + private readonly IUnityContainer _container; + public UnityDependencyContainer() + { + _container = new UnityContainer(); + } + + public void AddLogging(Action configure) + { + var logger = LoggerFactory.Create(configure); + _container.AddExtension(new LoggingExtension(logger)); + } + + public void RegisterType(bool isSingle) where U : class, T where T : class + { + if (isSingle) + { + _container.RegisterSingleton(); + } + else + { + _container.RegisterType(); + } + } + + public void RegisterType(bool isSingle) where T : class + { + if (isSingle) + { + _container.RegisterSingleton(); + } + else + { + _container.RegisterType(); + } + } + + public T Resolve() + { + return _container.Resolve(); + } + } +} diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/CarRepairShopDatabaseImplement.csproj b/CarRepairShop/CarRepairShopDatabaseImplement/CarRepairShopDatabaseImplement.csproj index 5e3533c..0c914fd 100644 --- a/CarRepairShop/CarRepairShopDatabaseImplement/CarRepairShopDatabaseImplement.csproj +++ b/CarRepairShop/CarRepairShopDatabaseImplement/CarRepairShopDatabaseImplement.csproj @@ -21,4 +21,8 @@ + + + + diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/DatabaseImplementationExtension.cs b/CarRepairShop/CarRepairShopDatabaseImplement/DatabaseImplementationExtension.cs new file mode 100644 index 0000000..ff6b4ce --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/DatabaseImplementationExtension.cs @@ -0,0 +1,27 @@ +using CarRepairShopContracts.DI; +using CarRepairShopContracts.StoragesContracts; +using CarRepairShopDatabaseImplement.Implements; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarRepairShopDatabaseImplement +{ + public class DatabaseImplementationExtension : IImplementationExtension + { + public int Priority => 2; + + public void RegisterServices() + { + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + } + } +} diff --git a/CarRepairShop/CarRepairShopFileImplement/CarRepairShopFileImplement.csproj b/CarRepairShop/CarRepairShopFileImplement/CarRepairShopFileImplement.csproj index b1210f3..2daf12b 100644 --- a/CarRepairShop/CarRepairShopFileImplement/CarRepairShopFileImplement.csproj +++ b/CarRepairShop/CarRepairShopFileImplement/CarRepairShopFileImplement.csproj @@ -13,4 +13,8 @@ + + + + diff --git a/CarRepairShop/CarRepairShopFileImplement/FileImplementationExtension.cs b/CarRepairShop/CarRepairShopFileImplement/FileImplementationExtension.cs new file mode 100644 index 0000000..b444b6c --- /dev/null +++ b/CarRepairShop/CarRepairShopFileImplement/FileImplementationExtension.cs @@ -0,0 +1,27 @@ +using CarRepairShopContracts.DI; +using CarRepairShopContracts.StoragesContracts; +using CarRepairShopFileImplement.Implements; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarRepairShopFileImplement +{ + public class FileImplementationExtension : IImplementationExtension + { + public int Priority => 1; + + public void RegisterServices() + { + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + } + } +} diff --git a/CarRepairShop/CarRepairShopListImplement/CarRepairShopListImplement.csproj b/CarRepairShop/CarRepairShopListImplement/CarRepairShopListImplement.csproj index e49be3f..60d2cac 100644 --- a/CarRepairShop/CarRepairShopListImplement/CarRepairShopListImplement.csproj +++ b/CarRepairShop/CarRepairShopListImplement/CarRepairShopListImplement.csproj @@ -11,4 +11,8 @@ + + + + diff --git a/CarRepairShop/CarRepairShopListImplement/Implements/BackUpInfo.cs b/CarRepairShop/CarRepairShopListImplement/Implements/BackUpInfo.cs new file mode 100644 index 0000000..cdfa280 --- /dev/null +++ b/CarRepairShop/CarRepairShopListImplement/Implements/BackUpInfo.cs @@ -0,0 +1,22 @@ +using CarRepairShopContracts.StoragesContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarRepairShopListImplement.Implements +{ + internal class BackUpInfo : IBackUpInfo + { + public List? GetList() where T : class, new() + { + throw new NotImplementedException(); + } + + public Type? GetTypeByModelInterface(string modelInterfaceName) + { + throw new NotImplementedException(); + } + } +} diff --git a/CarRepairShop/CarRepairShopListImplement/ListImplementationExtension.cs b/CarRepairShop/CarRepairShopListImplement/ListImplementationExtension.cs new file mode 100644 index 0000000..b7e2738 --- /dev/null +++ b/CarRepairShop/CarRepairShopListImplement/ListImplementationExtension.cs @@ -0,0 +1,27 @@ +using CarRepairShopContracts.DI; +using CarRepairShopContracts.StoragesContracts; +using CarRepairShopListImplement.Implements; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarRepairShopListImplement +{ + public class ListImplementationExtension : IImplementationExtension + { + public int Priority => 0; + + public void RegisterServices() + { + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + } + } +} diff --git a/CarRepairShop/CarRepairShopView/FormComponents.cs b/CarRepairShop/CarRepairShopView/FormComponents.cs index 1f99507..632da26 100644 --- a/CarRepairShop/CarRepairShopView/FormComponents.cs +++ b/CarRepairShop/CarRepairShopView/FormComponents.cs @@ -1,5 +1,6 @@ using CarRepairShopContracts.BindingModels; using CarRepairShopContracts.BusinessLogicsContracts; +using CarRepairShopContracts.DI; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -44,28 +45,24 @@ namespace CarRepairShopView 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/CarRepairShop/CarRepairShopView/FormImplementers.cs b/CarRepairShop/CarRepairShopView/FormImplementers.cs index 1e50c79..4e83fe8 100644 --- a/CarRepairShop/CarRepairShopView/FormImplementers.cs +++ b/CarRepairShop/CarRepairShopView/FormImplementers.cs @@ -1,4 +1,5 @@ using CarRepairShopContracts.BusinessLogicsContracts; +using CarRepairShopContracts.DI; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -44,13 +45,10 @@ namespace CarRepairShopView private void buttonCreate_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(); } } @@ -58,14 +56,11 @@ namespace CarRepairShopView { if (dataGridView.SelectedRows.Count == 1) { - var service = Program.ServiceProvider?.GetService(typeof(FormImplementer)); - if (service is FormImplementer form) + var form = DependencyManager.Instance.Resolve(); + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) { - form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - if (form.ShowDialog() == DialogResult.OK) - { - LoadData(); - } + LoadData(); } } } diff --git a/CarRepairShop/CarRepairShopView/FormMain.cs b/CarRepairShop/CarRepairShopView/FormMain.cs index 0372400..e2f0d3f 100644 --- a/CarRepairShop/CarRepairShopView/FormMain.cs +++ b/CarRepairShop/CarRepairShopView/FormMain.cs @@ -1,5 +1,6 @@ using CarRepairShopContracts.BindingModels; using CarRepairShopContracts.BusinessLogicsContracts; +using CarRepairShopContracts.DI; using CarRepairShopDataModels.Enums; using Microsoft.Extensions.Logging; using System; @@ -44,30 +45,26 @@ namespace CarRepairShopView 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(FormRepairs)); - if (service is FormRepairs 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) @@ -168,54 +165,39 @@ namespace CarRepairShopView private void ComponentRepairsToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormReportRepairComponents)); - if (service is FormReportRepairComponents form) - { - form.ShowDialog(); - } + var form = DependencyManager.Instance.Resolve(); + form.ShowDialog(); } private void OrdersToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormReportOrders)); - if (service is FormReportOrders form) - { - form.ShowDialog(); - } + var form = DependencyManager.Instance.Resolve(); + form.ShowDialog(); } private void 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 workStartToolStripMenuItem_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 implementersToolStripMenuItem_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(FormLetters)); - if (service is FormLetters form) - { - form.ShowDialog(); - } + var form = DependencyManager.Instance.Resolve(); + form.ShowDialog(); } private void CreateBackUpToolStripMenuItem_Click(object sender, EventArgs e) diff --git a/CarRepairShop/CarRepairShopView/FormRepair.cs b/CarRepairShop/CarRepairShopView/FormRepair.cs index a9f57fa..39d5c2d 100644 --- a/CarRepairShop/CarRepairShopView/FormRepair.cs +++ b/CarRepairShop/CarRepairShopView/FormRepair.cs @@ -1,5 +1,6 @@ using CarRepairShopContracts.BindingModels; using CarRepairShopContracts.BusinessLogicsContracts; +using CarRepairShopContracts.DI; using CarRepairShopContracts.SearchModels; using CarRepairShopDataModels.Models; using Microsoft.Extensions.Logging; @@ -79,26 +80,23 @@ namespace CarRepairShopView private void buttonAdd_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormRepairComponent)); - if (service is FormRepairComponent form) + var form = DependencyManager.Instance.Resolve(); + if (form.ShowDialog() == DialogResult.OK) { - if (form.ShowDialog() == DialogResult.OK) + if (form.ComponentModel == null) { - if (form.ComponentModel == null) - { - return; - } - _logger.LogInformation("Добавление нового компонента: {componentName} - {Count}", form.ComponentModel.ComponentName, form.Count); - if (_repairComponents.ContainsKey(form.Id)) - { - _repairComponents[form.Id] = (form.ComponentModel, form.Count); - } - else - { - _repairComponents.Add(form.Id, (form.ComponentModel, form.Count)); - } - LoadData(); + return; } + _logger.LogInformation("Добавление нового компонента: {componentName} - {Count}", form.ComponentModel.ComponentName, form.Count); + if (_repairComponents.ContainsKey(form.Id)) + { + _repairComponents[form.Id] = (form.ComponentModel, form.Count); + } + else + { + _repairComponents.Add(form.Id, (form.ComponentModel, form.Count)); + } + LoadData(); } } @@ -106,22 +104,20 @@ namespace CarRepairShopView { if (dataGridView.SelectedRows.Count == 1) { - var service = Program.ServiceProvider?.GetService(typeof(FormRepairComponent)); - if (service is FormRepairComponent form) + var form = DependencyManager.Instance.Resolve(); + + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value); + form.Id = id; + form.Count = _repairComponents[id].Item2; + if (form.ShowDialog() == DialogResult.OK) { - int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value); - form.Id = id; - form.Count = _repairComponents[id].Item2; - if (form.ShowDialog() == DialogResult.OK) + if (form.ComponentModel == null) { - if (form.ComponentModel == null) - { - return; - } - _logger.LogInformation("Изменение компонента: {ComponentName} - {Count}", form.ComponentModel.ComponentName, form.Count); - _repairComponents[form.Id] = (form.ComponentModel, form.Count); - LoadData(); + return; } + _logger.LogInformation("Изменение компонента: {ComponentName} - {Count}", form.ComponentModel.ComponentName, form.Count); + _repairComponents[form.Id] = (form.ComponentModel, form.Count); + LoadData(); } } } diff --git a/CarRepairShop/CarRepairShopView/FormRepairs.cs b/CarRepairShop/CarRepairShopView/FormRepairs.cs index 24282e0..f96e572 100644 --- a/CarRepairShop/CarRepairShopView/FormRepairs.cs +++ b/CarRepairShop/CarRepairShopView/FormRepairs.cs @@ -1,5 +1,6 @@ using CarRepairShopContracts.BindingModels; using CarRepairShopContracts.BusinessLogicsContracts; +using CarRepairShopContracts.DI; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -45,13 +46,10 @@ namespace CarRepairShopView private void buttonAdd_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormRepair)); - if (service is FormRepair form) + var form = DependencyManager.Instance.Resolve(); + if (form.ShowDialog() == DialogResult.OK) { - if (form.ShowDialog() == DialogResult.OK) - { - LoadData(); - } + LoadData(); } } @@ -59,14 +57,11 @@ namespace CarRepairShopView { if (dataGridView.SelectedRows.Count == 1) { - var service = Program.ServiceProvider?.GetService(typeof(FormRepair)); - if (service is FormRepair 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/CarRepairShop/CarRepairShopView/Program.cs b/CarRepairShop/CarRepairShopView/Program.cs index 0f5b4f7..4e6a1c1 100644 --- a/CarRepairShop/CarRepairShopView/Program.cs +++ b/CarRepairShop/CarRepairShopView/Program.cs @@ -4,6 +4,7 @@ using CarRepairShopBusinessLogic.OfficePackage; using CarRepairShopBusinessLogic.OfficePackage.Implements; using CarRepairShopContracts.BindingModels; using CarRepairShopContracts.BusinessLogicsContracts; +using CarRepairShopContracts.DI; using CarRepairShopContracts.StoragesContracts; using CarRepairShopDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; @@ -15,9 +16,6 @@ namespace CarRepairShopView { internal static class Program { - private static ServiceProvider? _serviceProvider; - public static ServiceProvider? ServiceProvider => _serviceProvider; - /// /// The main entry point for the application. /// @@ -28,12 +26,10 @@ namespace CarRepairShopView // 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, @@ -47,58 +43,50 @@ namespace CarRepairShopView } catch (Exception ex) { - var logger = _serviceProvider.GetService(); + var logger = DependencyManager.Instance.Resolve(); logger?.LogError(ex, " "); } - Application.Run(_serviceProvider.GetRequiredService()); + Application.Run(DependencyManager.Instance.Resolve()); } - - private static void ConfigureServices(ServiceCollection services) + private static void InitDependency() { - services.AddLogging(option => + DependencyManager.InitDependency(); + DependencyManager.Instance.AddLogging(option => { - option.SetMinimumLevel(LogLevel.Information); - option.AddNLog("nlog.config"); + option.SetMinimumLevel(LogLevel.Information); + option.AddNLog("nlog.config"); }); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - 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(true); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); - services.AddTransient(); - services.AddSingleton(); - - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - 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