Implementing File Save/Load Part 1
I began attempting to work the file save/load process as part of the first iteration. I started to create a method like this for the task object.
public string SaveToXml()
{
XmlDocument document = new XmlDocument();
//Create a root node called task
XmlElement rootNode = document.CreateElement("Task");
document.AppendChild(rootNode);
//If the name exists, store this information
if (this.Attributes.Contains("Name"))
{
//Append the name information as an attribute
XmlAttribute nameAttribute = document.CreateAttribute("Name");
nameAttribute.InnerText = this.Attributes["Name"].ToString();
rootNode.Attributes.Append(nameAttribute);
}
//If the creation date exists, store this information
if (this.Attributes.Contains("CreationDate"))
{
//Append the creation date information as an attribute
XmlAttribute creationDateAttribute = document.CreateAttribute("CreationDate");
creationDateAttribute.InnerText = this.Attributes["CreationDate"].ToString();
rootNode.Attributes.Append(creationDateAttribute);
}
//If the due date exists, store this information
if (this.Attributes.Contains("DueDate"))
{
//Append the due date information as an attribute
XmlAttribute dueDateAttribute = document.CreateAttribute("DueDate");
dueDateAttribute.InnerText = this.Attributes["DueDate"].ToString();
rootNode.Attributes.Append(dueDateAttribute);
}
//Create the element for is reoccurring
XmlElement reoccurringElement = document.CreateElement("IsReoccurring");
//If a value exists, set the inner text to the value
if (this.Attributes.Contains("IsReoccurring"))
reoccurringElement.InnerText = this.Attributes["IsReoccurring"].ToString();
//By default, use false for reoccurring
else
reoccurringElement.InnerText = false;
rootNode.AppendChild(reoccurringElement);
//If the reoccurrence element exists, store this information
if (this.Attributes.Contains("ReoccurrenceMeasurement"))
{
//Append the reoccurrence measurement as a child element
XmlElement reoccurrenceMeasurementElement = document.CreateElement("ReoccurrenceMeasurement");
}
}
My reasoning was I would loop through the collection, adding the tasks to a string, creating an XML document, and then save it. One of my thoughts was that I could include this functionality in a method above; however, I don't know of any other feature that would need that yet, and I could refactor that concept later into the application. However, as I was writing it, I wondered what I was doing, because this method needs heavy refactoring itself. This was a bad design on my part, and will need to be reevaluated.
You may also notice that I didn't create a major test for converting as task yet. The reason is because if I'm going to write all of this functionality, I figure I may as well actually write the method or feature, and test it heavily. The reason is, when you create a test, you write a bunch of code that looks similar, then create another bunch of code for the actual implementation. So why not write the implementation and then test it heavily to see if it worked? To me, that is very similar.
Also, I only do this for a limited number of things, like the Object Model API methods; and only where it seems I have the approach, and I would be writing all that code for the test anyway and it seems like I would go way out of my way not to do it right in the object model. View it however you may; I think that is an acceptable approach.