Реализован класс DataFileSingleton

This commit is contained in:
Никита Потапов 2024-02-26 09:48:14 +04:00
parent 4e317f4971
commit c1bc36a049
2 changed files with 57 additions and 14 deletions

View File

@ -0,0 +1,50 @@
using SecuritySystemFileImplement.Models;
using System.Xml.Linq;
namespace SecuritySystemFileImplement
{
internal class DataFileSingleton
{
private static DataFileSingleton? instance;
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string SecureFileName = "Secure.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Secure> Secures { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
{
instance = new DataFileSingleton();
}
return instance;
}
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement);
public void SaveSecures() => SaveData(Secures, SecureFileName, "Secures", x => x.GetXElement);
public void SaveOrders() { }
private DataFileSingleton()
{
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
Secures = LoadData(SecureFileName, "Secure", x => Secure.Create(x)!)!;
Orders = new List<Order>();
}
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>();
}
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);
}
}
}
}

View File

@ -1,4 +1,5 @@
using SecuritySystemContracts.BindingModels;
using SecuritySystemContracts.ViewModels;
using SecuritySystemDataModels.Models;
using System.Xml.Linq;
@ -10,8 +11,7 @@ namespace SecuritySystemFileImplement.Models
public string SecureName { get; private set; } = string.Empty;
public double Price { get; private set; }
public Dictionary<int, int> Components { get; private set; } = new();
private Dictionary<int, (IComponentModel, int)>? _secureComponents =
null;
private Dictionary<int, (IComponentModel, int)>? _secureComponents = null;
public Dictionary<int, (IComponentModel, int)> SecureComponents
{
get
@ -19,9 +19,8 @@ namespace SecuritySystemFileImplement.Models
if (_secureComponents == null)
{
var source = DataFileSingleton.GetInstance();
_secureComponents = Components.ToDictionary(x => x.Key, y =>
((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!,
y.Value));
_secureComponents = Components.ToDictionary(x => x.Key,
y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value));
}
return _secureComponents;
}
@ -37,8 +36,7 @@ namespace SecuritySystemFileImplement.Models
Id = model.Id,
SecureName = model.SecureName,
Price = model.Price,
Components = model.SecureComponents.ToDictionary(x => x.Key, x
=> x.Value.Item2)
Components = model.SecureComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Secure? Create(XElement element)
@ -52,11 +50,7 @@ namespace SecuritySystemFileImplement.Models
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
SecureName = element.Element("SecureName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
Components =
element.Element("SecureComponents")!.Elements("SecureComponent")
.ToDictionary(x =>
Convert.ToInt32(x.Element("Key")?.Value), x =>
Convert.ToInt32(x.Element("Value")?.Value))
Components = element.Element("SecureComponents")!.Elements("SecureComponent").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(SecureBindingModel model)
@ -67,8 +61,7 @@ namespace SecuritySystemFileImplement.Models
}
SecureName = model.SecureName;
Price = model.Price;
Components = model.SecureComponents.ToDictionary(x => x.Key, x =>
x.Value.Item2);
Components = model.SecureComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
_secureComponents = null;
}
public SecureViewModel GetViewModel => new()