Thursday, March 26, 2009

Because of my recent obsession to update my skills on Design (I think I am a decent programmer and with the design skills on my resume, it would be nice!), I created my first icon using Inkscape.

Ok – this isnt anyway close to the normal most stupid looking icons but hey I have an excuse – I am not a designer.

Anyway, as you work on Inkscape and try exporting your work as icon, I could not notice any export format as .ico. So here is what i did. I exported my work as a PNG (File->Export as Bitmap..) and then using http://www.convertico.com/ I converted my PNG to an icon!

Tuesday, March 17, 2009

In this post, I would like to document a couple of classes with several methods that are useful to convert from one type of data to another. For example, some of the methods can be used to convert a Dataset into JSON string and vice versa.

SQL Query output as XML

The first class to start with the most simplest one. This class can be used to wrap your SQL queries for Sql Server or Oracle such that they return XML output instead of rows. Strategy pattern has been used to implement these methods – nothing fancy.

The base class for query wrapper.

1:publicabstractclass QueryWrapper

2: {

3:publicabstractstring ForXmlOutput(string query);

4: }

Then one wrapper each for Sql and Oracle has been implemented as shown.

49:/// Opens the XML file mentioned in "fi" and return it as JSON String.

50:/// </summary>

51:/// <param name="fi"></param>

52:/// <returns></returns>

53:publicstaticstring GetJsonContent(this FileInfo fi)

54: {

55:if (File.Exists(fi.FullName))

56:return XDocument.Load(fi.FullName).AsJsonString();

57:else

58: {

59:returnnull;

60: }

61: }

62:

63:#endregion

64:

65:#region Json to XML

66:/// <summary>

67:/// Converts the json string into XML

68:/// </summary>

69:/// <param name="json">JSON as string</param>

70:/// <returns>JSON returned as XML</returns>

71:publicstaticstring AsXml(thisstring json)

72: {

73:return JavaScriptConvert.DeserializeXmlNode(json).InnerXml;

74: }

75:#endregion

76:

77:#region Object <-> JSON

78:publicstaticstring ToJSON<T>(this T obj)

79: {

80:return JavaScriptConvert.SerializeObject(obj);

81: }

82:

83:publicstatic T FromJSON<T>(thisstring json)

84: {

85:return JavaScriptConvert.DeserializeObject<T>(json);

86: }

87:#endregion

88:

89:#region Dataset <--> JSON

90:publicstaticstring ToJSON(this DataSet ds)

91: {

92:return ds.GetXml().AsJsonString();

93: }

94:

95:publicstatic DataSet ToDataset(thisstring jsonString, bool isXml)

96: {

97: var ds = new DataSet();

98:string asXml = string.Empty;

99:if (!isXml)

100: asXml = jsonString.AsXml();

101:else

102: {

103: asXml = jsonString;

104: }

105:using (var sr = new StringReader(asXml))

106: {

107: ds.ReadXml(sr);

108: }

109:return ds;

110: }

111:

112:#endregion

113:

114:#region Others....

115:/// <summary>

116:/// Computes the size of the byte array in Megabytes

117:/// </summary>

118:/// <param name="array">byte array to be computed</param>

119:/// <returns>size of the array in MB</returns>

120:publicstaticdouble SizeInMB(thisbyte[] array)

121: {

122:return array.Length / (1024 * 1024);

123: }

124:

125:/// <summary>

126:/// Converst the value into seconds. Basically it divides by 1000.

127:/// </summary>

128:/// <param name="val">Value in Milliseconds</param>

129:/// <returns>Value in seconds</returns>

130:publicstaticdouble TimeInSeconds(thisdouble val)

131: {

132:return val / (1000);

133: }

134:

135:#endregion

136: }

Test Data Helpers

In this set of helper classes, I wrote a couple of methods which wrap the most required functionality (at least for me) like generating a random string, getting a random integer, timing an execution. May or may not be useful to you all.

1:/// <summary>

2:/// Provides methods for timing execution of an action.

3:/// Also provides method to generate a byte[] array with all random values.

4:/// Other Random Helper methods.

5:/// </summary>

6:publicclass TestHelpers

7: {

8:privatestatic Random rand = new Random();

9:/// <summary>

10:/// This method executes the action passed and then returns the total time

11:/// taken in milliseconds using Stopwatch.

12:/// </summary>

13:/// <param name="action">the action to be performed</param>

14:/// <returns>time elapsed in milliseconds</returns>

15:publicstaticdouble TimeAndExecute(Action action, int repeatCount)

16: {

17:if (repeatCount == 0) return 0;

18:

19: Stopwatch sw = Stopwatch.StartNew();

20:for (int i = 0; i < repeatCount; i++)

21: action();

22: sw.Stop();

23:

24:return sw.ElapsedMilliseconds / repeatCount;

25: }

26:

27:/// <summary>

28:/// Returns a random byte array of the specified size.

29:/// </summary>

30:/// <param name="size"></param>

31:/// <returns></returns>

32:publicstaticbyte[] GetBytes(int size)

33: {

34: var b = newbyte[size];

35: var rand = new Random(size);

36: rand.NextBytes(b);

37:return b;

38: }

39:

40:publicstaticint NextIntRand()

41: {

42:return rand.Next();

43: }

44:

45:/// <summary>

46:/// returns a randomly generated string.

47:/// </summary>

48:/// <param name="length">length of the string to be returned</param>

49:/// <returns>string generated</returns>

50:publicstaticstring GetRandomString(int length)

51: {

52:return Encoding.Default.GetString(GetBytes(length));

53: }

54: }

Simplified Xml querying using Extension Methods.

The following method returns all the elements in the given document (XDocument) with the passed ElementName and which have the specified attribute name and value. I use it a lot for automating my XML documents manipulation.