/// <summary> /// The method deserializes all the files in the path and returns a list of the generic typed objects. /// </summary> /// <param name="path"> /// The path where the persisted objects are located. /// </param> /// <returns> /// A list of the generic typed objects /// </returns> public static IList <T> GetAllPersistedObjects(string path) { var listOfObjects = new List <T>(); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var directory = new DirectoryInfo(path); var fileInfos = directory.GetFiles().OrderBy(f => f.LastWriteTime); foreach (var file in fileInfos) { try { var persistedObject = PersistenceHelp <T> .Load(file.FullName); listOfObjects.Add(persistedObject); } catch (Exception ex) { var msg = string.Format("Cannot Load '{0}'. Reason: {1}", file.Name, ex.Message); // TODO: Log the exception when we integrate with the logger. } } return(listOfObjects); }
/// <summary> /// Serialize the object to an xml string. /// </summary> /// <returns> /// The xml string of the serialized object. If there is a failure, /// the exception is logged and the xml string will be string.Empty. /// </returns> public string ToXml() { string xml; try { xml = PersistenceHelp <T> .ToXml(this); } catch { return(string.Empty); } return(xml); }
/// <summary> /// Deserialize the object from an xml string. /// </summary> /// <param name="xml"> /// The xml to deserialize. /// </param> /// <returns> /// The generic object or null if there is a failure. /// </returns> public static T FromXml(string xml) { T obj; if (string.IsNullOrEmpty(xml)) { return(default(T)); } try { obj = PersistenceHelp <T> .FromXml(xml); } catch { return(default(T)); } return(obj); }
/// <summary> /// Retrieves an object instance. This is static so that we don't have to /// create the object, call this method, and then assign it. /// </summary> /// <param name="folder"> /// The folder. /// </param> /// <param name="key"> /// The key. /// </param> /// <param name="namespaceToIgnore"> /// This is used when we need to ignore the namespace (e.g. the namespace was changed from version /// to version). /// </param> /// <returns> /// Instance referenced by the key. Null if the object is not found. /// In case of errors, the exception is logged /// by the <see cref="PersistenceHelp{T}"/>. /// </returns> public static T Retrieve(string folder, string key, string namespaceToIgnore) { return(PersistenceHelp <T> .Load(GetFilePath(folder, key), namespaceToIgnore)); }
/// <summary> /// Retrieves an object instance. This is static so that we don't have to /// create the object, call this method, and then assign it. /// </summary> /// <param name="folder"> /// Folder where the persisted objects are located. /// </param> /// <param name="key"> /// Key to retrieve the object. /// </param> /// <returns> /// Instance referenced by the key. Null if the object is not found. /// In case of errors, the exception is logged /// by the <see cref="PersistenceHelp{T}"/>. /// </returns> public static T Retrieve(string folder, string key) { return(PersistenceHelp <T> .Load(GetFilePath(folder, key))); }
/// <summary> /// Method to remove the persisted instance of type T from the persistence mechanism. /// </summary> /// <param name="folder"> /// Folder where the persisted objects are located. /// </param> /// <param name="key"> /// Key for object instance to remove. /// </param> /// <exception cref="CommonComponentsException">Throw if there is a problem removing the file.</exception> public void Remove(string folder, string key) { PersistenceHelp <T> .Remove(GetFilePath(folder, key)); }
/// <summary> /// Method that persists the instance. It should support /// both new instance creation as well as updates. /// </summary> /// <param name="folder"> /// Folder where the persisted objects are located. /// </param> /// <param name="key"> /// key for object instance to save. /// </param> /// <exception cref="CommonComponentsException">Throw if there is a problem saving the file.</exception> public void Save(string folder, string key) { PersistenceHelp <T> .Save(GetFilePath(folder, key), this); }