using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Serialization.Formatters.Binary; using System.Xml.Serialization; namespace Database { public class Program { static void Main(string[] args) { string line = ""; using (StreamReader sr = new StreamReader("option.ini.txt", System.Text.Encoding.Default)) { line = sr.ReadLine(); } List<Person> employees = new List<Person>(); XmlSerializer xs = new XmlSerializer(typeof(List<Person>)); BinaryFormatter bf = new BinaryFormatter(); if (line == "xml") { if (File.Exists("person.xml")) { using (FileStream fs = new FileStream("person.xml", FileMode.OpenOrCreate)) { List<Person> newEmployees = (List<Person>)xs.Deserialize(fs); employees = newEmployees; } } } if (line == "bin") { if (File.Exists("person.dat")) { using (FileStream fs = new FileStream("person.dat", FileMode.OpenOrCreate)) { List<Person> newEmployees = (List<Person>)bf.Deserialize(fs); employees = newEmployees; } } } if (line == "bin" || line == "xml") { Console.WriteLine("УстановитС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ bin ΠΈΠ»ΠΈ xml Π² Ρ„Π°ΠΉΠ»Π΅ option.ini.txt"); } while (true) { Console.Clear(); Console.WriteLine("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΊΠΎΠΌΠ°Π½Π΄Ρƒ:"); string com = Console.ReadLine(); if (com.ToLower() == "exit") { if (line == "xml") { using (FileStream fs = new FileStream("person.xml", FileMode.Create)) { xs.Serialize(fs, employees); } return; } else { using (FileStream fs = new FileStream("person.dat", FileMode.Create)) { bf.Serialize(fs, employees); } return; } } if (com.ToLower() == "allsee") { if (line == "xml") { var res = employees.OrderBy(u => u.Name).ThenBy(u => u.Surname); foreach (Person u in res) { Console.WriteLine("Имя: {0} Ѐамилия: {1} Π’Π°Π±Π΅Π»ΡŒΠ½Ρ‹ΠΉ Π½ΠΎΠΌΠ΅Ρ€: {2}", u.Name, u.Surname, u.PersonnelNumber); } Console.ReadLine(); continue; } if (line == "bin") { var res = employees.OrderBy(u => u.Name).ThenBy(u => u.Surname); foreach (Person u in res) { Console.WriteLine("Имя: {0} Ѐамилия: {1} Π’Π°Π±Π΅Π»ΡŒΠ½Ρ‹ΠΉ Π½ΠΎΠΌΠ΅Ρ€: {2}", u.Name, u.Surname, u.PersonnelNumber); } Console.ReadLine(); continue; } } if (com.ToLower() == "del") { string t; Console.WriteLine("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ Ρ‚Π°Π±Π΅Π»ΡŒΠ½Ρ‹ΠΉ Π½ΠΎΠΌΠ΅Ρ€ сотрудника для удалСния ΠΈΠ· Π±Π°Π·Ρ‹:"); t = Console.ReadLine(); if (employees.Exists(p => p.PersonnelNumber == t)) { for (int i = 0; i < employees.Count; i++) { if (employees[i].PersonnelNumber == t) { employees.Remove(employees[i]); } } Console.WriteLine("Π£Π΄Π°Π»Π΅Π½ΠΈΠ΅ ΠΏΡ€ΠΎΡˆΠ»ΠΎ ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ."); } else { Console.WriteLine("Π‘ΠΎΡ‚Ρ€ΡƒΠ΄Π½ΠΈΠΊΠ° с Ρ‚Π°ΠΊΠΈΠΌ Ρ‚Π°Π±Π΅Π»ΡŒΠ½Ρ‹ΠΌ Π½ΠΎΠΌΠ΅Ρ€ΠΎΠΌ Π½Π΅ сущСствуСт!"); } Console.ReadKey(); continue; } if (com.ToLower() == "add") { string name; string surname; string personnelnumber; Console.WriteLine("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ имя сотрудника:"); name = Console.ReadLine(); Console.WriteLine("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ Ρ„Π°ΠΌΠΈΠ»ΠΈΡŽ сотрудника:"); surname = Console.ReadLine(); Console.WriteLine("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ Ρ‚Π°Π±Π΅Π»ΡŒΠ½Ρ‹ΠΉ Π½ΠΎΠΌΠ΅Ρ€ сотрудника:"); personnelnumber = Console.ReadLine(); Person p = new Person(name, surname, personnelnumber); employees.Add(p); continue; } if (com.ToLower() != "add") { Console.WriteLine("ДоступныС ΠΊΠΎΠΌΠ°Π½Π΄Ρ‹:"); Console.WriteLine("\tadd ΡΠΎΠ·Π΄Π°Ρ‚ΡŒ запись сотрудника"); Console.WriteLine("\tdel Π£Π΄Π°Π»ΠΈΡ‚ΡŒ запись сотрудника"); Console.WriteLine("\tallsee ΠŸΡ€ΠΎΡΠΌΠΎΡ‚Ρ€Π΅Ρ‚ΡŒ список сотрудников"); Console.WriteLine("\texit Π’Ρ‹ΠΉΡ‚ΠΈ ΠΈ ΡΠΎΡ…Ρ€Π°Π½ΠΈΡ‚ΡŒ Π΄Π°Π½Π½Ρ‹Π΅"); Console.ReadKey(); continue; } } } } [Serializable] public class Person { public string Name { get; set; } public string Surname { get; set; } public string PersonnelNumber { get; set; } public Person() { } public Person(string name, string surname, string personnelnumber) { Name = name; Surname = surname; PersonnelNumber = personnelnumber; } } } 

How in this code to take out the deserialization and serialization separately into classes and manage them from this menu?

    1 answer 1

    I can offer several steps to implement the breakdown of the code into components.

    1. The MenuItem class describes one menu item, contains the name of the menu item and an Action type delegate for binding a specific action to the menu item. For the console menu, full brute force events, but this already depends on the specific task and preferences.
    2. Menu class - contains a list of MenuItem and is responsible for user interaction, printing menu items in a given order, and calling Action s bound to menu items.
    3. Include in a separate class or your Person class methods of the form void SomeAction() to implement the necessary actions on the entity.

    If you want to save the menu code in the Main code, then you can limit the removal of the serialization code and deserialization into separate methods that are placed in the current class, or in a separate class, or in the Person class and replace the code in the methods with a call to the created methods. This option is implemented more quickly, but less flexible in further work.

    Of course, these are possible, but not the only options.

    If you are too keen on OOP and design patterns, then from a simple program of 50-100 lines of code, it is quite possible to blow up the monster into a couple of dozen classes and a couple of thousand lines of code, but whether you need all of this in your case or not. If this is a fragment of a large system, perhaps it is necessary, if an independent program, then most likely not.