code_junkie

Can a Generic Method handle both Reference and Nullable Value types?

Nov 12th, 2011
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. public static int? GetNullableInt32(this IDataRecord dr, int ordinal)
  2. {
  3. int? nullInt = null;
  4. return dr.IsDBNull(ordinal) ? nullInt : dr.GetInt32(ordinal);
  5. }
  6.  
  7. public static int? GetNullableInt32(this IDataRecord dr, string fieldname)
  8. {
  9. int ordinal = dr.GetOrdinal(fieldname);
  10. return dr.GetNullableInt32(ordinal);
  11. }
  12.  
  13. public static Nullable<T> GetNullable<T>(this IDataRecord dr, int ordinal)
  14. {
  15. Nullable<T> nullValue = null;
  16. return dr.IsDBNull(ordinal) ? nullValue : (Nullable<T>) dr.GetValue(ordinal);
  17. }
  18.  
  19. public static T GetNullable<T>(this IDataRecord dr, int ordinal)
  20. {
  21. return dr.IsDBNull(ordinal) ? default(T) : (T) dr.GetValue(ordinal);
  22. }
  23.  
  24. public static T Get<T>( this IDataRecord dr, int ordinal)
  25. {
  26. T nullValue = default(T);
  27. return dr.IsDBNull(ordinal) ? nullValue : (T) dr.GetValue(ordinal);
  28. }
  29.  
  30.  
  31. public void Code(params string[] args)
  32. {
  33. IDataRecord dr= null;
  34. int? a = Get<int?>(dr, 1);
  35. string b = Get<string>(dr, 2);
  36. }
  37.  
  38. private void Test()
  39. {
  40. int? iTestA;
  41. int? iTestB;
  42. int iTestC;
  43. string sTestA;
  44. string sTestB;
  45.  
  46. //Create connection
  47. using (SqlConnection oConnection = new SqlConnection(@""))
  48. {
  49. //Open connection
  50. oConnection.Open();
  51.  
  52. //Create command
  53. using (SqlCommand oCommand = oConnection.CreateCommand())
  54. {
  55. //Set command text
  56. oCommand.CommandText = "SELECT null, 1, null, null, '1'";
  57.  
  58. //Create reader
  59. using (SqlDataReader oReader = oCommand.ExecuteReader())
  60. {
  61. //Read the data
  62. oReader.Read();
  63.  
  64. //Set the values
  65. iTestA = oReader[0] as int?;
  66. iTestB = oReader[1] as int?;
  67. iTestC = oReader[2] as int? ?? -1;
  68. sTestA = oReader[3] as string;
  69. sTestB = oReader[4] as string;
  70. }
  71. }
  72. }
  73. }
  74.  
  75. public static T GetData<T>(this IDataReader reader, Func<int, T> getFunc, int index)
  76. {
  77. if (!reader.IsClosed)
  78. {
  79. return getFunc(index);
  80. }
  81. throw new ArgumentException("Reader is closed.", "reader");
  82. }
  83.  
  84. public static T GetDataNullableRef<T>(this IDataReader reader, Func<int, T> getFunc, int index) where T : class
  85. {
  86. if (!reader.IsClosed)
  87. {
  88. return reader.IsDBNull(index) ? null : getFunc(index);
  89. }
  90. throw new ArgumentException("Reader is closed.", "reader");
  91. }
  92.  
  93. public static T? GetDataNullableValue<T>(this IDataReader reader, Func<int, T> getFunc, int index) where T : struct
  94. {
  95. if (!reader.IsClosed)
  96. {
  97. return reader.IsDBNull(index) ? (T?)null : getFunc(index);
  98. }
  99. throw new ArgumentException("Reader is closed.", "reader");
  100. }
  101.  
  102. private static Whatever CreateObject(IDataReader reader)
  103. {
  104. Int32? id = reader.GetDataNullableValue<Int32>(reader.GetInt32, 0);
  105. string name = reader.GetDataNullableRef<string>(reader.GetString, 1);
  106. Int32 x = reader.GetData<Int32>(reader.GetInt32, 2);
  107. }
  108.  
  109. public static T Get<T>(this IDataRecord rec, Func<int, T> GetValue, int ordinal)
  110. {
  111. return rec.IsDBNull(ordinal) ? default(T) : GetValue(ordinal);
  112. }
  113.  
  114. public static T Get<T>(this IDataRecord rec, Func<IDataRecord, int, T> GetValue, int ordinal)
  115. {
  116. return rec.IsDBNull(ordinal) ? default(T) : GetValue(rec, ordinal);
  117. }
  118.  
  119. public static Func<IDataRecord, int, int> GetInt32 = (rec, i) => rec.GetInt32(i);
  120. public static Func<IDataRecord, int, bool> GetBool = (rec, i) => rec.GetBoolean(i);
  121. public static Func<IDataRecord, int, string> GetString = (rec, i) => rec.GetString(i);
  122.  
  123. rec.Get(GetString, index);
  124. rec.Get(GetInt32, index);
  125.  
  126. DataRow record = GetSomeRecord();
  127. int? someNumber = record[15] as int?
  128. Guid? someUID = record["MyPrimaryKey"] as Guid?;
  129. string someText = GetSomeText();
  130. record["Description"] = someText.ToDbString();
  131.  
  132. // ........
  133.  
  134. public static class StringExtensionHelper {
  135. public static object ToDbString( this string text ) {
  136. object ret = null != text ? text : DBNull.Value
  137. return ret;
  138. }
  139. }
  140.  
  141. public static class StringExtensionHelper {
  142. public static object ToDbValue( this object value ) {
  143. object ret = object.ReferenceEquals( value, null ) ? (object)DBNull.Value : value;
  144. return ret;
  145. }
  146. }
Add Comment
Please, Sign In to add comment