PIbd-22_Bulatova_K.R._Compu.../ComputersShop/ComputersShopFileImplement/DataFileSingleton.cs

134 lines
4.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ComputersShopFileImplement.Models;
using System.Xml.Linq;
namespace ComputersShopFileImplement
{
internal class DataFileSingleton
{
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 ComputerFileName = "Computer.xml";
/// <summary>
/// Название файла для хранения информации о магазинах
/// </summary>
private readonly string ShopFileName = "Shop.xml";
/// <summary>
/// Список классов-моделей компонентов
/// </summary>
public List<Component> Components { get; set; }
/// <summary>
/// Список классов-моделей заказов
/// </summary>
public List<Order> Orders { get; set; }
/// <summary>
/// Список классов-моделей изделий
/// </summary>
public List<Computer> Computers { get; set; }
/// <summary>
/// Список классов-моделей магазина
/// </summary>
public List<Shop> Shops { get; set; }
/// <summary>
/// Конструктор
/// </summary>
private DataFileSingleton()
{
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
Computers = LoadData(ComputerFileName, "Computer", x => Computer.Create(x)!)!;
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
Shops = LoadData(ShopFileName, "Shop", x => Shop.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 SaveComputers() => SaveData(Computers, ComputerFileName, "Computers", x => x.GetXElement);
/// <summary>
/// Сохранение заказов
/// </summary>
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
/// <summary>
/// Сохранение магазинов
/// </summary>
public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", 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);
}
}
}
}