From 89b43836d07acf4dcbd81938a7da5d0b990a2567 Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Mon, 15 Jul 2013 17:46:41 -0700 Subject: [PATCH] minor cleanup. --- Marr.Data/DataMapper.cs | 82 ++++++------------- NzbDrone.Common/PathExtensions.cs | 8 +- .../Notifications/Email/EmailService.cs | 2 +- 3 files changed, 31 insertions(+), 61 deletions(-) diff --git a/Marr.Data/DataMapper.cs b/Marr.Data/DataMapper.cs index d35194f1f..49ebd0125 100644 --- a/Marr.Data/DataMapper.cs +++ b/Marr.Data/DataMapper.cs @@ -18,12 +18,9 @@ using System.Collections.Generic; using System.Text; using System.Data; using System.Data.Common; -using System.Data.SqlClient; using System.Reflection; using System.Collections; -using System.Linq; using Marr.Data.Mapping; -using Marr.Data.Converters; using Marr.Data.Parameters; using Marr.Data.QGen; using System.Linq.Expressions; @@ -39,8 +36,6 @@ namespace Marr.Data #region - Contructor, Members - - private DbProviderFactory _dbProviderFactory; - private string _connectionString; private DbCommand _command; /// @@ -55,43 +50,32 @@ namespace Marr.Data /// /// A database provider agnostic initialization. /// - /// The database connection string. + /// The database connection string. public DataMapper(DbProviderFactory dbProviderFactory, string connectionString) { + SqlMode = SqlModes.StoredProcedure; if (dbProviderFactory == null) - throw new ArgumentNullException("dbProviderFactory instance cannot be null."); + throw new ArgumentNullException("dbProviderFactory"); if (string.IsNullOrEmpty(connectionString)) - throw new ArgumentNullException("connectionString cannot be null or empty."); + throw new ArgumentNullException("connectionString"); - _dbProviderFactory = dbProviderFactory; + ProviderFactory = dbProviderFactory; - _connectionString = connectionString; + ConnectionString = connectionString; } - public string ConnectionString - { - get - { - return _connectionString; - } - } + public string ConnectionString { get; private set; } + + public DbProviderFactory ProviderFactory { get; private set; } - public DbProviderFactory ProviderFactory - { - get - { - return _dbProviderFactory; - } - } - /// /// Creates a new command utilizing the connection string. /// private DbCommand CreateNewCommand() { - DbConnection conn = _dbProviderFactory.CreateConnection(); - conn.ConnectionString = _connectionString; + DbConnection conn = ProviderFactory.CreateConnection(); + conn.ConnectionString = ConnectionString; DbCommand cmd = conn.CreateCommand(); SetSqlMode(cmd); return cmd; @@ -155,23 +139,12 @@ namespace Marr.Data #region - SP / SQL Mode - - private SqlModes _sqlMode = SqlModes.StoredProcedure; // Defaults to SP. /// /// Gets or sets a value that determines whether the DataMapper will /// use a stored procedure or a sql text command to access /// the database. The default is stored procedure. /// - public SqlModes SqlMode - { - get - { - return _sqlMode; - } - set - { - _sqlMode = value; - } - } + public SqlModes SqlMode { get; set; } /// /// Sets the DbCommand objects CommandType to the current SqlMode. @@ -257,7 +230,7 @@ namespace Marr.Data { OpenConnection(); - List list = new List(); + var list = new List(); DbDataReader reader = null; try { @@ -290,8 +263,8 @@ namespace Marr.Data { if (string.IsNullOrEmpty(sql)) throw new ArgumentNullException("sql", "A SQL query or stored procedure name is required"); - else - Command.CommandText = sql; + + Command.CommandText = sql; try { @@ -334,7 +307,7 @@ namespace Marr.Data try { - using (DbDataAdapter adapter = _dbProviderFactory.CreateDataAdapter()) + using (DbDataAdapter adapter = ProviderFactory.CreateDataAdapter()) { Command.CommandText = sql; adapter.SelectCommand = Command; @@ -370,7 +343,7 @@ namespace Marr.Data try { - using (DbDataAdapter adapter = _dbProviderFactory.CreateDataAdapter()) + using (DbDataAdapter adapter = ProviderFactory.CreateDataAdapter()) { Command.CommandText = sql; adapter.SelectCommand = Command; @@ -404,7 +377,7 @@ namespace Marr.Data try { - adapter = _dbProviderFactory.CreateDataAdapter(); + adapter = ProviderFactory.CreateDataAdapter(); adapter.UpdateCommand = Command; adapter.UpdateCommand.CommandText = sql; @@ -437,7 +410,7 @@ namespace Marr.Data try { - adapter = _dbProviderFactory.CreateDataAdapter(); + adapter = ProviderFactory.CreateDataAdapter(); adapter.InsertCommand = Command; adapter.InsertCommand.CommandText = sql; @@ -467,7 +440,7 @@ namespace Marr.Data try { - adapter = _dbProviderFactory.CreateDataAdapter(); + adapter = ProviderFactory.CreateDataAdapter(); adapter.DeleteCommand = Command; adapter.DeleteCommand.CommandText = sql; @@ -620,7 +593,7 @@ namespace Marr.Data #endregion #region - Query to Graph - - + public List QueryToGraph(string sql) { return (List)QueryToGraph(sql, new List()); @@ -696,7 +669,7 @@ namespace Marr.Data public UpdateQueryBuilder Update() { - return new UpdateQueryBuilder(this); + return new UpdateQueryBuilder(this); } public int Update(T entity, Expression> filter) @@ -844,7 +817,7 @@ namespace Marr.Data } #endregion - + #region - Events - public event EventHandler OpeningConnection; @@ -894,7 +867,7 @@ namespace Marr.Data { if (MapRepository.Instance.EnableTraceLogging) { - StringBuilder sb = new StringBuilder(); + var sb = new StringBuilder(); sb.AppendLine(); sb.AppendLine("==== Begin Query Trace ===="); sb.AppendLine(); @@ -920,11 +893,8 @@ namespace Marr.Data private void UnbindEvents() { - if (OpeningConnection != null) - OpeningConnection = null; - - if (ClosingConnection != null) - ClosingConnection = null; + OpeningConnection = null; + ClosingConnection = null; } public void BeginTransaction() diff --git a/NzbDrone.Common/PathExtensions.cs b/NzbDrone.Common/PathExtensions.cs index 990c51927..2949111db 100644 --- a/NzbDrone.Common/PathExtensions.cs +++ b/NzbDrone.Common/PathExtensions.cs @@ -6,10 +6,10 @@ namespace NzbDrone.Common { public static class PathExtensions { - private static readonly string APP_CONFIG_FILE = "config.xml"; - private static readonly string NZBDRONE_DB = "nzbdrone.db"; - private static readonly string NZBDRONE_LOG_DB = "logs.db"; - private static readonly string BACKUP_ZIP_FILE = "NzbDrone_Backup.zip"; + private const string APP_CONFIG_FILE = "config.xml"; + private const string NZBDRONE_DB = "nzbdrone.db"; + private const string NZBDRONE_LOG_DB = "logs.db"; + private const string BACKUP_ZIP_FILE = "NzbDrone_Backup.zip"; private static readonly string UPDATE_SANDBOX_FOLDER_NAME = "nzbdrone_update" + Path.DirectorySeparatorChar; private static readonly string UPDATE_PACKAGE_FOLDER_NAME = "nzbdrone" + Path.DirectorySeparatorChar; diff --git a/NzbDrone.Core/Notifications/Email/EmailService.cs b/NzbDrone.Core/Notifications/Email/EmailService.cs index bd9b255f0..6b14a7e25 100644 --- a/NzbDrone.Core/Notifications/Email/EmailService.cs +++ b/NzbDrone.Core/Notifications/Email/EmailService.cs @@ -73,7 +73,7 @@ namespace NzbDrone.Core.Notifications.Email var settings = new EmailSettings(); settings.InjectFrom(message); - var body = "Success! You have properly configured your email notification settings"; + const string body = "Success! You have properly configured your email notification settings"; SendEmail(settings, "NzbDrone - Test Notification", body); }