Saturday, November 21, 2009

How to keep track of what you learnt?


This is a nice article that resonated with the method that I use to learn. I use the notepad approach, I keep a diary/notepad and write what I learn and it serves dual purposes, as an index for my learnings (so that I can go back and quickly reference) and the act of writing it makes me retain it longer.

http://www.freestylemind.com/how-to-keep-track-of-what-youve-learnt/

Thursday, November 5, 2009

.NET - Serialization to XML and back - Saving/reading custom configuration files

XML serialization in the .net framework is a powerful and very useful technology, yet I wasn't too familiar with it, so I felt I should blog about it. One of the situation where I think it is very appropriate to use this is to create custom configurations and read/save/modify them for web (server) or desktop applications. Using the asp.net Cache class it is also possible to assign the serialized file as a cache dependency thereby re-read and re-hydrate the serialized object from the xml file. This is a perfect scenario for xml configuration files. The below links are references and the code is from "switch on the code c# xml serialization tutorial", link shown below.
STEP 1: Create a simple .net class (Plain old .NET object, PONO), for example:

public class Movie
{
  public string Name
  { get; set; }

  public int Rating
  { get; set; }

  public DateTime ReleaseDate
  { get; set; }
}


STEP 2: Serialize it to Xml using the XmlSerializer class:


static public void SerializeToXML(Movie movie)
{
  XmlSerializer serializer = new XmlSerializer(typeof(Movie));
  TextWriter textWriter = new StreamWriter(@"C:\movie.xml");
  serializer.Serialize(textWriter, movie);
  textWriter.Close();
}

It would produce an XML file, named movie.xml, as:
<?xml version="1.0" encoding="utf-8"?>
<Movie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Title>Starship Troopers</Title>
  <Rating>6.9</Rating>
  <ReleaseDate>1997-11-07T00:00:00</ReleaseDate>
</Movie>




STEP 3: Modify the Xml file and deserialize (update and re-hydrate) the .net object from the xml file:

static Movie DeserializeFromXML()
{
   XmlSerializer deserializer = new XmlSerializer(typeof(Movie));
   TextReader textReader = new StreamReader(@"C:\movie.xml");
   Movie movie = (Movie)deserializer.Deserialize(textReader);
   textReader.Close();

   return movie;
}


Wednesday, November 4, 2009

Flex: Export chart / graph or some other component to image (save as image)

This was a non trivial issue prior to Flex 3 - most people had approached this using a mix of client and server side technologies, even using javascript as well. But most of the solutions were not simple or elegant, this is where Flex3 came to the rescue with the ImageSnapshot class. Doug McCune created an interesting approach using javascript and serializing image bytes to the browser address bar, although not simple, it is creative and that dynamic chart is great.

Today it is a very simple thing to do. It is a two fold problem with a very easy implementation thanks to the awesome flex framework.Graham Odds wrote a great article on how to do this and I'm elaborating on his article.
  1. Save the UIComponent as an image (you can specify the image format) using the ImageSnapshot class
  2. Send the image to be saved to the user's computer (using the FileReference class from Flash 10)
All UIComponents in flex extend DisplayObject which can be exported to an image, e.g. png,, jpg, etc using the ImageSnapshot class. Currently there are two classes that can take BitMapData or ByteArray  and convert them to an image format, class that implement interface IImageEncoder allow this. The image encoders are: JPEGEncoder and PNGEncoder. Once it is exported to an image, the byte data can now be transferred to the user's computer and saved as a file.This is done using the FileReference class introduced in Flash 10.

Below is the code snippet to do this (source) :
 
/**
 * Attempts to save the chart to the user's file-system.
 */
private function saveChart():void
{
    var image:ImageSnapshot = ImageSnapshot.captureImage(myChart, 300, new PNGEncoder());
    var file:FileReference = new FileReference();
    file.save(image.data, "chart.png");
}

Saturday, October 31, 2009

Transform any flex component with resizing, dragging with handles

Try it out yourself:



This is called "Flex Object Handles" available from Marc Hughes. This project is available on google code and has excellent tutorials, documentation, forum activity and support. This library supports using a custom container for flex components to enable thme to be resizable, draggable, moveable  and provides handles etc.

Another example:

Wednesday, October 28, 2009

Interesting article on how a plane ticket can be free

In europe there are plane tickets that costs around $20. How is this possible?
The article below explains this.