From 9a24268ee7681eab8d8536e2579787fe113f5c87 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Fri, 26 Jul 2013 22:02:25 -0700 Subject: [PATCH] removed code redundancies. --- Exceptron.Client/fastJSON/JSON.cs | 74 ++++++++----------- Marr.Data/Converters/BooleanIntConverter.cs | 16 ++-- Marr.Data/Converters/BooleanYNConverter.cs | 16 ++-- Marr.Data/Converters/EnumIntConverter.cs | 6 +- Marr.Data/Converters/EnumStringConverter.cs | 6 +- Marr.Data/DataHelper.cs | 5 +- Marr.Data/DataMapper.cs | 9 +-- Marr.Data/MapRepository.cs | 18 ++--- Marr.Data/Mapping/ColumnAttribute.cs | 5 +- Marr.Data/Mapping/ColumnInfo.cs | 5 +- Marr.Data/Mapping/ColumnMapBuilder.cs | 5 +- Marr.Data/Mapping/RelationshipBuilder.cs | 5 +- .../Strategies/ReflectionMapStrategyBase.cs | 5 +- Marr.Data/Parameters/DbTypeBuilder.cs | 25 +++---- Marr.Data/Parameters/OleDbTypeBuilder.cs | 23 +++--- Marr.Data/Parameters/SqlDbTypeBuilder.cs | 25 +++---- Marr.Data/QGen/PagingQueryDecorator.cs | 5 +- Marr.Data/QGen/RowCountQueryDecorator.cs | 5 +- Marr.Data/QGen/SelectQuery.cs | 5 +- Marr.Data/QGen/SqlitePagingQueryDecorator.cs | 5 +- Marr.Data/QGen/WhereBuilder.cs | 5 +- Marr.Data/Reflection/ReflectionHelper.cs | 7 +- NzbDrone.Common/PathExtensions.cs | 2 +- .../Notifications/Xbmc/JsonApiProvider.cs | 7 +- 24 files changed, 104 insertions(+), 185 deletions(-) diff --git a/Exceptron.Client/fastJSON/JSON.cs b/Exceptron.Client/fastJSON/JSON.cs index 45ad1210a..4aaa4dd2c 100644 --- a/Exceptron.Client/fastJSON/JSON.cs +++ b/Exceptron.Client/fastJSON/JSON.cs @@ -93,12 +93,9 @@ internal string GetTypeAssemblyName(Type t) string val = ""; if (_tyname.TryGetValue(t, out val)) return val; - else - { - string s = t.AssemblyQualifiedName; - _tyname.Add(t, s); - return s; - } + string s = t.AssemblyQualifiedName; + _tyname.Add(t, s); + return s; } readonly SafeDictionary _typecache = new SafeDictionary(); @@ -107,12 +104,9 @@ private Type GetTypeFromCache(string typename) Type val = null; if (_typecache.TryGetValue(typename, out val)) return val; - else - { - Type t = Type.GetType(typename); - _typecache.Add(typename, t); - return t; - } + Type t = Type.GetType(typename); + _typecache.Add(typename, t); + return t; } readonly SafeDictionary _constrcache = new SafeDictionary(); @@ -126,17 +120,14 @@ private object FastCreateInstance(Type objtype) { return c(); } - else - { - DynamicMethod dynMethod = new DynamicMethod("_", objtype, null, true); - ILGenerator ilGen = dynMethod.GetILGenerator(); + DynamicMethod dynMethod = new DynamicMethod("_", objtype, null, true); + ILGenerator ilGen = dynMethod.GetILGenerator(); - ilGen.Emit(OpCodes.Newobj, objtype.GetConstructor(Type.EmptyTypes)); - ilGen.Emit(OpCodes.Ret); - c = (CreateObject)dynMethod.CreateDelegate(typeof(CreateObject)); - _constrcache.Add(objtype, c); - return c(); - } + ilGen.Emit(OpCodes.Newobj, objtype.GetConstructor(Type.EmptyTypes)); + ilGen.Emit(OpCodes.Ret); + c = (CreateObject)dynMethod.CreateDelegate(typeof(CreateObject)); + _constrcache.Add(objtype, c); + return c(); } catch (Exception exc) { @@ -188,22 +179,19 @@ private SafeDictionary Getproperties(Type type, string typen { return sd; } - else + sd = new SafeDictionary(); + var pr = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); + foreach (var p in pr) { - sd = new SafeDictionary(); - var pr = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); - foreach (var p in pr) - { - myPropInfo d = CreateMyProp(p.PropertyType, p.Name); - d.CanWrite = p.CanWrite; - d.setter = CreateSetMethod(p); - d.getter = CreateGetMethod(p); - sd.Add(p.Name, d); - } - - _propertycache.Add(typename, sd); - return sd; + myPropInfo d = CreateMyProp(p.PropertyType, p.Name); + d.CanWrite = p.CanWrite; + d.setter = CreateSetMethod(p); + d.getter = CreateGetMethod(p); + sd.Add(p.Name, d); } + + _propertycache.Add(typename, sd); + return sd; } private myPropInfo CreateMyProp(Type t, string name) @@ -342,16 +330,16 @@ private object ChangeType(object value, Type conversionType) if (conversionType == typeof(int)) return (int)CreateLong((string)value); - else if (conversionType == typeof(long)) + if (conversionType == typeof(long)) return CreateLong((string)value); - else if (conversionType == typeof(string)) + if (conversionType == typeof(string)) return value; - else if (conversionType == typeof(Guid)) + if (conversionType == typeof(Guid)) return CreateGuid((string)value); - else if (conversionType.IsEnum) + if (conversionType.IsEnum) return CreateEnum(conversionType, (string)value); return Convert.ChangeType(value, conversionType, CultureInfo.InvariantCulture); @@ -550,8 +538,7 @@ private Guid CreateGuid(string s) { if (s.Length > 30) return new Guid(s); - else - return new Guid(Convert.FromBase64String(s)); + return new Guid(Convert.FromBase64String(s)); } private DateTime CreateDateTime(string value) @@ -571,8 +558,7 @@ private DateTime CreateDateTime(string value) if (UseUTCDateTime == false && utc == false) return new DateTime(year, month, day, hour, min, sec); - else - return new DateTime(year, month, day, hour, min, sec, DateTimeKind.Utc).ToLocalTime(); + return new DateTime(year, month, day, hour, min, sec, DateTimeKind.Utc).ToLocalTime(); } #if SILVERLIGHT diff --git a/Marr.Data/Converters/BooleanIntConverter.cs b/Marr.Data/Converters/BooleanIntConverter.cs index cbd86f7e0..9dc9b41d3 100644 --- a/Marr.Data/Converters/BooleanIntConverter.cs +++ b/Marr.Data/Converters/BooleanIntConverter.cs @@ -33,17 +33,14 @@ public object FromDB(ColumnMap map, object dbValue) { return true; } - else if (val == 0) + if (val == 0) { return false; } - else - { - throw new ConversionException( - string.Format( + throw new ConversionException( + string.Format( "The BooleanCharConverter could not convert the value '{0}' to a boolean.", dbValue)); - } } public object ToDB(object clrValue) @@ -54,14 +51,11 @@ public object ToDB(object clrValue) { return 1; } - else if (val == false) + if (val == false) { return 0; } - else - { - return DBNull.Value; - } + return DBNull.Value; } public Type DbType diff --git a/Marr.Data/Converters/BooleanYNConverter.cs b/Marr.Data/Converters/BooleanYNConverter.cs index 9043f220f..b116bdeaf 100644 --- a/Marr.Data/Converters/BooleanYNConverter.cs +++ b/Marr.Data/Converters/BooleanYNConverter.cs @@ -33,17 +33,14 @@ public object FromDB(ColumnMap map, object dbValue) { return true; } - else if (val == "N") + if (val == "N") { return false; } - else - { - throw new ConversionException( - string.Format( + throw new ConversionException( + string.Format( "The BooleanYNConverter could not convert the value '{0}' to a boolean.", dbValue)); - } } public object ToDB(object clrValue) @@ -54,14 +51,11 @@ public object ToDB(object clrValue) { return "Y"; } - else if (val == false) + if (val == false) { return "N"; } - else - { - return DBNull.Value; - } + return DBNull.Value; } public Type DbType diff --git a/Marr.Data/Converters/EnumIntConverter.cs b/Marr.Data/Converters/EnumIntConverter.cs index 4fa4c8aa4..a7d528d16 100644 --- a/Marr.Data/Converters/EnumIntConverter.cs +++ b/Marr.Data/Converters/EnumIntConverter.cs @@ -24,16 +24,14 @@ public object FromDB(ColumnMap map, object dbValue) { if (dbValue == null || dbValue == DBNull.Value) return null; - else - return Enum.ToObject(map.FieldType, (int)dbValue); + return Enum.ToObject(map.FieldType, (int)dbValue); } public object ToDB(object clrValue) { if (clrValue == null) return DBNull.Value; - else - return (int)clrValue; + return (int)clrValue; } public Type DbType diff --git a/Marr.Data/Converters/EnumStringConverter.cs b/Marr.Data/Converters/EnumStringConverter.cs index cdece6397..4e304fc10 100644 --- a/Marr.Data/Converters/EnumStringConverter.cs +++ b/Marr.Data/Converters/EnumStringConverter.cs @@ -24,16 +24,14 @@ public object FromDB(ColumnMap map, object dbValue) { if (dbValue == null || dbValue == DBNull.Value) return null; - else - return Enum.Parse(map.FieldType, (string)dbValue); + return Enum.Parse(map.FieldType, (string)dbValue); } public object ToDB(object clrValue) { if (clrValue == null) return DBNull.Value; - else - return clrValue.ToString(); + return clrValue.ToString(); } public Type DbType diff --git a/Marr.Data/DataHelper.cs b/Marr.Data/DataHelper.cs index 7fb7e0dc8..3c6e450f5 100644 --- a/Marr.Data/DataHelper.cs +++ b/Marr.Data/DataHelper.cs @@ -65,10 +65,7 @@ public static string GetColumName(this IColumnInfo col, bool useAltName) { return col.TryGetAltName(); } - else - { - return col.Name; - } + return col.Name; } /// diff --git a/Marr.Data/DataMapper.cs b/Marr.Data/DataMapper.cs index edc37ea2a..b35b4596f 100644 --- a/Marr.Data/DataMapper.cs +++ b/Marr.Data/DataMapper.cs @@ -174,8 +174,7 @@ public object ExecuteScalar(string sql) { 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 { @@ -197,8 +196,7 @@ public int ExecuteNonQuery(string sql) { 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 { @@ -223,8 +221,7 @@ public IEnumerable ExecuteReader(string sql, Func diff --git a/Marr.Data/Mapping/RelationshipBuilder.cs b/Marr.Data/Mapping/RelationshipBuilder.cs index 339ac030c..b4926633f 100644 --- a/Marr.Data/Mapping/RelationshipBuilder.cs +++ b/Marr.Data/Mapping/RelationshipBuilder.cs @@ -153,10 +153,7 @@ private void TryAddRelationshipForField(string fieldName) fieldName, typeof(TEntity).Name)); } - else - { - Relationships.Add(relationship); - } + Relationships.Add(relationship); } /// diff --git a/Marr.Data/Mapping/Strategies/ReflectionMapStrategyBase.cs b/Marr.Data/Mapping/Strategies/ReflectionMapStrategyBase.cs index 3e75cd716..429cfec87 100644 --- a/Marr.Data/Mapping/Strategies/ReflectionMapStrategyBase.cs +++ b/Marr.Data/Mapping/Strategies/ReflectionMapStrategyBase.cs @@ -67,10 +67,7 @@ public string MapTable(Type entityType) { return (atts[0] as TableAttribute).Name; } - else - { - return entityType.Name; - } + return entityType.Name; } /// diff --git a/Marr.Data/Parameters/DbTypeBuilder.cs b/Marr.Data/Parameters/DbTypeBuilder.cs index 7f5bcee88..94b6d2f5c 100644 --- a/Marr.Data/Parameters/DbTypeBuilder.cs +++ b/Marr.Data/Parameters/DbTypeBuilder.cs @@ -25,41 +25,40 @@ public Enum GetDbType(Type type) if (type == typeof(String)) return DbType.String; - else if (type == typeof(Int32)) + if (type == typeof(Int32)) return DbType.Int32; - else if (type == typeof(Decimal)) + if (type == typeof(Decimal)) return DbType.Decimal; - else if (type == typeof(DateTime)) + if (type == typeof(DateTime)) return DbType.DateTime; - else if (type == typeof(Boolean)) + if (type == typeof(Boolean)) return DbType.Boolean; - else if (type == typeof(Int16)) + if (type == typeof(Int16)) return DbType.Int16; - else if (type == typeof(Single)) + if (type == typeof(Single)) return DbType.Single; - else if (type == typeof(Int64)) + if (type == typeof(Int64)) return DbType.Int64; - else if (type == typeof(Double)) + if (type == typeof(Double)) return DbType.Double; - else if (type == typeof(Byte)) + if (type == typeof(Byte)) return DbType.Byte; - else if (type == typeof(Byte[])) + if (type == typeof(Byte[])) return DbType.Binary; - else if (type == typeof(Guid)) + if (type == typeof(Guid)) return DbType.Guid; - else - return DbType.Object; + return DbType.Object; } public void SetDbType(IDbDataParameter param, Enum dbType) diff --git a/Marr.Data/Parameters/OleDbTypeBuilder.cs b/Marr.Data/Parameters/OleDbTypeBuilder.cs index e500df501..d8af140a4 100644 --- a/Marr.Data/Parameters/OleDbTypeBuilder.cs +++ b/Marr.Data/Parameters/OleDbTypeBuilder.cs @@ -26,38 +26,37 @@ public Enum GetDbType(Type type) if (type == typeof(String)) return OleDbType.VarChar; - else if (type == typeof(Int32)) + if (type == typeof(Int32)) return OleDbType.Integer; - else if (type == typeof(Decimal)) + if (type == typeof(Decimal)) return OleDbType.Decimal; - else if (type == typeof(DateTime)) + if (type == typeof(DateTime)) return OleDbType.DBTimeStamp; - else if (type == typeof(Boolean)) + if (type == typeof(Boolean)) return OleDbType.Boolean; - else if (type == typeof(Int16)) + if (type == typeof(Int16)) return OleDbType.SmallInt; - else if (type == typeof(Int64)) + if (type == typeof(Int64)) return OleDbType.BigInt; - else if (type == typeof(Double)) + if (type == typeof(Double)) return OleDbType.Double; - else if (type == typeof(Byte)) + if (type == typeof(Byte)) return OleDbType.Binary; - else if (type == typeof(Byte[])) + if (type == typeof(Byte[])) return OleDbType.VarBinary; - else if (type == typeof(Guid)) + if (type == typeof(Guid)) return OleDbType.Guid; - else - return OleDbType.Variant; + return OleDbType.Variant; } public void SetDbType(IDbDataParameter param, Enum dbType) diff --git a/Marr.Data/Parameters/SqlDbTypeBuilder.cs b/Marr.Data/Parameters/SqlDbTypeBuilder.cs index 14123df73..39d2fdaae 100644 --- a/Marr.Data/Parameters/SqlDbTypeBuilder.cs +++ b/Marr.Data/Parameters/SqlDbTypeBuilder.cs @@ -26,41 +26,40 @@ public Enum GetDbType(Type type) if (type == typeof(String)) return SqlDbType.VarChar; - else if (type == typeof(Int32)) + if (type == typeof(Int32)) return SqlDbType.Int; - else if (type == typeof(Decimal)) + if (type == typeof(Decimal)) return SqlDbType.Decimal; - else if (type == typeof(DateTime)) + if (type == typeof(DateTime)) return SqlDbType.DateTime; - else if (type == typeof(Boolean)) + if (type == typeof(Boolean)) return SqlDbType.Bit; - else if (type == typeof(Int16)) + if (type == typeof(Int16)) return SqlDbType.SmallInt; - else if (type == typeof(Int64)) + if (type == typeof(Int64)) return SqlDbType.BigInt; - else if (type == typeof(Double)) + if (type == typeof(Double)) return SqlDbType.Float; - else if (type == typeof(Char)) + if (type == typeof(Char)) return SqlDbType.Char; - else if (type == typeof(Byte)) + if (type == typeof(Byte)) return SqlDbType.Binary; - else if (type == typeof(Byte[])) + if (type == typeof(Byte[])) return SqlDbType.VarBinary; - else if (type == typeof(Guid)) + if (type == typeof(Guid)) return SqlDbType.UniqueIdentifier; - else - return SqlDbType.Variant; + return SqlDbType.Variant; } public void SetDbType(IDbDataParameter param, Enum dbType) diff --git a/Marr.Data/QGen/PagingQueryDecorator.cs b/Marr.Data/QGen/PagingQueryDecorator.cs index 396bc0dba..60a2ece05 100644 --- a/Marr.Data/QGen/PagingQueryDecorator.cs +++ b/Marr.Data/QGen/PagingQueryDecorator.cs @@ -32,10 +32,7 @@ public string Generate() { return ComplexPaging(); } - else - { - return SimplePaging(); - } + return SimplePaging(); } /// diff --git a/Marr.Data/QGen/RowCountQueryDecorator.cs b/Marr.Data/QGen/RowCountQueryDecorator.cs index d347a542c..f7c225389 100644 --- a/Marr.Data/QGen/RowCountQueryDecorator.cs +++ b/Marr.Data/QGen/RowCountQueryDecorator.cs @@ -18,10 +18,7 @@ public string Generate() { return ComplexRowCount(); } - else - { - return SimpleRowCount(); - } + return SimpleRowCount(); } /// diff --git a/Marr.Data/QGen/SelectQuery.cs b/Marr.Data/QGen/SelectQuery.cs index 492582e48..886e0d651 100644 --- a/Marr.Data/QGen/SelectQuery.cs +++ b/Marr.Data/QGen/SelectQuery.cs @@ -95,10 +95,7 @@ public string NameOrAltName(IColumnInfo columnInfo) { return columnInfo.AltName; } - else - { - return columnInfo.Name; - } + return columnInfo.Name; } public void BuildFromClause(StringBuilder sql) diff --git a/Marr.Data/QGen/SqlitePagingQueryDecorator.cs b/Marr.Data/QGen/SqlitePagingQueryDecorator.cs index 30de17195..f77614523 100644 --- a/Marr.Data/QGen/SqlitePagingQueryDecorator.cs +++ b/Marr.Data/QGen/SqlitePagingQueryDecorator.cs @@ -32,10 +32,7 @@ public string Generate() { return ComplexPaging(); } - else - { - return SimplePaging(); - } + return SimplePaging(); } private string SimplePaging() diff --git a/Marr.Data/QGen/WhereBuilder.cs b/Marr.Data/QGen/WhereBuilder.cs index a227891d1..64992d542 100644 --- a/Marr.Data/QGen/WhereBuilder.cs +++ b/Marr.Data/QGen/WhereBuilder.cs @@ -281,10 +281,7 @@ public override string ToString() { return _sb.ToString(); } - else - { - return _constantWhereClause; - } + return _constantWhereClause; } } diff --git a/Marr.Data/Reflection/ReflectionHelper.cs b/Marr.Data/Reflection/ReflectionHelper.cs index ab2d47ba7..c8ca97308 100644 --- a/Marr.Data/Reflection/ReflectionHelper.cs +++ b/Marr.Data/Reflection/ReflectionHelper.cs @@ -32,14 +32,11 @@ public static object GetDefaultValue(Type fieldType) { return null; } - else if (fieldType.IsValueType) + if (fieldType.IsValueType) { return Activator.CreateInstance(fieldType); } - else - { - return null; - } + return null; } /// diff --git a/NzbDrone.Common/PathExtensions.cs b/NzbDrone.Common/PathExtensions.cs index 85c852e60..7c2eea7a5 100644 --- a/NzbDrone.Common/PathExtensions.cs +++ b/NzbDrone.Common/PathExtensions.cs @@ -11,11 +11,11 @@ public static class PathExtensions private const string NZBDRONE_LOG_DB = "logs.db"; private const string BACKUP_ZIP_FILE = "NzbDrone_Backup.zip"; private const string NLOG_CONFIG_FILE = "nlog.config"; + private const string UPDATE_CLIENT_EXE = "nzbdrone.update.exe"; private static readonly string UPDATE_SANDBOX_FOLDER_NAME = "nzbdrone_update" + Path.DirectorySeparatorChar; private static readonly string UPDATE_PACKAGE_FOLDER_NAME = "nzbdrone" + Path.DirectorySeparatorChar; private static readonly string UPDATE_BACKUP_FOLDER_NAME = "nzbdrone_backup" + Path.DirectorySeparatorChar; - private static readonly string UPDATE_CLIENT_EXE = "nzbdrone.update.exe"; private static readonly string UPDATE_CLIENT_FOLDER_NAME = "NzbDrone.Update" + Path.DirectorySeparatorChar; private static readonly string UPDATE_LOG_FOLDER_NAME = "UpdateLogs" + Path.DirectorySeparatorChar; diff --git a/NzbDrone.Core/Notifications/Xbmc/JsonApiProvider.cs b/NzbDrone.Core/Notifications/Xbmc/JsonApiProvider.cs index 2a74fa996..520c1c3a2 100644 --- a/NzbDrone.Core/Notifications/Xbmc/JsonApiProvider.cs +++ b/NzbDrone.Core/Notifications/Xbmc/JsonApiProvider.cs @@ -118,12 +118,9 @@ public string GetSeriesPath(XbmcSettings settings, Series series) return null; } - else - { - var matchingSeries = allSeries.FirstOrDefault(s => s.ImdbNumber == series.TvdbId || s.Label == series.Title); + var matchingSeries = allSeries.FirstOrDefault(s => s.ImdbNumber == series.TvdbId || s.Label == series.Title); - if (matchingSeries != null) return matchingSeries.File; - } + if (matchingSeries != null) return matchingSeries.File; return null; }