PIbd-21_Ihonkina_E.S._Preca.../PrecastConcreteFileImplement/DataFileSingleton.cs

71 lines
3.2 KiB
C#
Raw Permalink Normal View History

2023-05-20 23:04:30 +04:00
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.StoragesContracts;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantFileImplement.Models;
2023-03-18 17:55:02 +04:00
using System.Xml.Linq;
namespace PrecastConcretePlantFileImplement
2023-03-18 17:40:44 +04:00
{
2023-03-18 17:47:29 +04:00
public class DataFileSingleton
2023-03-18 17:40:44 +04:00
{
2023-03-18 17:55:02 +04:00
private static DataFileSingleton? instance;
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string ReinforcedFileName = "Reinforced.xml";
2023-04-05 00:55:08 +04:00
private readonly string ClientFileName = "Client.xml";
2023-04-28 22:24:51 +04:00
private readonly string ImplementerFileName = "Implementer.xml";
2023-05-20 23:04:30 +04:00
private readonly string MessageFileName = "Message.xml";
public List<Component> Components { get; private set; }
2023-03-18 17:55:02 +04:00
public List<Order> Orders { get; private set; }
public List<Reinforced> Reinforceds { get; private set; }
2023-04-05 00:55:08 +04:00
public List<Client> Clients { get; private set; }
2023-04-28 22:24:51 +04:00
public List<Implementer> Implementers { get; private set; }
2023-05-20 23:04:30 +04:00
public List<Message> Messages { get; private set; }
public static DataFileSingleton GetInstance()
2023-03-18 17:55:02 +04:00
{
if (instance == null)
{
instance = new DataFileSingleton();
}
return instance;
}
public void SaveComponents() => SaveData(Components, ComponentFileName,
"Components", x => x.GetXElement);
public void SaveReinforceds() => SaveData(Reinforceds, ReinforcedFileName,
"Reiforceds", x => x.GetXElement);
2023-05-20 23:04:30 +04:00
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement);
public void SaveImplementers() => SaveData(Implementers, ImplementerFileName, "Implementers", x => x.GetXElement);
public void SaveMessages() => SaveData(Messages, MessageFileName, "Messages", x => x.GetXElement);
private DataFileSingleton()
2023-03-18 17:55:02 +04:00
{
2023-05-20 23:04:30 +04:00
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
Reinforceds = LoadData(ReinforcedFileName, "Reinforced", x => Reinforced.Create(x)!)!;
2023-03-18 17:55:02 +04:00
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
2023-04-05 00:55:08 +04:00
Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!;
2023-04-28 22:24:51 +04:00
Implementers=LoadData(ImplementerFileName, "Implementer", x => Implementer.Create(x)!)!;
2023-05-20 23:04:30 +04:00
Messages = LoadData(MessageFileName, "Message", x => Message.Create(x)!)!;
}
private static List<T>? LoadData<T>(string filename, string xmlNodeName, Func<XElement, T> selectFunction)
2023-03-18 17:55:02 +04:00
{
if (File.Exists(filename))
{
2023-05-20 23:04:30 +04:00
return XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList();
2023-03-18 17:55:02 +04:00
}
return new List<T>();
}
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);
}
}
2023-03-18 17:40:44 +04:00
}
2023-03-18 17:55:02 +04:00
}