123 lines
4.3 KiB
C#
123 lines
4.3 KiB
C#
|
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";
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Список классов-моделей компонентов
|
|||
|
/// </summary>
|
|||
|
public List<Component> Components { get; set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Список классов-моделей заказов
|
|||
|
/// </summary>
|
|||
|
public List<Order> Orders { get; set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Список классов-моделей изделий
|
|||
|
/// </summary>
|
|||
|
public List<Plane> Planes { get; set; }
|
|||
|
|
|||
|
/// <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)!)!;
|
|||
|
}
|
|||
|
|
|||
|
/// <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);
|
|||
|
|
|||
|
/// <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);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|