Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove some redundant null checks #745

Merged
merged 1 commit into from
Jan 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions RepoDb.Core/RepoDb/Cachers/DbFieldCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ internal static IEnumerable<DbField> GetInternal<TDbConnection>(TDbConnection co
var key = (long)type.GetHashCode();

// Note: For SqlConnection, the ConnectionString is changing if the (Integrated Security=False). Actually for this isolation, the database name is enough.
if (!string.IsNullOrWhiteSpace(connection?.Database))
if (!string.IsNullOrWhiteSpace(connection.Database))
{
key += connection.Database.GetHashCode();
}
Expand Down Expand Up @@ -175,7 +175,7 @@ internal static async Task<IEnumerable<DbField>> GetAsyncInternal<TDbConnection>
var key = (long)type.GetHashCode();

// Note: For SqlConnection, the ConnectionString is changing if the (Integrated Security=False). Actually for this isolation, the database name is enough.
if (!string.IsNullOrWhiteSpace(connection?.Database))
if (!string.IsNullOrWhiteSpace(connection.Database))
{
key += connection.Database.GetHashCode();
}
Expand Down
2 changes: 1 addition & 1 deletion RepoDb.Core/RepoDb/DataEntityDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public DataEntityDataReader(string tableName,
// Type
var entityType = typeof(TEntity);
EntityType = entityType == StaticType.Object ?
(entities?.FirstOrDefault()?.GetType() ?? entityType) :
(entities.FirstOrDefault()?.GetType() ?? entityType) :
entityType;
isDictionaryStringObject = EntityType.IsDictionaryStringObject();

Expand Down
18 changes: 9 additions & 9 deletions RepoDb.Core/RepoDb/Extensions/DbCommandExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ private static void CreateParametersInternal(IDbCommand command,
if (propertiesToSkip != null)
{
classProperties = classProperties?
.Where(p => propertiesToSkip?.Contains(p.PropertyInfo.Name,
.Where(p => propertiesToSkip.Contains(p.PropertyInfo.Name,
StringComparer.OrdinalIgnoreCase) == false);
}

Expand Down Expand Up @@ -401,7 +401,7 @@ internal static void CreateParameters(IDbCommand command,
{
return;
}
CreateParameters(command, queryGroup?.GetFields(true), propertiesToSkip, entityType, dbFields);
CreateParameters(command, queryGroup.GetFields(true), propertiesToSkip, entityType, dbFields);
}

/// <summary>
Expand Down Expand Up @@ -475,7 +475,7 @@ private static void CreateParameters(this IDbCommand command,
// Variables
var dbField = GetDbField(queryField.Field.Name, dbFields);
var value = queryField.Parameter.Value;
var valueType = value?.GetType()?.GetUnderlyingType();
var valueType = value?.GetType().GetUnderlyingType();
var isEnum = valueType?.IsEnum;

// PropertyHandler
Expand All @@ -492,14 +492,14 @@ private static void CreateParameters(this IDbCommand command,
if (IsAutomaticConversion(dbField))
{
var underlyingType = dbField.Type.GetUnderlyingType();
value = AutomaticConvert(value, classProperty?.PropertyInfo?.PropertyType?.GetUnderlyingType(), underlyingType);
value = AutomaticConvert(value, classProperty?.PropertyInfo?.PropertyType.GetUnderlyingType(), underlyingType);
valueType = underlyingType;
}

// DbType
var dbType = (valueType != null ? clientTypeToDbTypeResolver.Resolve(valueType) : null) ??
classProperty?.GetDbType() ??
value?.GetType()?.GetDbType();
value?.GetType().GetDbType();

// Try get fallback dbType by classProperty to avoid being mistaken as string when value is null.
if (dbType == null && classProperty != null)
Expand Down Expand Up @@ -557,7 +557,7 @@ private static void CreateParametersForInOperation(this IDbCommand command,
{
var name = string.Concat(queryField.Parameter.Name, "_In_", i);
var value = values[i];
var valueType = value?.GetType()?.GetUnderlyingType();
var valueType = value?.GetType().GetUnderlyingType();
var isEnum = valueType?.IsEnum;

// Propertyhandler
Expand All @@ -573,7 +573,7 @@ private static void CreateParametersForInOperation(this IDbCommand command,
if (IsAutomaticConversion(dbField))
{
var underlyingType = dbField.Type.GetUnderlyingType();
value = AutomaticConvert(value, value?.GetType()?.GetUnderlyingType(), underlyingType);
value = AutomaticConvert(value, value?.GetType().GetUnderlyingType(), underlyingType);
valueType = underlyingType;
}

Expand Down Expand Up @@ -613,8 +613,8 @@ private static void CreateParametersForBetweenOperation(this IDbCommand command,
{
var leftValue = values[0];
var rightValue = values[1];
var leftValueType = leftValue?.GetType()?.GetUnderlyingType();
var rightValueType = rightValue?.GetType()?.GetUnderlyingType();
var leftValueType = leftValue?.GetType().GetUnderlyingType();
var rightValueType = rightValue?.GetType().GetUnderlyingType();
var isLeftEnum = leftValueType?.IsEnum;
var isRightEnum = rightValueType?.IsEnum;

Expand Down
12 changes: 6 additions & 6 deletions RepoDb.Core/RepoDb/Extensions/DbConnectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2051,7 +2051,7 @@ internal static Field GetAndGuardPrimaryKeyOrIdentityKey(Type entityType,
else
{
// Properties
var properties = PropertyCache.Get(entityType) ?? entityType?.GetClassProperties();
var properties = PropertyCache.Get(entityType) ?? entityType.GetClassProperties();
var property = (ClassProperty)null;

// Primary
Expand Down Expand Up @@ -2161,7 +2161,7 @@ internal static QueryGroup WhatToQueryGroup<T>(this IDbConnection connection,
var queryGroup = WhatToQueryGroup<T>(what);
if (queryGroup == null)
{
var whatType = what?.GetType();
var whatType = what.GetType();
if (whatType.IsClassType() || whatType.IsAnonymousType())
{
var field = GetAndGuardPrimaryKeyOrIdentityKey(connection, tableName, transaction, whatType);
Expand Down Expand Up @@ -2199,7 +2199,7 @@ internal static async Task<QueryGroup> WhatToQueryGroupAsync<T>(this IDbConnecti
var queryGroup = WhatToQueryGroup<T>(what);
if (queryGroup == null)
{
var whatType = what?.GetType();
var whatType = what.GetType();
if (whatType.IsClassType() || whatType.IsAnonymousType())
{
var field = await GetAndGuardPrimaryKeyOrIdentityKeyAsync(connection, tableName, transaction, whatType, cancellationToken);
Expand Down Expand Up @@ -2421,7 +2421,7 @@ internal static QueryGroup ToQueryGroup(DbField dbField,
}
if (dbField != null)
{
var type = entity?.GetType();
var type = entity.GetType();
if (type.IsClassType())
{
var properties = PropertyCache.Get(type) ?? type.GetClassProperties();
Expand Down Expand Up @@ -2632,7 +2632,7 @@ internal static QueryGroup CreateQueryGroupForUpsert(IDictionary<string, object>
}
}

if (queryFields?.Any() != true)
if (queryFields.Any() != true)
{
throw new MissingFieldsException("No qualifier fields defined for the 'Upsert' operation. Please check the items defined at the dictionary object.");
}
Expand Down Expand Up @@ -2837,7 +2837,7 @@ private static DbCommand CreateDbCommandForExecutionInternal(this IDbConnection
// Func
if (param != null)
{
var func = FunctionCache.GetPlainTypeToDbParametersCompiledFunction(param?.GetType(), entityType, dbFields);
var func = FunctionCache.GetPlainTypeToDbParametersCompiledFunction(param.GetType(), entityType, dbFields);
if (func != null)
{
var cmd = (DbCommand)command;
Expand Down
8 changes: 4 additions & 4 deletions RepoDb.Core/RepoDb/Extensions/ExpressionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static Field GetField(this MethodCallExpression expression)
// Contains
if (expression.Method.Name == "Contains")
{
var last = expression.Arguments?.Last();
var last = expression.Arguments.Last();
if (last?.IsMember() == true)
{
return last.ToMember().GetField();
Expand Down Expand Up @@ -198,7 +198,7 @@ public static string GetName(this MethodCallExpression expression)
expression.Method.Name == "StartsWith" ||
expression.Method.Name == "EndsWith")
{
var last = expression.Arguments?.Last();
var last = expression.Arguments.Last();
if (last?.IsMember() == true)
{
return last.ToMember().Member.GetMappedName();
Expand Down Expand Up @@ -383,7 +383,7 @@ public static object GetValue(this UnaryExpression expression) =>
/// <param name="expression">The instance of <see cref="MethodCallExpression"/> object where the value is to be extracted.</param>
/// <returns>The extracted value from <see cref="MethodCallExpression"/> object.</returns>
public static object GetValue(this MethodCallExpression expression) =>
expression.Method.GetValue(expression.Object?.GetValue(), expression.Arguments?.Select(argExpression => argExpression.GetValue()).ToArray());
expression.Method.GetValue(expression.Object?.GetValue(), expression.Arguments.Select(argExpression => argExpression.GetValue()).ToArray());

/// <summary>
/// Gets a value from the current instance of <see cref="MemberExpression"/> object.
Expand Down Expand Up @@ -419,7 +419,7 @@ public static object GetValue(this ListInitExpression expression)
var list = Activator.CreateInstance(expression.Type);
foreach (var item in expression.Initializers)
{
item.AddMethod.Invoke(list, new[] { item.Arguments?.FirstOrDefault().GetValue() });
item.AddMethod.Invoke(list, new[] { item.Arguments.FirstOrDefault().GetValue() });
}
return list;
}
Expand Down
2 changes: 1 addition & 1 deletion RepoDb.Core/RepoDb/Extensions/PropertyInfoExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static T GetCustomAttribute<T>(this PropertyInfo property)
public static Attribute GetCustomAttribute(this PropertyInfo property,
Type type)
{
var attributes = property.GetCustomAttributes(type, false)?.WithType<Attribute>();
var attributes = property.GetCustomAttributes(type, false).WithType<Attribute>();
return attributes?.FirstOrDefault(a => a.GetType() == type);
}

Expand Down
8 changes: 4 additions & 4 deletions RepoDb.Core/RepoDb/Extensions/TypeExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public static class TypeExtension
/// <param name="type">The current type.</param>
/// <returns>The instance of <see cref="ConstructorInfo"/> with the most arguments.</returns>
public static ConstructorInfo GetConstructorWithMostArguments(this Type type) =>
type.GetConstructors()?.Where(item => item.GetParameters().Length > 0)?
.OrderByDescending(item => item.GetParameters().Length)?.FirstOrDefault();
type.GetConstructors().Where(item => item.GetParameters().Length > 0)
.OrderByDescending(item => item.GetParameters().Length).FirstOrDefault();

/// <summary>
/// Checks whether the current type is of type <see cref="object"/>.
Expand Down Expand Up @@ -162,7 +162,7 @@ public static Type MakeGenericTypeFrom(this Type currentType,
Type sourceType)
{
var genericTypes = sourceType?.GetGenericArguments();
if (genericTypes?.Length == currentType?.GetGenericArguments()?.Length)
if (genericTypes?.Length == currentType?.GetGenericArguments().Length)
{
return currentType.MakeGenericType(genericTypes);
}
Expand Down Expand Up @@ -201,7 +201,7 @@ internal static bool IsClassHandlerValidForModel(this Type classHandlerType,
item.Name == StaticType.IClassHandler.Name && item.Namespace == StaticType.IClassHandler.Namespace);
if (targetInterface != null)
{
return targetInterface.GetGenericArguments()?.FirstOrDefault() == targetModelType;
return targetInterface.GetGenericArguments().FirstOrDefault() == targetModelType;
}
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion RepoDb.Core/RepoDb/Field.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ internal static IEnumerable<Field> Parse<TEntity>(NewExpression expression)
.WithType<PropertyInfo>();
var classProperties = PropertyCache.Get<TEntity>()?
.Where(classProperty =>
properties?.FirstOrDefault(property => string.Equals(property.Name, classProperty.PropertyInfo.Name, StringComparison.OrdinalIgnoreCase)) != null)?
properties?.FirstOrDefault(property => string.Equals(property.Name, classProperty.PropertyInfo.Name, StringComparison.OrdinalIgnoreCase)) != null)
.Select(classProperty => classProperty.PropertyInfo);
return (classProperties ?? properties).Select(property => property.AsField());
}
Expand Down
4 changes: 2 additions & 2 deletions RepoDb.Core/RepoDb/Operations/DbConnection/DeleteAll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ internal static int DeleteAllInternal(this IDbConnection connection,
{
break;
}
var field = new QueryField(key.Name.AsQuoted(dbSetting), Operation.In, keyValues?.AsList());
var field = new QueryField(key.Name.AsQuoted(dbSetting), Operation.In, keyValues.AsList());
deletedRows += DeleteInternal(connection: connection,
tableName: tableName,
where: new QueryGroup(field),
Expand Down Expand Up @@ -906,7 +906,7 @@ public static async Task<int> DeleteAllAsyncInternal(this IDbConnection connecti
{
break;
}
var field = new QueryField(key.Name.AsQuoted(dbSetting), Operation.In, keyValues?.AsList());
var field = new QueryField(key.Name.AsQuoted(dbSetting), Operation.In, keyValues.AsList());
deletedRows += await DeleteAsyncInternal(connection: connection,
tableName: tableName,
where: new QueryGroup(field),
Expand Down
4 changes: 2 additions & 2 deletions RepoDb.Core/RepoDb/Operations/DbConnection/Insert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ internal static TResult InsertInternal<TEntity, TResult>(this IDbConnection conn
IStatementBuilder statementBuilder = null)
where TEntity : class
{
if (entity?.GetType()?.IsDictionaryStringObject() == true)
if (entity?.GetType().IsDictionaryStringObject() == true)
{
return InsertInternalBase<IDictionary<string, object>, TResult>(connection: connection,
tableName: tableName,
Expand Down Expand Up @@ -398,7 +398,7 @@ internal static Task<TResult> InsertAsyncInternal<TEntity, TResult>(this IDbConn
CancellationToken cancellationToken = default)
where TEntity : class
{
if (entity?.GetType()?.IsDictionaryStringObject() == true)
if (entity?.GetType().IsDictionaryStringObject() == true)
{
return InsertAsyncInternalBase<IDictionary<string, object>, TResult>(connection: connection,
tableName: tableName,
Expand Down
8 changes: 4 additions & 4 deletions RepoDb.Core/RepoDb/Operations/DbConnection/Merge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ internal static TResult MergeInternal<TEntity, TResult>(this IDbConnection conne
// Return the result
if (setting.IsUseUpsert == false)
{
if (entity?.GetType()?.IsDictionaryStringObject() == true)
if (entity?.GetType().IsDictionaryStringObject() == true)
{
return MergeInternalBase<IDictionary<string, object>, TResult>(connection: connection,
tableName: tableName,
Expand Down Expand Up @@ -691,7 +691,7 @@ internal static TResult MergeInternal<TEntity, TResult>(this IDbConnection conne
}
else
{
if (entity?.GetType()?.IsDictionaryStringObject() == true)
if (entity?.GetType().IsDictionaryStringObject() == true)
{
return UpsertInternalBase<IDictionary<string, object>, TResult>(connection: connection,
tableName: tableName,
Expand Down Expand Up @@ -1416,7 +1416,7 @@ internal static Task<TResult> MergeAsyncInternal<TEntity, TResult>(this IDbConne
// Return the result
if (setting.IsUseUpsert == false)
{
if (entity?.GetType()?.IsDictionaryStringObject() == true)
if (entity?.GetType().IsDictionaryStringObject() == true)
{
return MergeAsyncInternalBase<IDictionary<string, object>, TResult>(connection: connection,
tableName: tableName,
Expand Down Expand Up @@ -1447,7 +1447,7 @@ internal static Task<TResult> MergeAsyncInternal<TEntity, TResult>(this IDbConne
}
else
{
if (entity?.GetType()?.IsDictionaryStringObject() == true)
if (entity?.GetType().IsDictionaryStringObject() == true)
{
return UpsertAsyncInternalBase<IDictionary<string, object>, TResult>(connection: connection,
tableName: tableName,
Expand Down
4 changes: 2 additions & 2 deletions RepoDb.Core/RepoDb/Operations/DbConnection/Update.cs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ internal static int UpdateInternal<TEntity>(this IDbConnection connection,
IStatementBuilder statementBuilder = null)
where TEntity : class
{
if (entity?.GetType()?.IsDictionaryStringObject() == true)
if (entity?.GetType().IsDictionaryStringObject() == true)
{
return UpdateInternalBase<IDictionary<string, object>>(connection: connection,
tableName: tableName,
Expand Down Expand Up @@ -1215,7 +1215,7 @@ internal static Task<int> UpdateAsyncInternal<TEntity>(this IDbConnection connec
CancellationToken cancellationToken = default)
where TEntity : class
{
if (entity?.GetType()?.IsDictionaryStringObject() == true)
if (entity?.GetType().IsDictionaryStringObject() == true)
{
return UpdateAsyncInternalBase<IDictionary<string, object>>(connection: connection,
tableName: tableName,
Expand Down
2 changes: 1 addition & 1 deletion RepoDb.Core/RepoDb/QueryField/ParseExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ internal static QueryField ParseContains<TEntity>(MethodCallExpression expressio
// Value
if (expression?.Object != null)
{
if (expression?.Object?.Type == StaticType.String)
if (expression.Object?.Type == StaticType.String)
{
var likeable = ConvertToLikeableValue("Contains", Converter.ToType<string>(expression.Arguments.First().GetValue()));
return ToLike(property.GetMappedName(), likeable, unaryNodeType);
Expand Down
2 changes: 1 addition & 1 deletion RepoDb.Core/RepoDb/QueryGroup/ParseDynamic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static QueryGroup Parse<T>(T obj,
}

// Return
return queryFields?.Any() == true ? new QueryGroup(queryFields).Fix() : null;
return queryFields.Any() == true ? new QueryGroup(queryFields).Fix() : null;
}
}
}
4 changes: 2 additions & 2 deletions RepoDb.Core/RepoDb/QueryGroup/ParseExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ private static QueryGroup Parse<TEntity>(UnaryExpression expression)
{
var queryGroup = (QueryGroup)null;

if (expression.Operand?.IsMember() == true)
if (expression.Operand.IsMember() == true)
{
queryGroup = Parse<TEntity>(expression.Operand.ToMember(), expression.NodeType);
}
else if (expression.Operand?.IsMethodCall() == true)
else if (expression.Operand.IsMethodCall() == true)
{
queryGroup = Parse<TEntity>(expression.Operand.ToMethodCall(), expression.NodeType);
}
Expand Down
Loading