From fd60c0ff1fdac4c2846c9dfe5de681a389de2ec7 Mon Sep 17 00:00:00 2001 From: Miguel Date: Sat, 22 Jun 2024 19:10:51 +0200 Subject: [PATCH] Add project files. --- EstadoPersistente.cs | 70 +++++++++++++++++++++++++++++++++++++++++++ libPersistence.csproj | 14 +++++++++ libPersistence.sln | 25 ++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 EstadoPersistente.cs create mode 100644 libPersistence.csproj create mode 100644 libPersistence.sln diff --git a/EstadoPersistente.cs b/EstadoPersistente.cs new file mode 100644 index 0000000..70ac5c0 --- /dev/null +++ b/EstadoPersistente.cs @@ -0,0 +1,70 @@ +using System; +using System.IO; +using System.Reflection; +using System.Windows; +using Newtonsoft.Json; + +namespace libPersistence +{ + + public abstract class EstadoPersistente where T : EstadoPersistente, new() + { + private static readonly Lazy instance = new Lazy(() => 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(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); + } + } + + +} diff --git a/libPersistence.csproj b/libPersistence.csproj new file mode 100644 index 0000000..e8607f5 --- /dev/null +++ b/libPersistence.csproj @@ -0,0 +1,14 @@ + + + + net8.0-windows + enable + true + enable + + + + + + + diff --git a/libPersistence.sln b/libPersistence.sln new file mode 100644 index 0000000..e90a35c --- /dev/null +++ b/libPersistence.sln @@ -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