Hello and welcome to my blog.
After being at Conchango for 10 months I have finally gotten around to starting my own blog page! Since starting at Conchango my skill set has been growing exponentally*. I've had to learn how to use; .Net framework 3.5 (WPF, WF, WCF), Team Foundation Server, Team System, WiX and stuff that I'm already starting to forget!
* This may be a slight exageration.
I've also written a couple of articles under the guise of "Scrum for Team System" and you may have spotted my webcasts on the Scrum for Team System site.
If not (and if your interested in the new Scrum for Team System WPF utility I've written) check out the web casts here:
And the Scrum for Team System blog posts here:
In addition I man the SfTS community forum, helping people with their TFS questions where I can.
Well that's enough of an introduction. How about something useful?
Ever wanted to store a custom user defined type in the savable .Net user settings? The below example base class uses XmlSerialization to make this possible...
C# .Net - Adding a custom type instance to the Property Settings (using XmlSerialization).
using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
public abstract class UserSettingBase<T>
{
private static XmlSerializer _serializer;
private static XmlSerializer Serializer
{
get
{
return _serializer = _serializer ?? new XmlSerializer(typeof(T));
}
}
protected static T Load(String settingName)
{
var output = default(T);
var source = Properties.Settings.Default[settingName] as String;
if (!String.IsNullOrEmpty(source))
{
using (var sr = new StringReader(source))
{
output = (T) Serializer.Deserialize(sr);
sr.Close();
}
}
return output;
}
protected void Save(String settingName, T item)
{
var sb = new StringBuilder();
using(var sw = new StringWriter(sb))
{
Serializer.Serialize(sw, item);
sw.Close();
}
Properties.Settings.Default[settingName] = sb.ToString();
}
public abstract void LoadFromSettings();
public abstract void SaveToSettings();
}
The below classes give an implemnetatiton example:
using System;
using System.Collections.Generic;
public class DemoType : UserSettingBase<DemoType>
{
private const String MY_SETTING_NAME = "DemoTypeSettingName";
public override void LoadFromSettings()
{
Items = Load(MY_SETTING_NAME).Items;
}
public override void SaveToSettings()
{
Save(MY_SETTING_NAME, this);
}
public List<DemoTypeItem> Items { get; set; }
}
public class DemoTypeItem
{
public String Name { get; set; }
public Int32 Id { get; set; }
public Boolean IsActive { get; set; }
}
The above "DemoType" class implements the "LoadFromSettings" method in order to read the serialised XML from user setting property "DemoTypeSettingName" and the "SaveToSettings" to write the XML back in to the user settings property.
The User Settings must contain a writable string property entitled "DemoTypeSettingName" in order to work.
Here, a simple console app creates a class instance, populates it from the settings and pushes the changes back to the settings file. Finally, the settings are saved.
class Program
{
static void Main(string[] args)
{
var test = new DemoType();
test.LoadFromSettings();
if (test.Items == null)
{
test.Items = new List(
new[]
{
new DemoTypeItem {Id = 1, IsActive = true, Name = "Name 1"},
new DemoTypeItem {Id = 2, IsActive = true, Name = "Name 2"},
new DemoTypeItem {Id = 3, IsActive = true, Name = "Name 3"},
new DemoTypeItem {Id = 4, IsActive = true, Name = "Name 4"},
new DemoTypeItem {Id = 5, IsActive = true, Name = "Name 5"}
}
);
}
test.SaveToSettings();
Properties.Settings.Default.Save();
}
}
If you don't like Xml Serialisation (creating the serialiser does have an overhead) or if you want to keep your user defined type internal, you could use this pattern with binary serialisation.
Well that's it for now. Next time - The Dependency Resolver pattern!