2024-02-25 01:22:22 +04:00
|
|
|
|
using AircraftPlantFileImplement.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
|
|
|
|
namespace AircraftPlantFileImplement
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Класс для хранения списков классов-моделей (паттерн Singleton)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class DataFileSingleton
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ссылка на класс
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static DataFileSingleton? _instance;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Название файла для хранения информации о компонентах
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string ComponentFileName = "Component.xml";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Название файла для хранения информации о заказах
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string OrderFileName = "Order.xml";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Название файла для хранения информации о изделиях
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string PlaneFileName = "Plane.xml";
|
|
|
|
|
|
2024-03-11 02:22:57 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Название файла для хранения информации о магазинах
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string ShopFileName = "Shop.xml";
|
|
|
|
|
|
2024-02-25 01:22:22 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Список классов-моделей компонентов
|
|
|
|
|
/// </summary>
|
|
|
|
|
public List<Component> Components { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Список классов-моделей заказов
|
|
|
|
|
/// </summary>
|
|
|
|
|
public List<Order> Orders { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Список классов-моделей изделий
|
|
|
|
|
/// </summary>
|
|
|
|
|
public List<Plane> Planes { get; set; }
|
|
|
|
|
|
2024-03-11 02:22:57 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Список классов-моделей магазина
|
|
|
|
|
/// </summary>
|
|
|
|
|
public List<Shop> Shops { get; set; }
|
|
|
|
|
|
2024-02-25 01:22:22 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
private DataFileSingleton()
|
|
|
|
|
{
|
|
|
|
|
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
|
|
|
|
|
Planes = LoadData(PlaneFileName, "Plane", x => Plane.Create(x)!)!;
|
|
|
|
|
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
|
2024-03-11 02:22:57 +04:00
|
|
|
|
Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!;
|
2024-02-25 01:22:22 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получить ссылку на класс
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static DataFileSingleton GetInstance()
|
|
|
|
|
{
|
|
|
|
|
if (_instance == null)
|
|
|
|
|
{
|
|
|
|
|
_instance = new DataFileSingleton();
|
|
|
|
|
}
|
|
|
|
|
return _instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сохранение компонентов
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сохранение изделий
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SavePlanes() => SaveData(Planes, PlaneFileName, "Planes", x => x.GetXElement);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сохранение заказов
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
|
|
|
|
|
|
2024-03-11 02:22:57 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сохранение магазинов
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement);
|
|
|
|
|
|
2024-02-25 01:22:22 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Метод для загрузки данных из xml-файла
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="filename"></param>
|
|
|
|
|
/// <param name="xmlNodeName"></param>
|
|
|
|
|
/// <param name="selectFunction"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private static List<T>? LoadData<T>(string filename, string xmlNodeName, Func<XElement, T> selectFunction)
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(filename))
|
|
|
|
|
{
|
|
|
|
|
return XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList();
|
|
|
|
|
}
|
|
|
|
|
return new List<T>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Метод для сохранения данных в xml-файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="data"></param>
|
|
|
|
|
/// <param name="filename"></param>
|
|
|
|
|
/// <param name="xmlNodeName"></param>
|
|
|
|
|
/// <param name="selectFunction"></param>
|
|
|
|
|
private static void SaveData<T>(List<T> data, string filename, string xmlNodeName, Func<T, XElement> selectFunction)
|
|
|
|
|
{
|
|
|
|
|
if (data != null)
|
|
|
|
|
{
|
|
|
|
|
new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|