Skip to content

Commit

Permalink
Merge pull request #723 from SimonCropp/mergeType
Browse files Browse the repository at this point in the history
merge type checks
  • Loading branch information
mikependon authored Jan 13, 2021
2 parents a7729c1 + 214917b commit a9db587
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 51 deletions.
4 changes: 2 additions & 2 deletions RepoDb.Core/RepoDb/ClassProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,9 @@ public override int GetHashCode() =>
/// <returns>True if the two instance is the same.</returns>
public override bool Equals(object obj)
{
if (obj is ClassProperty)
if (obj is ClassProperty property)
{
return PropertyInfo.Equals(((ClassProperty)obj).PropertyInfo);
return PropertyInfo.Equals(property.PropertyInfo);
}
return Equals(obj);
}
Expand Down
4 changes: 2 additions & 2 deletions RepoDb.Core/RepoDb/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public static object DbNullToNull(object value) =>
/// <returns>The converted value.</returns>
public static T ToType<T>(object value)
{
if (value is T)
if (value is T t)
{
return (T)value;
return t;
}
return value == null || DbNullToNull(value) == null ? default(T) :
(T)Convert.ChangeType(value, typeof(T));
Expand Down
4 changes: 2 additions & 2 deletions RepoDb.Core/RepoDb/Extensions/ArrayExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public static IEnumerable<T> AsEnumerable<T>(this Array array)
{
foreach (var value in array)
{
if (value is T)
if (value is T t)
{
yield return (T)value;
yield return t;
}
}
}
Expand Down
18 changes: 8 additions & 10 deletions RepoDb.Core/RepoDb/Extensions/DbCommandExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,21 @@ internal static void CreateParameters(this IDbCommand command,
}

// QueryField
else if (param is QueryField)
else if (param is QueryField field)
{
CreateParameters(command, (QueryField)param, propertiesToSkip, entityType, dbFields);
CreateParameters(command, field, propertiesToSkip, entityType, dbFields);
}

// IEnumerable<QueryField>
else if (param is IEnumerable<QueryField>)
else if (param is IEnumerable<QueryField> fields)
{
CreateParameters(command, (IEnumerable<QueryField>)param, propertiesToSkip, entityType, dbFields);
CreateParameters(command, fields, propertiesToSkip, entityType, dbFields);
}

// QueryGroup
else if (param is QueryGroup)
else if (param is QueryGroup group)
{
CreateParameters(command, (QueryGroup)param, propertiesToSkip, entityType, dbFields);
CreateParameters(command, group, propertiesToSkip, entityType, dbFields);
}

// Other
Expand Down Expand Up @@ -330,9 +330,8 @@ private static void CreateParameters(IDbCommand command,
var valueType = (Type)null;

// CommandParameter
if (kvp.Value is CommandParameter)
if (kvp.Value is CommandParameter commandParameter)
{
var commandParameter = (CommandParameter)kvp.Value;
classProperty = PropertyCache.Get(commandParameter.MappedToType, kvp.Key);
value = commandParameter.Value;
valueType = value?.GetType()?.GetUnderlyingType();
Expand Down Expand Up @@ -516,9 +515,8 @@ private static void CreateParameters(this IDbCommand command,

// Direction
var parameterDirection = (ParameterDirection?)null;
if (queryField is DirectionalQueryField)
if (queryField is DirectionalQueryField directionalQueryField)
{
var directionalQueryField = (DirectionalQueryField)queryField;
parameterDirection = directionalQueryField.Direction;

// Ensure the DB type
Expand Down
52 changes: 26 additions & 26 deletions RepoDb.Core/RepoDb/Extensions/DbConnectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1811,34 +1811,34 @@ public static IStatementBuilder GetStatementBuilder(this IDbConnection connectio
#region DbParameters

/// <summary>
///
///
/// </summary>
/// <param name="param"></param>
internal static void SetOutputParameters(object param)
{
if (param is QueryGroup)
if (param is QueryGroup group)
{
SetOutputParameters((QueryGroup)param);
SetOutputParameters(group);
}
else if (param is IEnumerable<QueryField>)
else if (param is IEnumerable<QueryField> fields)
{
SetOutputParameters((IEnumerable<QueryField>)param);
SetOutputParameters(fields);
}
else if (param is QueryField)
else if (param is QueryField field)
{
SetOutputParameter((QueryField)param);
SetOutputParameter(field);
}
}

/// <summary>
///
///
/// </summary>
/// <param name="queryGroup"></param>
internal static void SetOutputParameters(QueryGroup queryGroup) =>
SetOutputParameters(queryGroup.GetFields(true));

/// <summary>
///
///
/// </summary>
/// <param name="queryFields"></param>
internal static void SetOutputParameters(IEnumerable<QueryField> queryFields)
Expand All @@ -1854,7 +1854,7 @@ internal static void SetOutputParameters(IEnumerable<QueryField> queryFields)
}

/// <summary>
///
///
/// </summary>
/// <param name="queryField"></param>
internal static void SetOutputParameter(QueryField queryField)
Expand All @@ -1871,7 +1871,7 @@ internal static void SetOutputParameter(QueryField queryField)
#region Order Columns

/// <summary>
///
///
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="command"></param>
Expand Down Expand Up @@ -2357,17 +2357,17 @@ internal static QueryGroup WhatToQueryGroup<T>(T what)
{
return null;
}
else if (what is QueryField)
else if (what is QueryField field)
{
return ToQueryGroup(what as QueryField);
return ToQueryGroup(field);
}
else if (what is IEnumerable<QueryField>)
else if (what is IEnumerable<QueryField> fields)
{
return ToQueryGroup(what as IEnumerable<QueryField>);
return ToQueryGroup(fields);
}
else if (what is QueryGroup)
else if (what is QueryGroup group)
{
return what as QueryGroup;
return group;
}
else
{
Expand Down Expand Up @@ -2901,28 +2901,28 @@ internal static CommandArrayParametersText GetCommandArrayParametersText(string
// ExpandoObject
if (param is ExpandoObject || param is System.Collections.IDictionary)
{
if (param is IDictionary<string, object>)
if (param is IDictionary<string, object> objects)
{
return GetCommandArrayParametersText(commandText, (IDictionary<string, object>)param, dbSetting);
return GetCommandArrayParametersText(commandText, objects, dbSetting);
}
}

// QueryField
else if (param is QueryField)
else if (param is QueryField field)
{
return GetCommandArrayParametersText(commandText, (QueryField)param, dbSetting);
return GetCommandArrayParametersText(commandText, field, dbSetting);
}

// QueryFields
else if (param is IEnumerable<QueryField>)
else if (param is IEnumerable<QueryField> fields)
{
return GetCommandArrayParametersText(commandText, (IEnumerable<QueryField>)param, dbSetting);
return GetCommandArrayParametersText(commandText, fields, dbSetting);
}

// QueryGroup
else if (param is QueryGroup)
else if (param is QueryGroup group)
{
return GetCommandArrayParametersText(commandText, (QueryGroup)param, dbSetting);
return GetCommandArrayParametersText(commandText, group, dbSetting);
}

// Others
Expand Down Expand Up @@ -3227,7 +3227,7 @@ internal static string GetRawSqlText(string commandText,
}

// Items
var items = values is IEnumerable<object> ? (IEnumerable<object>)values : values.WithType<object>();
var items = values is IEnumerable<object> objects ? objects : values.WithType<object>();
if (items.Any() != true)
{
var parameter = parameterName.AsParameter(dbSetting);
Expand Down
8 changes: 4 additions & 4 deletions RepoDb.Core/RepoDb/Extensions/EnumerableExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> value,
/// <returns>The <see cref="IEnumerable{T}"/> object in which the items are of type <typeparamref name="TargetType"/>.</returns>
[Obsolete("Use the 'WithType<T>' method insted.")]
public static IEnumerable<TargetType> OfTargetType<SourceType, TargetType>(this IEnumerable<SourceType> value) =>
value is IEnumerable<TargetType> ? (IEnumerable<TargetType>)value : value.OfType<TargetType>();
value is IEnumerable<TargetType> types ? types : value.OfType<TargetType>();

/// <summary>
/// Checks whether the instance of <see cref="System.Collections.IEnumerable"/> is of type <see cref="IEnumerable{T}"/>, then casts it, otherwise,
Expand All @@ -61,7 +61,7 @@ public static IEnumerable<TargetType> OfTargetType<SourceType, TargetType>(this
/// <param name="value">The actual enumerable instance.</param>
/// <returns>The <see cref="IEnumerable{T}"/> object in which the items are of type <typeparamref name="T"/>.</returns>
public static IEnumerable<T> WithType<T>(this System.Collections.IEnumerable value) =>
value is IEnumerable<T> ? (IEnumerable<T>)value : value.OfType<T>();
value is IEnumerable<T> enumerable ? enumerable : value.OfType<T>();

/// <summary>
/// Checks whether the instance of <see cref="IEnumerable{T}"/> is of type <see cref="List{T}"/>, then casts it, otherwise, converts it.
Expand All @@ -71,7 +71,7 @@ public static IEnumerable<T> WithType<T>(this System.Collections.IEnumerable val
/// <param name="value">The actual enumerable instance.</param>
/// <returns>The converted <see cref="IList{T}"/> object.</returns>
public static List<T> AsList<T>(this IEnumerable<T> value) =>
value is List<T> ? (List<T>)value : value?.ToList();
value is List<T> list ? list : value?.ToList();

/// <summary>
/// Checks whether the instance of <see cref="IEnumerable{T}"/> is an array of <typeparamref name="T"/>, then casts it, otherwise, converts it.
Expand All @@ -81,6 +81,6 @@ public static List<T> AsList<T>(this IEnumerable<T> value) =>
/// <param name="value">The actual enumerable instance.</param>
/// <returns>The converted <see cref="Array"/> object.</returns>
public static T[] AsArray<T>(this IEnumerable<T> value) =>
value is T[]? (T[])value : value?.ToArray();
value is T[] array ? array : value?.ToArray();
}
}
5 changes: 3 additions & 2 deletions RepoDb.Core/RepoDb/QueryField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using RepoDb.Enumerations;
using RepoDb.Extensions;
using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Linq;
Expand Down Expand Up @@ -245,9 +246,9 @@ public override int GetHashCode()
}
// The parameter's length affects the uniqueness of the object
else if ((Operation == Operation.In || Operation == Operation.NotIn) &&
Parameter.Value != null && Parameter.Value is System.Collections.IEnumerable)
Parameter.Value != null && Parameter.Value is IEnumerable enumerable)
{
var items = ((System.Collections.IEnumerable)Parameter.Value);
var items = enumerable;
hashCode += items
.WithType<object>()
.Count()
Expand Down
7 changes: 4 additions & 3 deletions RepoDb.Core/RepoDb/QueryGroup/AsMappedObject.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using RepoDb.Enumerations;
using System.Collections;
using RepoDb.Enumerations;
using RepoDb.Extensions;
using System.Collections.Generic;
using System.Dynamic;
Expand Down Expand Up @@ -199,9 +200,9 @@ private static IList<T> GetValueList<T>(T value)
{
var list = new List<T>();

if (value is System.Collections.IEnumerable)
if (value is IEnumerable enumerable)
{
var items = ((System.Collections.IEnumerable)value)
var items = enumerable
.WithType<T>()
.AsList();
list.AddRange(items);
Expand Down

0 comments on commit a9db587

Please sign in to comment.