PageRenderTime 101ms CodeModel.GetById 22ms RepoModel.GetById 4ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Providers/DbConnectionHelper.cs

#
C# | 199 lines | 111 code | 29 blank | 59 comment | 9 complexity | fa48cd681908e1bfcdf12038adb43fc4 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Configuration.Provider;
  5. using System.Data;
  6. using System.Data.Common;
  7. using System.Linq;
  8. using System.Text;
  9. namespace BlogEngine.Core.Providers
  10. {
  11. /// <summary>
  12. /// Helper class for working with DbConnections.
  13. /// </summary>
  14. /// <remarks>
  15. ///
  16. /// This class is meant to reduce the amount of repeated code in database provider classes by pulling all the common actions
  17. /// into one spot. This should remove the many repetitive null checks on connections/parameters.
  18. ///
  19. /// This class handles the creation of DbConnection, setting its connection string, and opening the connection if possible.
  20. ///
  21. /// Usage is simple:
  22. /// using(var helper = new ConnectionHelper(provider)) {
  23. /// if (helper.HasConnection) {
  24. /// // do stuff
  25. /// }
  26. /// }
  27. ///
  28. /// Note: This class throws a NullReferenceException if its Provider.CreateParameter() method returns a null object.
  29. /// All of the methods in the DbBlogProvider class require parameterized queries, and previously each parameter
  30. /// created was being checked for null before proceeding. It's better to fail fast in this instance, to help creaters
  31. /// of custom implementations figure out what's wrong.
  32. ///
  33. /// </remarks>
  34. public sealed class DbConnectionHelper : IDisposable
  35. {
  36. #region "Constructors"
  37. /// <summary>
  38. /// Creates a new DbConnectionHelper instance from the given ConnectionStringSettings.
  39. /// </summary>
  40. /// <param name="settings"></param>
  41. public DbConnectionHelper(ConnectionStringSettings settings) : this(settings.ProviderName, settings.ConnectionString)
  42. {
  43. }
  44. /// <summary>
  45. /// Creates a new DbConnectionHelper instance from the given provider name and database connection string..
  46. /// </summary>
  47. /// <param name="providerName"></param>
  48. /// <param name="connectionString"></param>
  49. public DbConnectionHelper(string providerName, string connectionString)
  50. {
  51. this._dbProvFactory = DbProviderFactories.GetFactory(providerName);
  52. this._connection = this._dbProvFactory.CreateConnection();
  53. this._hasConnection = (this._connection != null);
  54. if (this._hasConnection)
  55. {
  56. this._connection.ConnectionString = connectionString;
  57. this._connection.Open();
  58. }
  59. }
  60. #endregion
  61. #region "Properties"
  62. /// <summary>
  63. /// Returns the DbConnection of this instance.
  64. /// </summary>
  65. public DbConnection Connection
  66. {
  67. get { return this._connection; }
  68. }
  69. private DbConnection _connection;
  70. /// <summary>
  71. /// Gets whether the Connection of this ConnectionHelper instance is null.
  72. /// </summary>
  73. public bool HasConnection
  74. {
  75. get { return this._hasConnection; }
  76. }
  77. private bool _hasConnection;
  78. /// <summary>
  79. /// Gets the DbProviderFactory used by this ConnectionHelper instance.
  80. /// </summary>
  81. public DbProviderFactory Provider
  82. {
  83. get { return this._dbProvFactory; }
  84. }
  85. private DbProviderFactory _dbProvFactory;
  86. #endregion
  87. #region "Methods"
  88. private void CheckDisposed()
  89. {
  90. if (this.isDisposed)
  91. {
  92. throw new ObjectDisposedException("ConnectionHelper");
  93. }
  94. }
  95. /// <summary>
  96. /// Uses this ConnectionHelper instance's connection to create and return a new DbCommand instance.
  97. /// </summary>
  98. /// <returns></returns>
  99. public DbCommand CreateCommand()
  100. {
  101. this.CheckDisposed();
  102. return this.Connection.CreateCommand();
  103. }
  104. /// <summary>
  105. /// Users this ConnectionHelper instance's connection to create and return a new DbCommand with the given command text. CommandType is automatically set to CommandType.Text.
  106. /// </summary>
  107. /// <param name="commandText"></param>
  108. /// <returns></returns>
  109. public DbCommand CreateTextCommand(string commandText)
  110. {
  111. var command = this.CreateCommand();
  112. command.CommandText = commandText;
  113. command.CommandType = CommandType.Text;
  114. return command;
  115. }
  116. /// <summary>
  117. /// Uses this ConnectionHelper's Provider to create a DbParameter instance with the given parameter name and value.
  118. /// </summary>
  119. /// <param name="parameterName">The name of the parameter.</param>
  120. /// <param name="value">The value of the parameter.</param>
  121. /// <returns></returns>
  122. public DbParameter CreateParameter(string parameterName, object value)
  123. {
  124. this.CheckDisposed();
  125. var param = this.Provider.CreateParameter();
  126. if (param == null)
  127. {
  128. throw new NullReferenceException("DbBlogProvider");
  129. }
  130. else
  131. {
  132. param.ParameterName = parameterName;
  133. param.Value = value;
  134. return param;
  135. }
  136. }
  137. #endregion
  138. #region "IDisposable"
  139. private bool isDisposed;
  140. private void Dispose(bool disposing)
  141. {
  142. try
  143. {
  144. if (!this.isDisposed && disposing)
  145. {
  146. if (this._connection != null)
  147. {
  148. this._connection.Dispose();
  149. }
  150. }
  151. }
  152. finally
  153. {
  154. this._dbProvFactory = null;
  155. this._connection = null;
  156. this._hasConnection = false;
  157. this.isDisposed = true;
  158. }
  159. }
  160. /// <summary>
  161. /// Disposes this DbConnectionHelper and its underlying connection.
  162. /// </summary>
  163. public void Dispose()
  164. {
  165. this.Dispose(true);
  166. GC.SuppressFinalize(this);
  167. }
  168. #endregion
  169. }
  170. }