redSkywalker

Сакутин_сериализация в хранилище

Mar 15th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Linq;
  5. using System.IO;
  6. using System.Runtime.Serialization;
  7. using System.Runtime.Serialization.Formatters.Binary;
  8. using System.Text;
  9.  
  10. namespace IMJunior
  11. {
  12.     class Program
  13.     {
  14.         static UserRepository _users = new UserRepository();
  15.  
  16.         static void Main()
  17.         {
  18.             while (true)
  19.             {
  20.                 string command = Console.ReadLine();
  21.                 switch (command)
  22.                 {
  23.                     case "Update":
  24.                         Console.WriteLine("Id:");
  25.                         int id = Convert.ToInt32(Console.ReadLine());
  26.                         Console.WriteLine("Name:");
  27.                         string name = Console.ReadLine();
  28.                         Console.WriteLine("Money:");
  29.                         float money = (float)Convert.ToDouble(Console.ReadLine());
  30.                         Console.WriteLine(UpdateUser(id, name, money));
  31.  
  32.                         break;
  33.                     case "Create":
  34.  
  35.                         Console.WriteLine("Name:");
  36.                         string name2 = Console.ReadLine();
  37.                         Console.WriteLine("Money:");
  38.                         float money2 = (float)Convert.ToDouble(Console.ReadLine());
  39.                         Console.WriteLine(CreateUser(name2, money2));
  40.  
  41.                         break;
  42.                     case "Show":
  43.                         Console.WriteLine(ShowUsers());
  44.                         break;
  45.                 }
  46.             }
  47.         }
  48.  
  49.         static string UpdateUser(int id, string name, float? money)
  50.         {
  51.             User user = _users.GetById(id);
  52.             if (user == null) return "Вы не можете обновить это пользователя";
  53.  
  54.             if (name != null)
  55.             {
  56.                 user.Name = name;
  57.             }
  58.             if (name != null)
  59.             {
  60.                 user.Money = (float)money;
  61.             }
  62.  
  63.             return "Обновление прошло успешно";
  64.         }
  65.  
  66.         static string CreateUser(string name, float money)
  67.         {
  68.             User user = _users.Create();
  69.  
  70.             user.Name = name;
  71.             user.Money = money;
  72.  
  73.             return "Пользователь создан";
  74.         }
  75.  
  76.         static string ShowUsers()
  77.         {
  78.             StringBuilder response = new StringBuilder();
  79.  
  80.             foreach (var user in _users.GetAll())
  81.             {
  82.                 response.Append($"[{user.Id}] {user.Name} - {user.Money}\n");
  83.             }
  84.  
  85.             return response.ToString();
  86.         }
  87.     }
  88.  
  89.     class UserRepository
  90.     {
  91.         private readonly List<User> _users = new List<User>();
  92.        
  93.         public UserRepository()
  94.         {
  95.             using (var stream = File.Open($"database/users.database", FileMode.OpenOrCreate))
  96.             {
  97.                 if (stream.Length == 0) return;
  98.  
  99.                 IFormatter formater = new BinaryFormatter();
  100.                 _users = (List<User>)formater.Deserialize(stream);
  101.  
  102.                 foreach (var user in _users)
  103.                 {
  104.                     user.OnDirty = Update;
  105.                 }
  106.             }
  107.         }
  108.  
  109.         public User[] GetAll()
  110.         {
  111.             return _users.ToArray();
  112.         }
  113.  
  114.         public User GetById(int id)
  115.         {
  116.             return _users.FirstOrDefault((user) => user.Id == id);
  117.         }
  118.  
  119.         public User Create()
  120.         {
  121.             int id = 0;
  122.             if (_users.Count > 0)
  123.                 id = _users.Max((u) => u.Id) + 1;
  124.  
  125.             User user = new User(id);
  126.             user.OnDirty = Update;
  127.             _users.Add(user);
  128.             SaveList();
  129.  
  130.             return user;
  131.         }
  132.  
  133.         public void Update(User user)
  134.         {
  135.             //
  136.             SaveList();
  137.         }
  138.  
  139.         public void Delete(User user)
  140.         {
  141.             _users.Remove(user);
  142.             SaveList();
  143.         }
  144.  
  145.         private void SaveList()
  146.         {
  147.             using (var stream = File.Open($"database/users.database", FileMode.OpenOrCreate))
  148.             {
  149.                 IFormatter formater = new BinaryFormatter();
  150.                 formater.Serialize(stream, _users);
  151.  
  152.                 stream.Flush();
  153.             }
  154.         }
  155.     }
  156.  
  157.     delegate void DirtyHandler(User user);
  158.  
  159.     [Serializable]
  160.     class User
  161.     {
  162.         public int Id { get; private set; }
  163.         private string _name;
  164.         private float _money;
  165.  
  166.         [NonSerialized]
  167.         public DirtyHandler OnDirty;
  168.  
  169.         public string Name {
  170.             get => _name;
  171.             set
  172.             {
  173.                 _name = value;
  174.                 if(OnDirty != null)
  175.                 {
  176.                     OnDirty(this);
  177.                 }
  178.             }
  179.         }
  180.         public float Money {
  181.             get => _money;
  182.             set
  183.             {
  184.                 _money = value;
  185.                 if (OnDirty != null)
  186.                 {
  187.                     OnDirty(this);
  188.                 }
  189.             }
  190.         }
  191.  
  192.         public User(int id)
  193.         {
  194.             Id = id;
  195.         }
  196.     }
  197. }
Add Comment
Please, Sign In to add comment