C# Class BlogEngine.Core.Providers.DbConnectionHelper

Helper class for working with DbConnections.
This class is meant to reduce the amount of repeated code in database provider classes by pulling all the common actions into one spot. This should remove the many repetitive null checks on connections/parameters. This class handles the creation of DbConnection, setting its connection string, and opening the connection if possible. Usage is simple: using(var helper = new ConnectionHelper(provider)) { if (helper.HasConnection) { // do stuff } } Note: This class throws a NullReferenceException if its Provider.CreateParameter() method returns a null object. All of the methods in the DbBlogProvider class require parameterized queries, and previously each parameter created was being checked for null before proceeding. It's better to fail fast in this instance, to help creaters of custom implementations figure out what's wrong.
Inheritance: IDisposable
Show file Open project: rasmuskl/ReSharperCourse Class Usage Examples

Public Methods

Method Description
CreateCommand ( ) : DbCommand

Uses this ConnectionHelper instance's connection to create and return a new DbCommand instance.

CreateParameter ( string parameterName, object value ) : DbParameter

Uses this ConnectionHelper's Provider to create a DbParameter instance with the given parameter name and value.

CreateParameter ( string parameterName, object value, DbType dbType ) : DbParameter

Uses this ConnectionHelper's Provider to create a DbParameter instance with the given parameter name and value.

CreateParameter ( string parameterName, object value, DbType dbType, int size ) : DbParameter

Uses this ConnectionHelper's Provider to create a DbParameter instance with the given parameter name and value.

CreateTextCommand ( string commandText ) : DbCommand

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.

DbConnectionHelper ( ConnectionStringSettings settings ) : System

Creates a new DbConnectionHelper instance from the given ConnectionStringSettings.

DbConnectionHelper ( string providerName, string connectionString ) : System

Creates a new DbConnectionHelper instance from the given provider name and database connection string..

Dispose ( ) : void

Disposes this DbConnectionHelper and its underlying connection.

Private Methods

Method Description
CheckDisposed ( ) : void
Dispose ( bool disposing ) : void

Method Details

CreateCommand() public method

Uses this ConnectionHelper instance's connection to create and return a new DbCommand instance.
public CreateCommand ( ) : DbCommand
return System.Data.Common.DbCommand

CreateParameter() public method

Uses this ConnectionHelper's Provider to create a DbParameter instance with the given parameter name and value.
public CreateParameter ( string parameterName, object value ) : DbParameter
parameterName string The name of the parameter.
value object The value of the parameter.
return System.Data.Common.DbParameter

CreateParameter() public method

Uses this ConnectionHelper's Provider to create a DbParameter instance with the given parameter name and value.
public CreateParameter ( string parameterName, object value, DbType dbType ) : DbParameter
parameterName string The name of the parameter.
value object The value of the parameter.
dbType DbType The DB type.
return System.Data.Common.DbParameter

CreateParameter() public method

Uses this ConnectionHelper's Provider to create a DbParameter instance with the given parameter name and value.
public CreateParameter ( string parameterName, object value, DbType dbType, int size ) : DbParameter
parameterName string The name of the parameter.
value object The value of the parameter.
dbType DbType The DB type.
size int The size/length of the parameter.
return System.Data.Common.DbParameter

CreateTextCommand() public method

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.
public CreateTextCommand ( string commandText ) : DbCommand
commandText string
return System.Data.Common.DbCommand

DbConnectionHelper() public method

Creates a new DbConnectionHelper instance from the given ConnectionStringSettings.
public DbConnectionHelper ( ConnectionStringSettings settings ) : System
settings System.Configuration.ConnectionStringSettings
return System

DbConnectionHelper() public method

Creates a new DbConnectionHelper instance from the given provider name and database connection string..
public DbConnectionHelper ( string providerName, string connectionString ) : System
providerName string
connectionString string
return System

Dispose() public method

Disposes this DbConnectionHelper and its underlying connection.
public Dispose ( ) : void
return void