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-04-07 00:02:29 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Название файла для хранения информации о клиентах
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string ClientFileName = "Client.xml";
|
|
|
|
|
|
2024-04-20 22:35:38 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Название файла для хранения информации о исполнителях
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string ImplementerFileName = "Implementer.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-04-07 00:02:29 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Список классов-моделей клиентов
|
|
|
|
|
/// </summary>
|
|
|
|
|
public List<Client> Clients { get; set; }
|
|
|
|
|
|
2024-04-20 22:35:38 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Список классов-моделей исполнителей
|
|
|
|
|
/// </summary>
|
|
|
|
|
public List<Implementer> Implementers { 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-04-07 00:02:29 +04:00
|
|
|
|
Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!;
|
2024-04-20 22:35:38 +04:00
|
|
|
|
Implementers = LoadData(ImplementerFileName, "Implementer", x => Implementer.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-04-07 00:02:29 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сохранение клиентов
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement);
|
|
|
|
|
|
2024-04-20 22:35:38 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сохранение исполнителей
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SaveImplementers() => SaveData(Implementers, ImplementerFileName, "Implementers", 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|