From 8f6439b7d8f3b5469eba71657780d67750b79624 Mon Sep 17 00:00:00 2001 From: babaev-an Date: Tue, 26 Mar 2024 18:50:21 +0300 Subject: [PATCH] 20240326 --- anbs_cp/Classes/ActionState.cs | 93 +++++++++++- anbs_cp/Classes/ActionStateSerializable.cs | 27 ++++ .../Classes/ActionStateSerializableValue.cs | 90 ++++++++++++ anbs_cp/Interfaces/IActionState.cs | 4 +- demo/ActionStateSerialize.Designer.cs | 92 ++++++++++++ demo/ActionStateSerialize.cs | 31 ++++ demo/ActionStateSerialize.resx | 120 +++++++++++++++ demo/DemoClass.cs | 8 + demo/MainMenu.Designer.cs | 138 ++++++++++-------- demo/MainMenu.cs | 6 + demo/MainMenu.resx | 62 +++++++- 11 files changed, 601 insertions(+), 70 deletions(-) create mode 100644 anbs_cp/Classes/ActionStateSerializable.cs create mode 100644 anbs_cp/Classes/ActionStateSerializableValue.cs create mode 100644 demo/ActionStateSerialize.Designer.cs create mode 100644 demo/ActionStateSerialize.cs create mode 100644 demo/ActionStateSerialize.resx create mode 100644 demo/DemoClass.cs diff --git a/anbs_cp/Classes/ActionState.cs b/anbs_cp/Classes/ActionState.cs index c3e93c6..f0f7471 100644 --- a/anbs_cp/Classes/ActionState.cs +++ b/anbs_cp/Classes/ActionState.cs @@ -1,5 +1,6 @@ // ReSharper disable MemberCanBePrivate.Global // ReSharper disable UnusedAutoPropertyAccessor.Global + using anbs_cp.Enums; using anbs_cp.Interfaces; @@ -23,10 +24,7 @@ namespace anbs_cp.Classes; * Заменены интерфейсы IAction* на соответствующие классы */ -/// -/// Класс, хранящий в себе состояния действия -/// -/// Тип значения - результата действия +/// public class ActionState: IActionState { /// @@ -350,4 +348,91 @@ public class ActionState: IActionState } #endregion #endregion + + #region Реализация интерфейса ISerializable + /// + public string Serialize () + { + // Создаю модель + ActionStateSerializable serializableModel = new(); + + // Для каждого информационного сообщения + foreach (IActionStateMessage info in Info) + // - добавляю в модель + serializableModel.Info.Add(info.Serialize()); + + // Для каждого предупреждения + foreach (IActionStateMessage warning in Warnings) + // - добавляю в модель + serializableModel.Info.Add(warning.Serialize()); + + // Для каждой ошибки + foreach (IActionStateMessage error in Errors) + // - добавляю в модель + serializableModel.Info.Add(error.Serialize()); + + // Создаю модель значения + ActionStateSerializableValue value = new(); + + // Получаю данные + value.GetValue(Value); + + // Добавляю в модель + serializableModel.Value = value.Serialize(); + + // Возвращаю сериализованную модель + return new SysTextSerializer().Serialize(serializableModel); + } + + /// + public void Deserialize (string json) + { + // Десериализую строку + ActionStateSerializable itemSerializable = new SysTextSerializer().Deserialize(json) ?? new(); + + // Создаю модель значения + ActionStateSerializableValue value = new(); + + // Очищаю списки + Info.Clear(); + Warnings.Clear(); + Errors.Clear(); + + // Для каждого информационного сообщения + foreach (string infoString in itemSerializable.Info) + { + // - создаю сообщение + ActionStateMessage info = new(); + // - десериализую в него данные из модели + info.Deserialize(infoString); + // - добавляю в список + Info.Add(info); + } + foreach (string warningString in itemSerializable.Warnings) + { + // - создаю сообщение + ActionStateMessage warning = new(); + // - десериализую в него данные из модели + warning.Deserialize(warningString); + // - добавляю в список + Info.Add(warning); + } + foreach (string errorString in itemSerializable.Errors) + { + // - создаю сообщение + ActionStateMessage error = new(); + // - десериализую в него данные из модели + error.Deserialize(errorString); + // - добавляю в список + Info.Add(error); + } + + // Десериализую данные значения из модели + value.Deserialize(itemSerializable.Value); + + // Получаю значение + Value = value.SetValue(); + } + #endregion + } \ No newline at end of file diff --git a/anbs_cp/Classes/ActionStateSerializable.cs b/anbs_cp/Classes/ActionStateSerializable.cs new file mode 100644 index 0000000..cc351bd --- /dev/null +++ b/anbs_cp/Classes/ActionStateSerializable.cs @@ -0,0 +1,27 @@ +namespace anbs_cp.Classes; + +/// +/// Сериализованная модель класса, хранящего в себе состояния действия +/// +public class ActionStateSerializable +{ + /// + /// Список информации + /// + public List Info { get; } = []; + + /// + /// Список предупреждений + /// + public List Warnings { get; } = []; + + /// + /// Список ошибок + /// + public List Errors { get; } = []; + + /// + /// Значение + /// + public string Value { get; set; } = string.Empty; +} \ No newline at end of file diff --git a/anbs_cp/Classes/ActionStateSerializableValue.cs b/anbs_cp/Classes/ActionStateSerializableValue.cs new file mode 100644 index 0000000..a738ea0 --- /dev/null +++ b/anbs_cp/Classes/ActionStateSerializableValue.cs @@ -0,0 +1,90 @@ +using anbs_cp.Interfaces; + +namespace anbs_cp.Classes; + +/// +/// Сериализованная модель значения класса, хранящего в себе состояния действия +/// +public class ActionStateSerializableValue: ISerializable +{ + /// + /// Значение - null? + /// + private bool IsNull { get; set; } + + /// + /// Сериализуемое значение + /// + private string Serialized { get; set; } = string.Empty; + + /// + /// Получает значение из + /// + /// Значение + /// Тип значения + public void GetValue (T? value) + { + // Значение - null? + IsNull = value == null; + + // Если не null + if (value != null) + { + // - значение поддерживает интерфейс IsSerialized + if (value is ISerializable serializableValue) + // -- сериализую + Serialized = serializableValue.Serialize(); + else + // -- сериализую + Serialized = new SysTextSerializer().Serialize(value); + } + else + // Сериализую + Serialized = new SysTextSerializer().Serialize(value); + } + + /// + /// Устанавливает значение + /// + /// Класс + /// Значение + public T? SetValue () + { + // Если null + if (IsNull) + // - то возыращаем null + return default; + + // Если тип T не реализует интерфейс ISerializable + if (typeof(T).GetInterface(nameof(ISerializable)) == null) + // - то воспользуемся простой десериализацией + return new SysTextSerializer().Deserialize(Serialized); + + // Создаю модель + T? model = default; + + // Десериализую модель + (model as ISerializable)?.Deserialize(Serialized); + + // Возвращаю модель + return model!; + + // Если null + } + + #region Реализация интерфейса ISerializable + /// + public string Serialize () => new SysTextSerializer().Serialize(this); + + /// + public void Deserialize (string json) + { + // Десериализую строку + ActionStateSerializableValue item = new SysTextSerializer().Deserialize(json) ?? new(); + + // Передаю параметры + IsNull = item.IsNull; + Serialized = item.Serialized; + } + #endregion +} \ No newline at end of file diff --git a/anbs_cp/Interfaces/IActionState.cs b/anbs_cp/Interfaces/IActionState.cs index cdbbc0a..2f77a36 100644 --- a/anbs_cp/Interfaces/IActionState.cs +++ b/anbs_cp/Interfaces/IActionState.cs @@ -3,9 +3,9 @@ namespace anbs_cp.Interfaces; /// -/// Интерфейс класса, хранящего в себе состояния действия +/// Класс, хранящий в себе состояния действия /// -public interface IActionState +public interface IActionState: ISerializable { /// /// Список информации diff --git a/demo/ActionStateSerialize.Designer.cs b/demo/ActionStateSerialize.Designer.cs new file mode 100644 index 0000000..c805b67 --- /dev/null +++ b/demo/ActionStateSerialize.Designer.cs @@ -0,0 +1,92 @@ +namespace demo; + +partial class ActionStateSerialize +{ + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose (bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent () + { + textBox1 = new TextBox(); + textBox2 = new TextBox(); + label1 = new Label(); + button1 = new Button(); + SuspendLayout(); + // + // textBox1 + // + textBox1.Location = new Point(24, 12); + textBox1.Name = "textBox1"; + textBox1.Size = new Size(209, 23); + textBox1.TabIndex = 0; + // + // textBox2 + // + textBox2.Location = new Point(312, 6); + textBox2.Multiline = true; + textBox2.Name = "textBox2"; + textBox2.Size = new Size(463, 225); + textBox2.TabIndex = 1; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(24, 65); + label1.Name = "label1"; + label1.Size = new Size(38, 15); + label1.TabIndex = 2; + label1.Text = "label1"; + // + // button1 + // + button1.Location = new Point(24, 111); + button1.Name = "button1"; + button1.Size = new Size(75, 23); + button1.TabIndex = 3; + button1.Text = "button1"; + button1.UseVisualStyleBackColor = true; + button1.Click += button1_Click; + // + // ActionStateSerialize + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 250); + Controls.Add(button1); + Controls.Add(label1); + Controls.Add(textBox2); + Controls.Add(textBox1); + Name = "ActionStateSerialize"; + Text = "Тест сериализации ActionState"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private TextBox textBox1; + private TextBox textBox2; + private Label label1; + private Button button1; +} \ No newline at end of file diff --git a/demo/ActionStateSerialize.cs b/demo/ActionStateSerialize.cs new file mode 100644 index 0000000..d87687b --- /dev/null +++ b/demo/ActionStateSerialize.cs @@ -0,0 +1,31 @@ +using anbs_cp.Classes; + +namespace demo; + +public partial class ActionStateSerialize: Form +{ + public ActionStateSerialize () + { + InitializeComponent(); + } + + private void button1_Click (object sender, EventArgs e) + { + DemoClass demoClass = new() + { + Name = textBox1.Text.Length > 0 ? textBox1.Text : string.Empty + }; + + ActionState state = new(); + + state.AddError("ОШИБКА, ШЕФ!"); + + textBox2.Text = state.Serialize(); + + ActionState state2 = new(); + + state2.Deserialize(textBox2.Text); + + label1.Text = state2.Value?.Name ?? "НЕТ ЗНАЧЕНИЯ"; + } +} diff --git a/demo/ActionStateSerialize.resx b/demo/ActionStateSerialize.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/demo/ActionStateSerialize.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/demo/DemoClass.cs b/demo/DemoClass.cs new file mode 100644 index 0000000..cd646e8 --- /dev/null +++ b/demo/DemoClass.cs @@ -0,0 +1,8 @@ +namespace demo; + +public class DemoClass +{ + public int Num { get; set; } = 5; + public string Name { get; set; } = ""; + public bool NotB { get; set; } = true; +} \ No newline at end of file diff --git a/demo/MainMenu.Designer.cs b/demo/MainMenu.Designer.cs index a13600a..23417f8 100644 --- a/demo/MainMenu.Designer.cs +++ b/demo/MainMenu.Designer.cs @@ -28,69 +28,80 @@ sealed partial class MainMenu /// private void InitializeComponent () { - this.CountValueTest = new System.Windows.Forms.Button(); - this.SimpleMapperTest = new System.Windows.Forms.Button(); - this.FileExtensionTest = new System.Windows.Forms.Button(); - this.OsInfoBtn = new System.Windows.Forms.Button(); - this.SuspendLayout(); - // - // CountValueTest - // - this.CountValueTest.Location = new System.Drawing.Point(12, 12); - this.CountValueTest.Name = "CountValueTest"; - this.CountValueTest.Size = new System.Drawing.Size(337, 53); - this.CountValueTest.TabIndex = 0; - this.CountValueTest.Text = "Новый тест модуля форматирования строки"; - this.CountValueTest.UseVisualStyleBackColor = true; - this.CountValueTest.Click += new System.EventHandler(this.button1_Click); - // - // SimpleMapperTest - // - this.SimpleMapperTest.Location = new System.Drawing.Point(12, 71); - this.SimpleMapperTest.Name = "SimpleMapperTest"; - this.SimpleMapperTest.Size = new System.Drawing.Size(335, 51); - this.SimpleMapperTest.TabIndex = 1; - this.SimpleMapperTest.Text = "Новый тест модуля SimpleMapper"; - this.SimpleMapperTest.UseVisualStyleBackColor = true; - this.SimpleMapperTest.Click += new System.EventHandler(this.SimpleMapperTest_Click); - // - // FileExtensionTest - // - this.FileExtensionTest.Location = new System.Drawing.Point(12, 128); - this.FileExtensionTest.Name = "FileExtensionTest"; - this.FileExtensionTest.Size = new System.Drawing.Size(335, 51); - this.FileExtensionTest.TabIndex = 2; - this.FileExtensionTest.Text = "Новый тест модуля FileExtension"; - this.FileExtensionTest.UseVisualStyleBackColor = true; - this.FileExtensionTest.Click += new System.EventHandler(this.FileExtensionTest_Click); - // - // OsInfoBtn - // - this.OsInfoBtn.Location = new System.Drawing.Point(12, 185); - this.OsInfoBtn.Name = "OsInfoBtn"; - this.OsInfoBtn.Size = new System.Drawing.Size(335, 51); - this.OsInfoBtn.TabIndex = 3; - this.OsInfoBtn.Text = "Информация о системе"; - this.OsInfoBtn.UseVisualStyleBackColor = true; - this.OsInfoBtn.Click += new System.EventHandler(this.OsInfoBtn_Click); - // - // MainMenu - // - this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 21F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(361, 252); - this.Controls.Add(this.OsInfoBtn); - this.Controls.Add(this.FileExtensionTest); - this.Controls.Add(this.SimpleMapperTest); - this.Controls.Add(this.CountValueTest); - this.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; - this.Margin = new System.Windows.Forms.Padding(4); - this.Name = "MainMenu"; - this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; - this.Text = "Главное меню"; - this.ResumeLayout(false); - + CountValueTest = new Button(); + SimpleMapperTest = new Button(); + FileExtensionTest = new Button(); + OsInfoBtn = new Button(); + actionStateSerializerTestBtn = new Button(); + SuspendLayout(); + // + // CountValueTest + // + CountValueTest.Location = new Point(12, 12); + CountValueTest.Name = "CountValueTest"; + CountValueTest.Size = new Size(337, 53); + CountValueTest.TabIndex = 0; + CountValueTest.Text = "Новый тест модуля форматирования строки"; + CountValueTest.UseVisualStyleBackColor = true; + CountValueTest.Click += button1_Click; + // + // SimpleMapperTest + // + SimpleMapperTest.Location = new Point(12, 71); + SimpleMapperTest.Name = "SimpleMapperTest"; + SimpleMapperTest.Size = new Size(335, 51); + SimpleMapperTest.TabIndex = 1; + SimpleMapperTest.Text = "Новый тест модуля SimpleMapper"; + SimpleMapperTest.UseVisualStyleBackColor = true; + SimpleMapperTest.Click += SimpleMapperTest_Click; + // + // FileExtensionTest + // + FileExtensionTest.Location = new Point(12, 128); + FileExtensionTest.Name = "FileExtensionTest"; + FileExtensionTest.Size = new Size(335, 51); + FileExtensionTest.TabIndex = 2; + FileExtensionTest.Text = "Новый тест модуля FileExtension"; + FileExtensionTest.UseVisualStyleBackColor = true; + FileExtensionTest.Click += FileExtensionTest_Click; + // + // OsInfoBtn + // + OsInfoBtn.Location = new Point(12, 185); + OsInfoBtn.Name = "OsInfoBtn"; + OsInfoBtn.Size = new Size(335, 51); + OsInfoBtn.TabIndex = 3; + OsInfoBtn.Text = "Информация о системе"; + OsInfoBtn.UseVisualStyleBackColor = true; + OsInfoBtn.Click += OsInfoBtn_Click; + // + // actionStateSerializerTestBtn + // + actionStateSerializerTestBtn.Location = new Point(12, 254); + actionStateSerializerTestBtn.Name = "actionStateSerializerTestBtn"; + actionStateSerializerTestBtn.Size = new Size(335, 51); + actionStateSerializerTestBtn.TabIndex = 4; + actionStateSerializerTestBtn.Text = "Тест сериализации ActionState"; + actionStateSerializerTestBtn.UseVisualStyleBackColor = true; + actionStateSerializerTestBtn.Click += actionStateSerializerTestBtn_Click; + // + // MainMenu + // + AutoScaleDimensions = new SizeF(10F, 21F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(361, 327); + Controls.Add(actionStateSerializerTestBtn); + Controls.Add(OsInfoBtn); + Controls.Add(FileExtensionTest); + Controls.Add(SimpleMapperTest); + Controls.Add(CountValueTest); + Font = new Font("Times New Roman", 14.25F); + FormBorderStyle = FormBorderStyle.FixedSingle; + Margin = new Padding(4); + Name = "MainMenu"; + StartPosition = FormStartPosition.CenterScreen; + Text = "Главное меню"; + ResumeLayout(false); } #endregion @@ -99,4 +110,5 @@ sealed partial class MainMenu private Button SimpleMapperTest; private Button FileExtensionTest; private Button OsInfoBtn; + private Button actionStateSerializerTestBtn; } diff --git a/demo/MainMenu.cs b/demo/MainMenu.cs index e4acabc..79fd69a 100644 --- a/demo/MainMenu.cs +++ b/demo/MainMenu.cs @@ -29,4 +29,10 @@ public sealed partial class MainMenu: Form OsInfoFrm formTest = new(); formTest.ShowDialog(); } + + private void actionStateSerializerTestBtn_Click (object sender, EventArgs e) + { + ActionStateSerialize form = new(); + form.ShowDialog(); + } } diff --git a/demo/MainMenu.resx b/demo/MainMenu.resx index f298a7b..af32865 100644 --- a/demo/MainMenu.resx +++ b/demo/MainMenu.resx @@ -1,4 +1,64 @@ - + + +