From 43c543a33c7efed7b780bae9ffd17548cb344503 Mon Sep 17 00:00:00 2001 From: Alexandre Agostinho Date: Sun, 14 May 2023 21:45:27 +0200 Subject: [PATCH] update documentation --- MCTG/ConsoleApp/Program.cs | 9 ++--- MCTG/DataPersistence/DataContractJSON.cs | 14 ++++++++ MCTG/DataPersistence/DataContractXML.cs | 15 +++++++++ MCTG/DataPersistence/DataManager.cs | 43 ++++++++++++++++++++++++ MCTG/DataPersistence/IDataManager.cs | 24 +++++++++++++ MCTG/DataPersistence/Stubs.cs | 7 ++++ MCTG/DataPersistence/data/data.json | 37 ++++---------------- 7 files changed, 115 insertions(+), 34 deletions(-) diff --git a/MCTG/ConsoleApp/Program.cs b/MCTG/ConsoleApp/Program.cs index 190fe4b..554006c 100644 --- a/MCTG/ConsoleApp/Program.cs +++ b/MCTG/ConsoleApp/Program.cs @@ -11,16 +11,17 @@ Console.WriteLine("Hello, World!\n\n"); // TESTS: -DataManager dataMgr = new DataManager(new Stubs()); +//DataManager dataMgr = new DataManager(new Stubs()); //DataManager dataMgr = new DataManager(new DataContractXML(xmlFolderPath: "../../../../DataPersistence/data")); -//DataManager dataMgr = new DataManager(new DataContractJSON(jsonFolderPath: "../../../../DataPersistence/data")); +DataManager dataMgr = new DataManager(new DataContractJSON(jsonFolderPath: "../../../../DataPersistence/data")); //dataMgr.Serializer = new DataContractXML(xmlFolderPath: "../../../../DataPersistence/data"); -dataMgr.Serializer = new DataContractJSON(jsonFolderPath: "../../../../DataPersistence/data"); +//dataMgr.Serializer = new DataContractJSON(jsonFolderPath: "../../../../DataPersistence/data"); +// /!\ here is an absolute path I put for testing purpose. It will only work on my computer so don't forget to change it whene you test. //dataMgr.Export(rc[2], "C:\\Users\\alex6\\Downloads\\recipe2.json"); -dataMgr.Import("C:\\Users\\alex6\\Downloads\\recipe2.json"); +//dataMgr.Import("C:\\Users\\alex6\\Downloads\\recipe2.json"); RecipeCollection rc = new RecipeCollection("All recipes", dataMgr.Data[nameof(Recipe)].Cast().ToArray()); diff --git a/MCTG/DataPersistence/DataContractJSON.cs b/MCTG/DataPersistence/DataContractJSON.cs index c250a2e..382a4eb 100644 --- a/MCTG/DataPersistence/DataContractJSON.cs +++ b/MCTG/DataPersistence/DataContractJSON.cs @@ -11,11 +11,22 @@ using System.Xml.Linq; namespace DataPersistence { + /// + /// Define a serializer to manage JSON files. + /// public class DataContractJSON : IDataManager { + #region Attributes private string _jsonFolderPath; private DataContractJsonSerializerSettings _dataContractJsonSerializerSettings; + #endregion + #region Constructors + /// + /// Constructor of the DataContractJSON serializer. + /// + /// Give the default path where to load and save the data (by default is the current execution dir). + /// Give another set of DataContractJson serializer setting to write file public DataContractJSON(string jsonFolderPath = "", DataContractJsonSerializerSettings? dataContractJsonSerializerSettings = null) { @@ -28,7 +39,9 @@ namespace DataPersistence else _dataContractJsonSerializerSettings = dataContractJsonSerializerSettings; } + #endregion + #region IDataManager implementation public void Export(T obj, string pathToExport) where T : class { @@ -93,5 +106,6 @@ namespace DataPersistence } } } + #endregion } } diff --git a/MCTG/DataPersistence/DataContractXML.cs b/MCTG/DataPersistence/DataContractXML.cs index b805ec6..3944c85 100644 --- a/MCTG/DataPersistence/DataContractXML.cs +++ b/MCTG/DataPersistence/DataContractXML.cs @@ -10,12 +10,24 @@ using System.Xml.Linq; namespace DataPersistence { + /// + /// Define a serializer to manage XML files. + /// public class DataContractXML : IDataManager { + #region Attributes private string _xmlFolderPath; private XmlWriterSettings _xmlWriterSettings; private DataContractSerializerSettings _dataContractSerializerSettings; + #endregion + #region Constructors + /// + /// Constructor of a DataContractXML serializer. + /// + /// Give the default path where to load and save the data (by default is the current execution dir). + /// Give another set of XML setting to write file. + /// Give another set of DataContract serializer setting to write file public DataContractXML(string xmlFolderPath = "", XmlWriterSettings? xmlWriterSettings = null, DataContractSerializerSettings? dataContractSerializerSettings = null) @@ -36,7 +48,9 @@ namespace DataPersistence else _dataContractSerializerSettings = dataContractSerializerSettings; } + #endregion + #region IDataManager implementation public void Export(T obj, string pathToExport) where T : class { @@ -96,5 +110,6 @@ namespace DataPersistence } } } + #endregion } } diff --git a/MCTG/DataPersistence/DataManager.cs b/MCTG/DataPersistence/DataManager.cs index 2ef82af..a06d94d 100644 --- a/MCTG/DataPersistence/DataManager.cs +++ b/MCTG/DataPersistence/DataManager.cs @@ -7,23 +7,58 @@ using System.Threading.Tasks; namespace DataPersistence { + /// + /// Define the manager of the data. This is where all the data are put, and where we call the loading and the saving of them. + /// public class DataManager { + #region Attributes & Properties + /// + /// The data manager injected that know how to serialize the data. + ///
The setter is actually public for testing purpose. It will be private after. + ///
See: + ///
public IDataManager Serializer { get; set; } + + /// + /// The collection of all data. Each line of this dictionary has the type of the data as it key and the data for values. + /// public Dictionary> Data { get; private set; } + #endregion + #region Constructors + /// + /// Constructor of the DataManager class. Take a IDataManager that will provide methods for the serialisation of the data. + /// + /// The data manager that know how to serialize a file. public DataManager(IDataManager dataMgr) { Serializer = dataMgr; Data = Serializer.Load(); } + #endregion + #region Methods + /// + /// Reload the data. Useful to update new data written in the save file. + ///
See: + ///
public void Reload() => Data = Serializer.Load(); + /// + /// Save the data. Call the Save method of the serializer. + ///
See: + ///
public void Save() => Serializer.Save(Data); + /// + /// Import data from a file. + ///
See: + ///
+ /// The type of data to import. + /// The path containing the name of the file created. public void Import(string pathOfTheFile) where T : class { @@ -31,8 +66,16 @@ namespace DataPersistence Data[import.Key].Add(import.Value); } + /// + /// Export the data from the collection of data. + ///
See: + ///
+ /// The type of data to export + /// The object to export + /// The path containing the name of the file created. public void Export(T obj, string pathToExport) where T : class => Serializer.Export(obj, pathToExport); + #endregion } } diff --git a/MCTG/DataPersistence/IDataManager.cs b/MCTG/DataPersistence/IDataManager.cs index 49d8553..95209d8 100644 --- a/MCTG/DataPersistence/IDataManager.cs +++ b/MCTG/DataPersistence/IDataManager.cs @@ -6,14 +6,38 @@ using System.Threading.Tasks; namespace DataPersistence { + /// + /// Interface that define the methods of a data serializer. + /// public interface IDataManager { + /// + /// Save all the data in a file. + /// + /// The data to save. void Save(Dictionary> elements); + + /// + /// Load all the data from a file. + /// + /// The data loaded. Dictionary> Load(); + /// + /// Import an element to the collection of data. + /// + /// The type of the element to impoert + /// The path containing the name of the file. + /// A pair where the key is the entry in the data and the value is the value to add on this entry. public KeyValuePair Import(string pathToImport) where T : class; + /// + /// Export an element from the collection of data. + /// + /// The type of the exported object. + /// The object to export. + /// The path containing the name of the file created. public void Export(T obj, string pathToExport) where T : class; } diff --git a/MCTG/DataPersistence/Stubs.cs b/MCTG/DataPersistence/Stubs.cs index 89fe5b9..cc09f39 100644 --- a/MCTG/DataPersistence/Stubs.cs +++ b/MCTG/DataPersistence/Stubs.cs @@ -7,6 +7,9 @@ using System.Threading.Tasks; namespace DataPersistence { + /// + /// The subs class is a group of prefabricated object that can only be loaded. It only use is for testing. + /// public class Stubs : IDataManager { public Dictionary> Load() @@ -14,6 +17,7 @@ namespace DataPersistence Dictionary> data = new Dictionary> { { + #region Data: Recipes nameof(Recipe), new List(new[] { @@ -81,12 +85,14 @@ namespace DataPersistence new PreparationStep(6, "Dégustez en famille !") }) }) + #endregion } }; return data; } + #region Not supported methods public void Save(Dictionary> elements) { throw new NotSupportedException(); @@ -101,5 +107,6 @@ namespace DataPersistence { throw new NotSupportedException(); } + #endregion } } diff --git a/MCTG/DataPersistence/data/data.json b/MCTG/DataPersistence/data/data.json index a3f4c7a..8d25dfd 100644 --- a/MCTG/DataPersistence/data/data.json +++ b/MCTG/DataPersistence/data/data.json @@ -4,7 +4,7 @@ "Value": [ { "__type": "recipe:#Model", - "id": 8877, + "id": 26700, "preparation-steps": [ { "description": "Faire cuire.", @@ -19,7 +19,7 @@ }, { "__type": "recipe:#Model", - "id": 20950, + "id": 16433, "preparation-steps": [ { "description": "Moulinez la pâte.", @@ -38,7 +38,7 @@ }, { "__type": "recipe:#Model", - "id": 23979, + "id": 26093, "preparation-steps": [ { "description": "Achetez les ingrédients.", @@ -57,7 +57,7 @@ }, { "__type": "recipe:#Model", - "id": 17989, + "id": 21481, "preparation-steps": [ { "description": "Achetez les légumes.", @@ -80,7 +80,7 @@ }, { "__type": "recipe:#Model", - "id": 28864, + "id": 15049, "preparation-steps": [ { "description": "Ajouter les oeufs.", @@ -107,7 +107,7 @@ }, { "__type": "recipe:#Model", - "id": 30406, + "id": 28153, "preparation-steps": [ { "description": "Faire une cuisson bien sec de la dinde à la poêle", @@ -134,7 +134,7 @@ }, { "__type": "recipe:#Model", - "id": 17097, + "id": 8053, "preparation-steps": [ { "description": "Trouvez des épices de curry.", @@ -162,29 +162,6 @@ } ], "title": "Poulet au curry" - }, - { - "__type": "recipe:#Model", - "id": 23542, - "preparation-steps": [ - { - "description": "Achetez les ingrédients.", - "order": 1 - }, - { - "description": "Préparez le matériel. Ustensiles et tout.", - "order": 2 - }, - { - "description": "Pleurez.", - "order": 3 - }, - { - "description": "Ce n'est pourtant pas une fatalité !", - "order": 4 - } - ], - "title": "Gateau nature v3" } ] }