update documentation
continuous-integration/drone/push Build is passing Details

pull/39/head
Alexandre AGOSTINHO 2 years ago
parent 21d527a2ce
commit 43c543a33c

@ -11,16 +11,17 @@ Console.WriteLine("Hello, World!\n\n");
// TESTS: // 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 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 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.Export(rc[2], "C:\\Users\\alex6\\Downloads\\recipe2.json");
dataMgr.Import<Recipe>("C:\\Users\\alex6\\Downloads\\recipe2.json"); //dataMgr.Import<Recipe>("C:\\Users\\alex6\\Downloads\\recipe2.json");
RecipeCollection rc = new RecipeCollection("All recipes", dataMgr.Data[nameof(Recipe)].Cast<Recipe>().ToArray()); RecipeCollection rc = new RecipeCollection("All recipes", dataMgr.Data[nameof(Recipe)].Cast<Recipe>().ToArray());

@ -11,11 +11,22 @@ using System.Xml.Linq;
namespace DataPersistence namespace DataPersistence
{ {
/// <summary>
/// Define a serializer to manage JSON files.
/// </summary>
public class DataContractJSON : IDataManager public class DataContractJSON : IDataManager
{ {
#region Attributes
private string _jsonFolderPath; private string _jsonFolderPath;
private DataContractJsonSerializerSettings _dataContractJsonSerializerSettings; private DataContractJsonSerializerSettings _dataContractJsonSerializerSettings;
#endregion
#region Constructors
/// <summary>
/// Constructor of the DataContractJSON serializer.
/// </summary>
/// <param name="jsonFolderPath">Give the default path where to load and save the data (by default is the current execution dir).</param>
/// <param name="dataContractJsonSerializerSettings">Give another set of DataContractJson serializer setting to write file</param>
public DataContractJSON(string jsonFolderPath = "", public DataContractJSON(string jsonFolderPath = "",
DataContractJsonSerializerSettings? dataContractJsonSerializerSettings = null) DataContractJsonSerializerSettings? dataContractJsonSerializerSettings = null)
{ {
@ -28,7 +39,9 @@ namespace DataPersistence
else else
_dataContractJsonSerializerSettings = dataContractJsonSerializerSettings; _dataContractJsonSerializerSettings = dataContractJsonSerializerSettings;
} }
#endregion
#region IDataManager implementation
public void Export<T>(T obj, string pathToExport) public void Export<T>(T obj, string pathToExport)
where T : class where T : class
{ {
@ -93,5 +106,6 @@ namespace DataPersistence
} }
} }
} }
#endregion
} }
} }

@ -10,12 +10,24 @@ using System.Xml.Linq;
namespace DataPersistence namespace DataPersistence
{ {
/// <summary>
/// Define a serializer to manage XML files.
/// </summary>
public class DataContractXML : IDataManager public class DataContractXML : IDataManager
{ {
#region Attributes
private string _xmlFolderPath; private string _xmlFolderPath;
private XmlWriterSettings _xmlWriterSettings; private XmlWriterSettings _xmlWriterSettings;
private DataContractSerializerSettings _dataContractSerializerSettings; private DataContractSerializerSettings _dataContractSerializerSettings;
#endregion
#region Constructors
/// <summary>
/// Constructor of a DataContractXML serializer.
/// </summary>
/// <param name="xmlFolderPath">Give the default path where to load and save the data (by default is the current execution dir).</param>
/// <param name="xmlWriterSettings">Give another set of XML setting to write file.</param>
/// <param name="dataContractSerializerSettings">Give another set of DataContract serializer setting to write file</param>
public DataContractXML(string xmlFolderPath = "", public DataContractXML(string xmlFolderPath = "",
XmlWriterSettings? xmlWriterSettings = null, XmlWriterSettings? xmlWriterSettings = null,
DataContractSerializerSettings? dataContractSerializerSettings = null) DataContractSerializerSettings? dataContractSerializerSettings = null)
@ -36,7 +48,9 @@ namespace DataPersistence
else else
_dataContractSerializerSettings = dataContractSerializerSettings; _dataContractSerializerSettings = dataContractSerializerSettings;
} }
#endregion
#region IDataManager implementation
public void Export<T>(T obj, string pathToExport) public void Export<T>(T obj, string pathToExport)
where T : class where T : class
{ {
@ -96,5 +110,6 @@ namespace DataPersistence
} }
} }
} }
#endregion
} }
} }

