Thursday, 2 February 2012

Deserializing XML to Dynamic Object in C#

Little bit background about this blog entry – most of the applications today are driven by (BIG) settings file (usually in XML format) , this is absolutely fine and will work as expected. Let’s look at a real world scenario of XML driven applications – suppose user wants an extra feature which is controlled based on an entry in the XML settings file that means we have to handle that extra settings parameter in our application.

Not a BIG deal; we could do it in following ways
#1) if you are using a custom XML Settings parser then change it (add XPATH/read node attribute etc.) - Old fashion approach :-(.
Or
#2) Create a new XML schema from XML and use XSD.exe to generate classes from the schema (if it simple change like adding attribute etc. then you can modify the auto generated class file directly ) ;And deserialize the XML to an object and use this object everywhere.
Or
#3) LinqToXML
etc...

Since I came from strong OOPS background I personally prefer to use everything as objects, so obviously my choice is second one (of course I won’t generate class file from schema for every change!!! )
These patterns are good still I was looking something DYNAMIC until DotNet 4.0 released with ‘dynamic’ type support.

Let’s look at an example with ‘Dynamic’ type(these are samples so keeping it simple as possible :-) to explain the idea)

In version 1.0 of settings (config) file [application will read this settings file and set the button text – simple yaaaa ],

<?xml version="1.0" encoding="utf-8" ?>
<UISettings >
<Button Text="Show Data" />
</UISettings>


//Code snippet
dynamic settingObject = SomeXmlToDynamicObjectParser.Parse(“Settings.xml”);
Then real beauty,
MyForm.btnData.Text = settingObject.Button.Text;

Now in version 2.0 of settings ,suppose we have added one more button setting (i.e. width)

<? version="1.0" encoding="utf-8" ?>
<?UISettings>
<Button Text="Show Data" Width="50" />
</UISettings>


//Code snippet
dynamic settingObject = SomeXmlToDynamicObjectParser.Parse(“Settings.xml”);
MyForm.btnData.Text = settingObject.Button.Text;
MyForm.btnData.Width = double.Parse(settingObject.Button.Width); (So simple yaaa!!!!)

I started to love this pattern and applied in many BIG xml driven production applications.(This is just my view if you have a different opinion or better approach please share in comment section).

You can find the implementation of ‘SomeXmlToDynamicObjectParserHere.

Happy Coding Days ….

Friday, 8 July 2011

Abstract Vs Interface - Good read.

Everybody knows what are the points discussed here but still worth reading it as most of the time we forget basics since we are living in a abstract world of coding.

Saturday, 18 June 2011

C# - Singleton Class using Lazy <T> in Dotnet .40

Code Snippet,

public sealed class Singleton
{
private static readonly Lazy lazy =
new Lazy(() => new Singleton());

public static Singleton Instance { get { return lazy.Value; } }

private Singleton()
{
}
}

Reference:-http://csharpindepth.com/Articles/General/Singleton.aspx

Tuesday, 8 December 2009

Simple way to pass numeric array from c# to matlab.

This is a follow up screencast on Matlab Neural Network in C# Application (http://baijumax.blogspot.com/2009/05/matlab-neural-network-in-c-application.html),users often face issues with passing array from c# to matlab and based on their feedback I have decided to prepare this
screen cast in which I have shown simple solution to get around with syntactical issues of passing numeric array from c# to matlab by using simple string array.

Sunday, 1 November 2009

Creating simple WPF Multi-Thread application.

This videos shows how to create simple wpf -thread application,gives an overview of how to use "Dispatcher" object in WPF.




Below extension method will allow us to update any controls in thread safe manner.

public static class ControlExtension
{
public static void UpdateControlSafe(this Control control, Action code)
{
if (!control.Dispatcher.CheckAccess())
control.Dispatcher.BeginInvoke(code);
else
code.Invoke();
}
}

Monday, 29 June 2009

Generic InvokeRequired pattern - (C# Multi-Threading)

Tody I read article on InokeRequired patterns posted in code project ( Read Full Article )

I have wrote more generic version of the same ( Less code !!!!! ...clear & crisp...)

public static class ControlExtension
{
public static void UpdateControl(this Control control, MethodInvoker code)
{
if (control.InvokeRequired) {
control.Invoke(code);
}
else{
code.Invoke();
}
}

using 'Control' type param in Extension mentod give us an advantage to call UpdateControl on any control.

for eg:- In the below example I have called 'UpdateControl' extension method on textbox control.

private void btnUpdate_Click(object sender, EventArgs e)
{
Thread th = new Thread(new ThreadStart( () => txtInfo.UpdateControl(Display) ) );
th.Start();
}

private void Display()
{
txtInfo.Text += "Updated";
}