diff --git a/anbs_cp/Classes/ActionState.cs b/anbs_cp/Classes/ActionState.cs index 124baea..eb83ce1 100644 --- a/anbs_cp/Classes/ActionState.cs +++ b/anbs_cp/Classes/ActionState.cs @@ -35,9 +35,7 @@ public class ActionState: ISerializable // ReSharper disable once MemberCanBeProtected.Global public ActionState () { - Info = []; - Warnings = []; - Errors = []; + Messages = []; Value = default; } @@ -47,26 +45,14 @@ public class ActionState: ISerializable /// Начальное значение public ActionState (T defaultValue) { - Info = []; - Warnings = []; - Errors = []; + Messages = []; Value = defaultValue; } /// /// Список информации /// - public List Info { get; } - - /// - /// Список предупреждений - /// - public List Warnings { get; } - - /// - /// Список ошибок - /// - public List Errors { get; } + public List Messages { get; } /// /// Значение @@ -114,21 +100,23 @@ public class ActionState: ISerializable } #endregion - #region Добавление ошибки + #region Добавление /// - /// Добавление ошибки + /// Добавление сообщения /// - /// Ошибка + /// Сообщение // ReSharper disable once MemberCanBeMadeStatic.Global // ReSharper disable once FunctionRecursiveOnAllPaths - public void AddError (ActionStateMessage error) => Errors.Add(error); + public void Add (ActionStateMessage message) => Messages.Add(message); /// - /// Добавляет ошибки в список + /// Добавляет список /// - /// Список ошибок - public void AddErrors (IEnumerable errors) => Errors.AddRange(errors); + /// Список сообщений + public void AddRange (IEnumerable messages) => Messages.AddRange(messages); + #endregion + #region Добавление ошибки /// /// Добавление ошибки /// @@ -136,10 +124,13 @@ public class ActionState: ISerializable public void AddError (bool critical = true) { //Создаю ошибку - ActionStateMessage error = new("", critical); + ActionStateMessage error = new("", critical) + { + MessageType = EActionStateMessageType.Error + }; //Добавляю ошибку - AddError(error); + Add(error); } /// @@ -150,10 +141,13 @@ public class ActionState: ISerializable public void AddError (string message, bool critical = true) { //Создаю ошибку - ActionStateMessage error = new(message, critical); + ActionStateMessage error = new(message, critical) + { + MessageType = EActionStateMessageType.Error + }; //Добавляю ошибку - AddError(error); + Add(error); } /// @@ -165,10 +159,13 @@ public class ActionState: ISerializable public void AddError (string errorObject, string message, bool critical = true) { //Создаю ошибку - ActionStateMessage error = new(errorObject, message, critical); + ActionStateMessage error = new(errorObject, message, critical) + { + MessageType = EActionStateMessageType.Error + }; //Добавляю ошибку - AddError(error); + Add(error); } #endregion @@ -389,6 +386,9 @@ public class ActionState: ISerializable /// public void Deserialize (string json) { + // Удаляю лишние символы + json = json.Trim("\"".ToCharArray()); + // Десериализую строку ActionStateSerializable itemSerializable = new NewtonsoftJsonSerializer().Deserialize(json) ?? new(); diff --git a/anbs_cp/Classes/ActionStateMessage.cs b/anbs_cp/Classes/ActionStateMessage.cs index 4cbbe55..61c6ec0 100644 --- a/anbs_cp/Classes/ActionStateMessage.cs +++ b/anbs_cp/Classes/ActionStateMessage.cs @@ -1,5 +1,7 @@ // ReSharper disable MemberCanBeInternal // ReSharper disable MemberCanBePrivate.Global + +using anbs_cp.Enums; using anbs_cp.Interfaces; namespace anbs_cp.Classes; @@ -9,84 +11,141 @@ namespace anbs_cp.Classes; /// public sealed class ActionStateMessage: ISerializable { - #region Свойства - /// - /// Критичность сообщения - /// - public bool IsCritical { get; set; } + #region Свойства + /// + /// Тип сообщения + /// + public EActionStateMessageType MessageType { get; set; } = EActionStateMessageType.Information; - /// - /// Объект сообщения - /// - public object Object { get; set; } + /// + /// Критичность сообщения + /// + public bool IsCritical { get; set; } - /// - /// Текст сообщения - /// - public string Message { get; set; } - #endregion + /// + /// Объект сообщения + /// + public string Object { get; set; } - #region Конструкторы - /// - /// Конструктор по умолчанию - /// - // ReSharper disable once MemberCanBePrivate.Global - public ActionStateMessage () + /// + /// Текст сообщения + /// + public string Message { get; set; } + + #endregion + + #region Конструкторы + + /// + /// Конструктор по умолчанию + /// + // ReSharper disable once MemberCanBePrivate.Global + public ActionStateMessage () + { + IsCritical = true; + Object = "Not Implemented"; + Message = "Not Implemented"; + } + + /// + /// Конструктор с 2 параметрами + /// + /// Сообщение + /// Критичность сообщения + public ActionStateMessage (string message, bool isCritical = true) + { + IsCritical = isCritical; + Object = ""; + Message = message; + } + + /// + /// Конструктор с 3 параметрами + /// + /// Объект сообщения + /// Сообщение + /// Критичность сообщения + public ActionStateMessage (string eObject, string message, bool isCritical = true) + { + IsCritical = isCritical; + Object = eObject; + Message = message; + } + + #endregion + + #region Методы + + /// + /// Вывод сообщения + /// + /// Строка-форматирование (например, «[{0}] - {1}») + /// Отформатированную строка + public string PrintMessage (string format) => string.Format(format, Object, Message); + + /// + /// Устанавливает объект + /// + /// Объект + /// Тип объекта + public void SetObject (T obj) + { + // Если объект реализует интерфейс ISerializable + if (obj is ISerializable serializable) { - IsCritical = true; - Object = "Not Implemented"; - Message = "Not Implemented"; + // - то сериализуем его методами интерфейса + Object = serializable.Serialize(); + // - и прерываем + return; } - /// - /// Конструктор с 2 параметрами - /// - /// Сообщение - /// Критичность сообщения - public ActionStateMessage (string message, bool isCritical = true) - { - IsCritical = isCritical; - Object = ""; - Message = message; - } + // Сериализуем объект с помощью NewtonsoftJson + Object = new NewtonsoftJsonSerializer().Serialize(obj); + } - /// - /// Конструктор с 3 параметрами - /// - /// Объект сообщения - /// Сообщение - /// Критичность сообщения - public ActionStateMessage (object eObject, string message, bool isCritical = true) - { - IsCritical = isCritical; - Object = eObject; - Message = message; - } - #endregion + /// + /// Получает объект + /// + /// Класс объекта + /// Объект или null + public T? GetObject () => new NewtonsoftJsonSerializer().Deserialize(Object); - #region Методы - /// - /// Вывод сообщения - /// - /// Строка-форматирование (например, «[{0}] - {1}») - /// Отформатированную строка - public string PrintMessage (string format) => string.Format(format, Object, Message); - #endregion + /// + /// Получает объект + /// + /// Класс объекта, реализующий интерфейс ISerializable + /// Объект + public T GetSerializedObject () where T : ISerializable, new() + { + // Создаём результирующую модель + T model = new(); - #region Реализация интерфейса ISerializable - /// - public string Serialize () => new NewtonsoftJsonSerializer().Serialize(this); + // Десериализуем её методами интерфейса ISerializable + model.Deserialize(Object); - /// - public void Deserialize (string json) - { - // Десериализую строку - ActionStateMessage item = new NewtonsoftJsonSerializer().Deserialize(json) ?? new(); + // Возвращаем модель + return model; + } - // Передаю параметры - IsCritical = item.IsCritical; - Object = item.Object; - Message = item.Message; - } - #endregion + #endregion + + #region Реализация интерфейса ISerializable + + /// + public string Serialize () => new NewtonsoftJsonSerializer().Serialize(this); + + /// + public void Deserialize (string json) + { + // Десериализую строку + ActionStateMessage item = new NewtonsoftJsonSerializer().Deserialize(json) ?? new(); + + // Передаю параметры + MessageType = item.MessageType; + IsCritical = item.IsCritical; + Object = item.Object; + Message = item.Message; + } + + #endregion } \ No newline at end of file diff --git a/anbs_cp/Enums/EActionStateMessageType.cs b/anbs_cp/Enums/EActionStateMessageType.cs new file mode 100644 index 0000000..7aafdad --- /dev/null +++ b/anbs_cp/Enums/EActionStateMessageType.cs @@ -0,0 +1,21 @@ +namespace anbs_cp.Enums; + +/// +/// Тип сообщения о состоянии +/// +public enum EActionStateMessageType +{ /// + /// Информация + /// + Information = 0, + + /// + /// Предупреждение + /// + Warning = 1, + + /// + /// Ошибка + /// + Error = 2 +} \ No newline at end of file diff --git a/anbs_cp/anbs_cp.csproj b/anbs_cp/anbs_cp.csproj index 8a8c5c2..1474a2e 100644 --- a/anbs_cp/anbs_cp.csproj +++ b/anbs_cp/anbs_cp.csproj @@ -2,7 +2,7 @@ net8.0 - 2024.3.26.2 + 2024.3.27.2 Александр Бабаев Набор компонентов ANB Software Библиотека полезных методов языка C# @@ -38,7 +38,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/anbs_cpdb/anbs_cpdb.csproj b/anbs_cpdb/anbs_cpdb.csproj index 5784956..af25478 100644 --- a/anbs_cpdb/anbs_cpdb.csproj +++ b/anbs_cpdb/anbs_cpdb.csproj @@ -8,7 +8,7 @@ anbs_cp.Database True ANBSoftware.ComponentsPack.Database - 2024.3.25 + 2024.3.27 Александр Бабаев Набор компонентов ANB Software для работы с БД Библиотека полезных методов языка C# для работы с базами данных @@ -21,7 +21,7 @@ - + diff --git a/anbs_cpfn/anbs_cpfn.csproj b/anbs_cpfn/anbs_cpfn.csproj index c2ad934..2a9ad3b 100644 --- a/anbs_cpfn/anbs_cpfn.csproj +++ b/anbs_cpfn/anbs_cpfn.csproj @@ -6,7 +6,7 @@ enable True ANBSoftware.ComponentsPackForNet - 2024.3.26 + 2024.3.27 Александр Бабаев Набор компонентов ANB Software для ASP.NET Core Библиотека полезных методов языка C# для ASP.NET Core @@ -21,11 +21,11 @@ - + - + diff --git a/anbsoftware.componentspack.sln.DotSettings b/anbsoftware.componentspack.sln.DotSettings index a83721d..c30ce8d 100644 --- a/anbsoftware.componentspack.sln.DotSettings +++ b/anbsoftware.componentspack.sln.DotSettings @@ -18,6 +18,7 @@ True True True + True True True True @@ -43,6 +44,7 @@ True True True + True True True True