diff --git a/.gitignore b/.gitignore index 9422752..06e5834 100644 --- a/.gitignore +++ b/.gitignore @@ -398,6 +398,7 @@ FodyWeavers.xsd # JetBrains Rider *.sln.iml +/Typography/ImplementationExtensions # ---> Java # Compiled class file *.class diff --git a/Typography/TypographyBusinessLogic/BusinessLogics/BackUpLogic.cs b/Typography/TypographyBusinessLogic/BusinessLogics/BackUpLogic.cs new file mode 100644 index 0000000..acf9ed4 --- /dev/null +++ b/Typography/TypographyBusinessLogic/BusinessLogics/BackUpLogic.cs @@ -0,0 +1,94 @@ +using TypographyContracts.BindingModels; +using TypographyContracts.BusinessLogicsContracts; +using TypographyContracts.StoragesContracts; +using TypographyDataModels; +using Microsoft.Extensions.Logging; +using System.IO.Compression; +using System.Reflection; +using System.Runtime.Serialization.Json; + +namespace TypographyBusinessLogic.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/Typography/TypographyContracts/Attributes/ColumnAttribute.cs b/Typography/TypographyContracts/Attributes/ColumnAttribute.cs new file mode 100644 index 0000000..d3dee9e --- /dev/null +++ b/Typography/TypographyContracts/Attributes/ColumnAttribute.cs @@ -0,0 +1,25 @@ +namespace TypographyContracts.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/Typography/TypographyContracts/Attributes/GridViewAutoSize.cs b/Typography/TypographyContracts/Attributes/GridViewAutoSize.cs new file mode 100644 index 0000000..f82c727 --- /dev/null +++ b/Typography/TypographyContracts/Attributes/GridViewAutoSize.cs @@ -0,0 +1,14 @@ +namespace TypographyContracts.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/Typography/TypographyContracts/BindingModels/BackUpSaveBinidngModel.cs b/Typography/TypographyContracts/BindingModels/BackUpSaveBinidngModel.cs new file mode 100644 index 0000000..5e6b703 --- /dev/null +++ b/Typography/TypographyContracts/BindingModels/BackUpSaveBinidngModel.cs @@ -0,0 +1,7 @@ +namespace TypographyContracts.BindingModels +{ + public class BackUpSaveBinidngModel + { + public string FolderName { get; set; } = string.Empty; + } +} \ No newline at end of file diff --git a/Typography/TypographyContracts/BusinessLogicsContracts/IBackUpLogic.cs b/Typography/TypographyContracts/BusinessLogicsContracts/IBackUpLogic.cs new file mode 100644 index 0000000..231da6b --- /dev/null +++ b/Typography/TypographyContracts/BusinessLogicsContracts/IBackUpLogic.cs @@ -0,0 +1,9 @@ +using TypographyContracts.BindingModels; + +namespace TypographyContracts.BusinessLogicsContracts +{ + public interface IBackUpLogic + { + void CreateBackUp(BackUpSaveBinidngModel model); + } +} \ No newline at end of file diff --git a/Typography/TypographyContracts/DI/DependencyManager.cs b/Typography/TypographyContracts/DI/DependencyManager.cs new file mode 100644 index 0000000..eaa8f9f --- /dev/null +++ b/Typography/TypographyContracts/DI/DependencyManager.cs @@ -0,0 +1,51 @@ +using Microsoft.Extensions.Logging; + +namespace TypographyContracts.DI +{ + public class DependencyManager + { + private readonly IDependencyContainer _dependencyManager; + + private static DependencyManager? _manager; + + private static readonly object _locjObject = new(); + + private DependencyManager() + { + _dependencyManager = new UnityDependencyContainer(); + } + + 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/Typography/TypographyContracts/DI/IDependencyContainer.cs b/Typography/TypographyContracts/DI/IDependencyContainer.cs new file mode 100644 index 0000000..2045c88 --- /dev/null +++ b/Typography/TypographyContracts/DI/IDependencyContainer.cs @@ -0,0 +1,15 @@ +using Microsoft.Extensions.Logging; + +namespace TypographyContracts.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/Typography/TypographyContracts/DI/IImplementationExtension.cs b/Typography/TypographyContracts/DI/IImplementationExtension.cs new file mode 100644 index 0000000..9e26239 --- /dev/null +++ b/Typography/TypographyContracts/DI/IImplementationExtension.cs @@ -0,0 +1,9 @@ +namespace TypographyContracts.DI +{ + public interface IImplementationExtension + { + public int Priority { get; } + + public void RegisterServices(); + } +} \ No newline at end of file diff --git a/Typography/TypographyContracts/DI/ServiceDependencyContainer.cs b/Typography/TypographyContracts/DI/ServiceDependencyContainer.cs new file mode 100644 index 0000000..e2ff45a --- /dev/null +++ b/Typography/TypographyContracts/DI/ServiceDependencyContainer.cs @@ -0,0 +1,57 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace TypographyContracts.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/Typography/TypographyContracts/DI/ServiceProviderLoader.cs b/Typography/TypographyContracts/DI/ServiceProviderLoader.cs new file mode 100644 index 0000000..d758a5f --- /dev/null +++ b/Typography/TypographyContracts/DI/ServiceProviderLoader.cs @@ -0,0 +1,46 @@ +using System.Reflection; + +namespace TypographyContracts.DI +{ + public class ServiceProviderLoader + { + public static IImplementationExtension? GetImplementationExtensions() + { + IImplementationExtension? source = null; + var files = Directory.GetFiles(TryGetImplementationExtensionsFolder(), "*.dll", SearchOption.AllDirectories); + foreach (var file in files.Distinct()) + { + Assembly asm = Assembly.LoadFrom(file); + foreach (var t in asm.GetExportedTypes()) + { + if (t.IsClass && typeof(IImplementationExtension).IsAssignableFrom(t)) + { + if (source == null) + { + source = (IImplementationExtension)Activator.CreateInstance(t)!; + } + else + { + var newSource = (IImplementationExtension)Activator.CreateInstance(t)!; + if (newSource.Priority > source.Priority) + { + source = newSource; + } + } + } + } + } + return source; + } + + private static string TryGetImplementationExtensionsFolder() + { + var directory = new DirectoryInfo(Directory.GetCurrentDirectory()); + while (directory != null && !directory.GetDirectories("ImplementationExtensions", SearchOption.AllDirectories).Any(x => x.Name == "ImplementationExtensions")) + { + directory = directory.Parent; + } + return $"{directory?.FullName}\\ImplementationExtensions"; + } + } +} \ No newline at end of file diff --git a/Typography/TypographyContracts/DI/UnityDependencyContainer.cs b/Typography/TypographyContracts/DI/UnityDependencyContainer.cs new file mode 100644 index 0000000..a43085d --- /dev/null +++ b/Typography/TypographyContracts/DI/UnityDependencyContainer.cs @@ -0,0 +1,50 @@ +using Microsoft.Extensions.Logging; +using Unity; +using Unity.Microsoft.Logging; + +namespace TypographyContracts.DI +{ + public class UnityDependencyContainer : IDependencyContainer + { + private readonly UnityContainer _unityContainer; + + public UnityDependencyContainer() + { + _unityContainer = new UnityContainer(); + } + + public void AddLogging(Action configure) + { + _unityContainer.AddExtension(new LoggingExtension(LoggerFactory.Create(configure))); + } + + public void RegisterType(bool isSingle) where U : class, T where T : class + { + if (isSingle) + { + _unityContainer.RegisterSingleton(); + } + else + { + _unityContainer.RegisterType(); + } + } + + public void RegisterType(bool isSingle) where T : class + { + if (isSingle) + { + _unityContainer.RegisterSingleton(); + } + else + { + _unityContainer.RegisterType(); + } + } + + public T Resolve() + { + return _unityContainer.Resolve(); + } + } +} \ No newline at end of file diff --git a/Typography/TypographyContracts/Properties/launchSettings.json b/Typography/TypographyContracts/Properties/launchSettings.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/Typography/TypographyContracts/Properties/launchSettings.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/Typography/TypographyContracts/StoragesContracts/IBackUpInfo.cs b/Typography/TypographyContracts/StoragesContracts/IBackUpInfo.cs new file mode 100644 index 0000000..4b29eb0 --- /dev/null +++ b/Typography/TypographyContracts/StoragesContracts/IBackUpInfo.cs @@ -0,0 +1,9 @@ +namespace TypographyContracts.StoragesContracts +{ + public interface IBackUpInfo + { + List? GetList() where T : class, new(); + + Type? GetTypeByModelInterface(string modelInterfaceName); + } +} \ No newline at end of file diff --git a/Typography/TypographyContracts/TypographyContracts.csproj b/Typography/TypographyContracts/TypographyContracts.csproj index 30c2576..70e95ff 100644 --- a/Typography/TypographyContracts/TypographyContracts.csproj +++ b/Typography/TypographyContracts/TypographyContracts.csproj @@ -6,6 +6,12 @@ enable + + + + + + diff --git a/Typography/TypographyDatabaseImplement/DatabaseImplementationExtension.cs b/Typography/TypographyDatabaseImplement/DatabaseImplementationExtension.cs new file mode 100644 index 0000000..aad27dd --- /dev/null +++ b/Typography/TypographyDatabaseImplement/DatabaseImplementationExtension.cs @@ -0,0 +1,22 @@ +using TypographyContracts.DI; +using TypographyContracts.StoragesContracts; +using TypographyDatabaseImplement.Implements; + +namespace TypographyDatabaseImplement +{ + 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(); + } + } +} \ No newline at end of file diff --git a/Typography/TypographyDatabaseImplement/Implements/BackUpInfo.cs b/Typography/TypographyDatabaseImplement/Implements/BackUpInfo.cs new file mode 100644 index 0000000..04c8afe --- /dev/null +++ b/Typography/TypographyDatabaseImplement/Implements/BackUpInfo.cs @@ -0,0 +1,27 @@ +using TypographyContracts.StoragesContracts; + +namespace TypographyDatabaseImplement.Implements +{ + public class BackUpInfo : IBackUpInfo + { + public List? GetList() where T : class, new() + { + using var context = new TypographyDatabase(); + 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/Typography/TypographyDatabaseImplement/TypographyDatabaseImplement.csproj b/Typography/TypographyDatabaseImplement/TypographyDatabaseImplement.csproj index 1ce9597..f3ebf98 100644 --- a/Typography/TypographyDatabaseImplement/TypographyDatabaseImplement.csproj +++ b/Typography/TypographyDatabaseImplement/TypographyDatabaseImplement.csproj @@ -20,4 +20,8 @@ + + + + diff --git a/Typography/TypographyFileImplement/FileImplementationExtension.cs b/Typography/TypographyFileImplement/FileImplementationExtension.cs new file mode 100644 index 0000000..5847dcb --- /dev/null +++ b/Typography/TypographyFileImplement/FileImplementationExtension.cs @@ -0,0 +1,22 @@ +using TypographyContracts.DI; +using TypographyContracts.StoragesContracts; +using TypographyFileImplement.Implements; + +namespace TypographyFileImplement +{ + 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(); + } + } +} \ No newline at end of file diff --git a/Typography/TypographyFileImplement/Implements/BackUpInfo.cs b/Typography/TypographyFileImplement/Implements/BackUpInfo.cs new file mode 100644 index 0000000..aa8e209 --- /dev/null +++ b/Typography/TypographyFileImplement/Implements/BackUpInfo.cs @@ -0,0 +1,38 @@ +using System.Reflection; +using TypographyContracts.StoragesContracts; + +namespace TypographyFileImplement.Implements +{ + public class BackUpInfo : IBackUpInfo + { + private readonly DataFileSingleton source; + private readonly PropertyInfo[] sourceProps; + + public BackUpInfo() + { + source = DataFileSingleton.GetInstance(); + sourceProps = source.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); + } + + public List? GetList() where T : class, new() + { + return (List?)sourceProps + .FirstOrDefault(x => x.PropertyType.IsGenericType && x.PropertyType.GetGenericArguments()[0] == typeof(T))? + .GetValue(source); + } + + public Type? GetTypeByModelInterface(string modelInterfaceName) + { + var assembly = typeof(BackUpInfo).Assembly; + var types = assembly.GetTypes(); + foreach (var type in types) + { + if (type.IsClass && type.GetInterface(modelInterfaceName) != null) + { + return type; + } + } + return null; + } + } +} \ No newline at end of file diff --git a/Typography/TypographyFileImplement/TypographyFileImplement.csproj b/Typography/TypographyFileImplement/TypographyFileImplement.csproj index 96adc37..06faf2d 100644 --- a/Typography/TypographyFileImplement/TypographyFileImplement.csproj +++ b/Typography/TypographyFileImplement/TypographyFileImplement.csproj @@ -14,4 +14,8 @@ + + + + diff --git a/Typography/TypographyListImplement/Implements/BackUpInfo.cs b/Typography/TypographyListImplement/Implements/BackUpInfo.cs new file mode 100644 index 0000000..f8b4d9f --- /dev/null +++ b/Typography/TypographyListImplement/Implements/BackUpInfo.cs @@ -0,0 +1,17 @@ +using TypographyContracts.StoragesContracts; + +namespace TypographyListImplement.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/Typography/TypographyListImplement/ListImplementationExtension.cs b/Typography/TypographyListImplement/ListImplementationExtension.cs new file mode 100644 index 0000000..4bda5aa --- /dev/null +++ b/Typography/TypographyListImplement/ListImplementationExtension.cs @@ -0,0 +1,22 @@ +using TypographyContracts.DI; +using TypographyContracts.StoragesContracts; +using TypographyListImplement.Implements; + +namespace TypographyListImplement +{ + 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/Typography/TypographyListImplement/Models/MessageInfo .cs b/Typography/TypographyListImplement/Models/MessageInfo .cs deleted file mode 100644 index af0d76c..0000000 --- a/Typography/TypographyListImplement/Models/MessageInfo .cs +++ /dev/null @@ -1,53 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TypographyContracts.BindingModels; -using TypographyContracts.ViewModels; -using TypographyDataModels.Models; - -namespace TypographyListImplement.Models -{ - public class MessageInfo : IMessageInfoModel - { - public string MessageId { get; private set; } = string.Empty; - - public int? ClientId { get; private set; } - - public string SenderName { get; private set; } = string.Empty; - - public DateTime DateDelivery { get; private set; } - - public string Subject { get; private set; } = string.Empty; - - public string Body { get; private set; } = string.Empty; - - public static MessageInfo? Create(MessageInfoBindingModel? model) - { - if (model == null) - { - return null; - } - return new MessageInfo() - { - MessageId = model.MessageId, - ClientId = model.ClientId, - SenderName = model.SenderName, - DateDelivery = model.DateDelivery, - Subject = model.Subject, - Body = model.Body - }; - } - - public MessageInfoViewModel GetViewModel => new() - { - MessageId = MessageId, - ClientId = ClientId, - SenderName = SenderName, - DateDelivery = DateDelivery, - Subject = Subject, - Body = Body - }; - } -} diff --git a/Typography/TypographyListImplement/Models/MessageInfo.cs b/Typography/TypographyListImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..46b57e3 --- /dev/null +++ b/Typography/TypographyListImplement/Models/MessageInfo.cs @@ -0,0 +1,50 @@ +using TypographyDataModels.Models; +using TypographyContracts.BindingModels; +using TypographyContracts.ViewModels; + +namespace TypographyListImplement.Models +{ + public class MessageInfo : IMessageInfoModel + { + public string MessageId { get; private set; } = string.Empty; + + public int? ClientId { get; private set; } + + public string SenderName { get; private set; } = string.Empty; + + public DateTime DateDelivery { get; private set; } + + public string Subject { get; private set; } = string.Empty; + + public string Body { get; private set; } = string.Empty; + + public int Id { get; private set; } + + public static MessageInfo? Create(MessageInfoBindingModel? model) + { + if (model == null) + { + return null; + } + return new MessageInfo() + { + MessageId = model.MessageId, + ClientId = model.ClientId, + SenderName = model.SenderName, + DateDelivery = model.DateDelivery, + Subject = model.Subject, + Body = model.Body + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + MessageId = MessageId, + ClientId = ClientId, + SenderName = SenderName, + DateDelivery = DateDelivery, + Subject = Subject, + Body = Body + }; + } +} \ No newline at end of file diff --git a/Typography/TypographyListImplement/TypographyListImplement.csproj b/Typography/TypographyListImplement/TypographyListImplement.csproj index db73c6b..e6874e0 100644 --- a/Typography/TypographyListImplement/TypographyListImplement.csproj +++ b/Typography/TypographyListImplement/TypographyListImplement.csproj @@ -10,4 +10,8 @@ + + + +