Controls serialization with lambda expressions and IoC

To use our serializer, you just need to derive from the generic class BaseControlSerializer :

public class TextBoxControlSerializer : BaseControlSerializer<TextBox>
{
	public TextBoxControlSerializer(IContainer container) 
		: base(container)
	{
	}

	protected override void Map()
	{
		Map(c => c.Text);
		Map(c => c.Font);
	}
}

Then you have to initialize the IoC Containter :

private IContainer InitContainer()
{
	var container = new Container();

	RegisterDefaultTypeConverters(container);
	RegisterDefaulControlConverters(container);

	return container;
}

////Register here your serializers for each control type
private void RegisterDefaulControlConverters(Container container)
{
	container.Register<TextBox>(new TextBoxControlSerializer(container));
	container.Register<CheckBox>(new CheckBoxControlSerializer(container));
}

////Register here your type converters
private void RegisterDefaultTypeConverters(IContainer container)
{
	container.Register<string>(new StringConverter());
	container.Register<bool>(new BooleanConverter());
	container.Register<Font>(new FontConverter());
}

And just use it!

var xmlFormSerializer = new XmlFormSerializer(InitContainer());

////When the application starts, for example the OnLoad form method
////Control could be your form or another control on it
xmlFormSerializer.Save(control, fileName);

////Before exit from the applicatoin, for example the OnClosing form method
xmlFormSerializer.Save(control, fileName);

Last edited Jun 21, 2012 at 11:08 AM by teoarch, version 2

Comments

No comments yet.