@ -7,23 +7,58 @@ using System.Threading.Tasks;
namespace DataPersistence namespace DataPersistence
{ {
/// <summary>
/// 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.
/// </summary>
public class DataManager public class DataManager
{ {
#region Attributes & Properties
/// <summary>
/// The data manager injected that know how to serialize the data.
/// <br/><remarks><i>The setter is actually public for testing purpose. It will be private after.</i></remarks>
/// <br/>See: <see cref="IDataManager"/>
/// </summary>
public IDataManager Serializer { get; set; } public IDataManager Serializer { get; set; }
/// <summary>
/// The collection of all data. Each line of this dictionary has the type of the data as it key and the data for values.
/// </summary>
public Dictionary<string, List<object>> Data { get; private set; } public Dictionary<string, List<object>> Data { get; private set; }
#endregion
#region Constructors
/// <summary>
/// Constructor of the DataManager class. Take a IDataManager that will provide methods for the serialisation of the data.
/// </summary>
/// <param name="dataMgr">The data manager that know how to serialize a file.</param>
public DataManager(IDataManager dataMgr) public DataManager(IDataManager dataMgr)
{ {
Serializer = dataMgr; Serializer = dataMgr;
Data = Serializer.Load(); Data = Serializer.Load();
} }
#endregion
#region Methods
/// <summary>
/// Reload the data. Useful to update new data written in the save file.
/// <br/>See: <see cref="IDataManager.Load"/>
/// </summary>
public void Reload() public void Reload()
=> Data = Serializer.Load(); => Data = Serializer.Load();
/// <summary>
/// Save the data. Call the Save method of the serializer.
/// <br/>See: <see cref="IDataManager.Save(Dictionary{string, List{object}})"/>
/// </summary>
public void Save() public void Save()
=> Serializer.Save(Data); => Serializer.Save(Data);
/// <summary>
/// Import data from a file.
/// <br/>See: <see cref="IDataManager.Import{T}(string)"/>
/// </summary>
/// <typeparam name="T">The type of data to import.</typeparam>
/// <param name="pathOfTheFile">The path containing the name of the file created.</param>
public void Import<T>(string pathOfTheFile) public void Import<T>(string pathOfTheFile)
where T : class where T : class
{ {
@ -31,8 +66,16 @@ namespace DataPersistence
Data[import.Key].Add(import.Value); Data[import.Key].Add(import.Value);
} }
/// <summary>
/// Export the data from the collection of data.
/// <br/>See: <see cref="IDataManager.Export{T}(T, string)"/>
/// </summary>
/// <typeparam name="T">The type of data to export</typeparam>
/// <param name="obj">The object to export</param>
/// <param name="pathToExport">The path containing the name of the file created.</param>
public void Export<T>(T obj, string pathToExport) public void Export<T>(T obj, string pathToExport)
where T : class where T : class
=> Serializer.Export<T>(obj, pathToExport); => Serializer.Export<T>(obj, pathToExport);
#endregion
} }
} }

