Add project files.
This commit is contained in:
parent
712c8d1f2a
commit
fd60c0ff1f
|
@ -0,0 +1,70 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Windows;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace libPersistence
|
||||
{
|
||||
|
||||
public abstract class EstadoPersistente<T> where T : EstadoPersistente<T>, new()
|
||||
{
|
||||
private static readonly Lazy<T> instance = new Lazy<T>(() => Load());
|
||||
|
||||
static EstadoPersistente()
|
||||
{
|
||||
Application.Current.Exit += OnApplicationExit;
|
||||
}
|
||||
|
||||
public static T Instance => instance.Value;
|
||||
|
||||
private static string GetFileName()
|
||||
{
|
||||
var appName = AppDomain.CurrentDomain.FriendlyName;
|
||||
var className = typeof(T).Name;
|
||||
var folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), appName);
|
||||
Directory.CreateDirectory(folderPath);
|
||||
return Path.Combine(folderPath, $"{className}.json");
|
||||
}
|
||||
|
||||
private static T Load()
|
||||
{
|
||||
var fileName = GetFileName();
|
||||
if (File.Exists(fileName))
|
||||
{
|
||||
var settings = new JsonSerializerSettings
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.Auto,
|
||||
ObjectCreationHandling = ObjectCreationHandling.Replace,
|
||||
// PreserveReferencesHandling = PreserveReferencesHandling.Objects,
|
||||
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
|
||||
};
|
||||
var jsonString = File.ReadAllText(fileName);
|
||||
return JsonConvert.DeserializeObject<T>(jsonString) ?? new T();
|
||||
}
|
||||
return new T();
|
||||
}
|
||||
|
||||
private static void OnApplicationExit(object sender, ExitEventArgs e)
|
||||
{
|
||||
Instance.Save();
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
var fileName = GetFileName();
|
||||
var settings = new JsonSerializerSettings
|
||||
{
|
||||
Formatting = Formatting.Indented,
|
||||
// PreserveReferencesHandling = PreserveReferencesHandling.Objects,
|
||||
NullValueHandling = NullValueHandling.Ignore,
|
||||
TypeNameHandling = TypeNameHandling.Auto
|
||||
};
|
||||
// Serializar
|
||||
var serializedData = JsonConvert.SerializeObject(this, settings);
|
||||
File.WriteAllText(fileName, serializedData);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0-windows</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<UseWPF>true</UseWPF>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,25 @@
|
|||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.10.34928.147
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "libPersistence", "libPersistence.csproj", "{DF086CA8-DF0E-4A37-9527-3823EB617A35}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{DF086CA8-DF0E-4A37-9527-3823EB617A35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{DF086CA8-DF0E-4A37-9527-3823EB617A35}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{DF086CA8-DF0E-4A37-9527-3823EB617A35}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{DF086CA8-DF0E-4A37-9527-3823EB617A35}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {848C4205-DE9A-4A71-9A42-23D49A1CBA36}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
Loading…
Reference in New Issue