@ -6,14 +6,38 @@ using System.Threading.Tasks;
namespace DataPersistence namespace DataPersistence
{ {
/// <summary>
/// Interface that define the methods of a data serializer.
/// </summary>
public interface IDataManager public interface IDataManager
{ {
/// <summary>
/// Save all the data in a file.
/// </summary>
/// <param name="elements">The data to save.</param>
void Save(Dictionary<string, List<object>> elements); void Save(Dictionary<string, List<object>> elements);
/// <summary>
/// Load all the data from a file.
/// </summary>
/// <returns>The data loaded.</returns>
Dictionary<string, List<object>> Load(); Dictionary<string, List<object>> Load();
/// <summary>
/// Import an element to the collection of data.
/// </summary>
/// <typeparam name="T">The type of the element to impoert</typeparam>
/// <param name="pathToImport">The path containing the name of the file.</param>
/// <returns>A pair where the key is the entry in the data and the value is the value to add on this entry.</returns>
public KeyValuePair<string, T> Import<T>(string pathToImport) public KeyValuePair<string, T> Import<T>(string pathToImport)
where T : class; where T : class;
/// <summary>
/// Export an element from the collection of data.
/// </summary>
/// <typeparam name="T">The type of the exported object.</typeparam>
/// <param name="obj">The object to export.</param>
/// <param name="pathToExport">The path containing the name of the file created.</param>
public void Export<T>(T obj, string pathToExport) public void Export<T>(T obj, string pathToExport)
where T : class; where T : class;
} }

@ -7,6 +7,9 @@ using System.Threading.Tasks;
namespace DataPersistence namespace DataPersistence
{ {
/// <summary>
/// The subs class is a group of prefabricated object that can only be loaded. It only use is for testing.
/// </summary>
public class Stubs : IDataManager public class Stubs : IDataManager
{ {
public Dictionary<string, List<object>> Load() public Dictionary<string, List<object>> Load()
@ -14,6 +17,7 @@ namespace DataPersistence
Dictionary<string, List<object>> data = new Dictionary<string, List<object>> Dictionary<string, List<object>> data = new Dictionary<string, List<object>>
{ {
{ {
#region Data: Recipes
nameof(Recipe), nameof(Recipe),
new List<object>(new[] new List<object>(new[]
{ {
@ -81,12 +85,14 @@ namespace DataPersistence
new PreparationStep(6, "Dégustez en famille !") new PreparationStep(6, "Dégustez en famille !")
}) })
}) })
#endregion
} }
}; };
return data; return data;
} }
#region Not supported methods
public void Save(Dictionary<string, List<object>> elements) public void Save(Dictionary<string, List<object>> elements)
{ {
throw new NotSupportedException(); throw new NotSupportedException();
@ -101,5 +107,6 @@ namespace DataPersistence
{ {
throw new NotSupportedException(); throw new NotSupportedException();
} }
#endregion
} }
} }

@ -4,7 +4,7 @@
"Value": [ "Value": [
{ {
"__type": "recipe:#Model", "__type": "recipe:#Model",
"id": 8877, "id": 26700,
"preparation-steps": [ "preparation-steps": [
{ {
"description": "Faire cuire.", "description": "Faire cuire.",
@ -19,7 +19,7 @@
}, },
{ {
"__type": "recipe:#Model", "__type": "recipe:#Model",
"id": 20950, "id": 16433,
"preparation-steps": [ "preparation-steps": [
{ {
"description": "Moulinez la pâte.", "description": "Moulinez la pâte.",
@ -38,7 +38,7 @@
}, },
{ {
"__type": "recipe:#Model", "__type": "recipe:#Model",
"id": 23979, "id": 26093,
"preparation-steps": [ "preparation-steps": [
{ {
"description": "Achetez les ingrédients.", "description": "Achetez les ingrédients.",
@ -57,7 +57,7 @@
}, },
{ {
"__type": "recipe:#Model", "__type": "recipe:#Model",
"id": 17989, "id": 21481,
"preparation-steps": [ "preparation-steps": [
{ {
"description": "Achetez les légumes.", "description": "Achetez les légumes.",
@ -80,7 +80,7 @@
}, },
{ {
"__type": "recipe:#Model", "__type": "recipe:#Model",
"id": 28864, "id": 15049,
"preparation-steps": [ "preparation-steps": [
{ {
"description": "Ajouter les oeufs.", "description": "Ajouter les oeufs.",
@ -107,7 +107,7 @@
}, },
{ {
"__type": "recipe:#Model", "__type": "recipe:#Model",
"id": 30406, "id": 28153,
"preparation-steps": [ "preparation-steps": [
{ {
"description": "Faire une cuisson bien sec de la dinde à la poêle", "description": "Faire une cuisson bien sec de la dinde à la poêle",
@ -134,7 +134,7 @@
}, },
{ {
"__type": "recipe:#Model", "__type": "recipe:#Model",
"id": 17097, "id": 8053,
"preparation-steps": [ "preparation-steps": [
{ {
"description": "Trouvez des épices de curry.", "description": "Trouvez des épices de curry.",
@ -162,29 +162,6 @@
} }
], ],
"title": "Poulet au curry" "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"
} }
] ]
} }

Loading…
Cancel
Save