Skip to content

Commit

Permalink
#1087 Introduced the ApplicationConfiguration and ApplicationConfigur…
Browse files Browse the repository at this point in the history
…ationOptions class.
  • Loading branch information
mikependon committed Sep 20, 2022
1 parent a900e39 commit 306d8b0
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@ public void Initialize()
{
Database.Initialize();
Cleanup();
Converter.ConversionType = ConversionType.Automatic;

ApplicationConfiguration.Setup(new()
{
ConversionType = ConversionType.Automatic
});
}

[TestCleanup]
public void Cleanup()
{
Database.Cleanup();
Converter.ConversionType = ConversionType.Default;

ApplicationConfiguration.Setup(new()
{
ConversionType = ConversionType.Default
});
}

#region TypedResult
Expand Down
30 changes: 30 additions & 0 deletions RepoDb.Core/RepoDb/ApplicationConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using RepoDb.Options;

namespace RepoDb
{
/// <summary>
/// A static class that is being used to define the globalized configurations for the library.
/// </summary>
public static class ApplicationConfiguration
{
#region Methods

/// <summary>
/// Setup the globalized configurations for the application.
/// </summary>
/// <param name="options">The option class that contains the value for the configurations.</param>
public static void Setup(ApplicationConfigurationOptions options) =>
Options = options;

#endregion

#region Properties

/// <summary>
/// Gets the globalized configurations.
/// </summary>
public static ApplicationConfigurationOptions Options { get; private set; } = new ApplicationConfigurationOptions();

#endregion
}
}
5 changes: 0 additions & 5 deletions RepoDb.Core/RepoDb/Constant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,5 @@ public static class Constant
/// The default value of the cache expiration in minutes.
/// </summary>
public const int DefaultCacheItemExpirationInMinutes = 180;

/// <summary>
/// The maximum parameters of ADO.Net when executing a command.
/// </summary>
public const int MaxParametersCount = 2100;
}
}
4 changes: 3 additions & 1 deletion RepoDb.Core/RepoDb/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ public static class Converter
/// Gets or sets the conversion type when converting the instance of <see cref="DbDataReader"/> object into its destination .NET CLR Types.
/// The default value is <see cref="ConversionType.Default"/>.
/// </summary>
[Obsolete("Use the definition of the ApplicationConfigurationOptions class instead.")]
public static ConversionType ConversionType { get; set; } = ConversionType.Default;

/// <summary>
/// Gets or sets the default equivalent database type (of type <see cref="DbType"/>) of an enumeration if it is being used as a parameter to the
/// execution of any non-entity-based operations.
/// </summary>
[Obsolete("Use the definition of the ApplicationConfigurationOptions class instead.")]
public static DbType EnumDefaultDatabaseType { get; set; } = DbType.String;

#endregion
Expand All @@ -34,7 +36,7 @@ public static class Converter
/// <param name="value">The value to be checked for <see cref="DBNull.Value"/>.</param>
/// <returns>The converted value.</returns>
public static object NullToDbNull(object value) =>
ReferenceEquals(null, value) ? DBNull.Value : value;
value is null ? DBNull.Value : value;

/// <summary>
/// Converts the value into null if the value is equals to <see cref="DBNull.Value"/>.
Expand Down
4 changes: 2 additions & 2 deletions RepoDb.Core/RepoDb/Extensions/DbCommandExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ private static IDbDataParameter CreateParameterForEnum(IDbCommand command,
classProperty?.GetDbType() ??
valueType.GetDbType() ??
(dbField != null ? clientTypeToDbTypeResolver.Resolve(dbField.Type) : null) ??
(DbType?)Converter.EnumDefaultDatabaseType;
(DbType?)ApplicationConfiguration.Options.EnumDefaultDatabaseType;

// Create the parameter
var parameter = command.CreateParameter(name, value, dbType, parameterDirection);
Expand Down Expand Up @@ -814,7 +814,7 @@ private static void EnsureAutomaticConversion(DbField dbField,
/// <returns></returns>
private static bool IsAutomaticConversion(DbField dbField) =>
(
Converter.ConversionType == ConversionType.Automatic ||
ApplicationConfiguration.Options.ConversionType == ConversionType.Automatic ||
dbField?.IsPrimary == true ||
dbField?.IsIdentity == true
) &&
Expand Down
2 changes: 1 addition & 1 deletion RepoDb.Core/RepoDb/Operations/DbConnection/DeleteAll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static partial class DbConnectionExtension
* Link: https://github.com/dotnet/SqlClient/issues/531
*/

private const int ParameterBatchCount = Constant.MaxParametersCount - 2;
private const int ParameterBatchCount = 2100 - 2;

#region DeleteAll<TEntity>

Expand Down
32 changes: 32 additions & 0 deletions RepoDb.Core/RepoDb/Options/ApplicationConfigurationOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using RepoDb.Enumerations;
using System.Data;
using System.Data.Common;

namespace RepoDb.Options
{
/// <summary>
/// A class that is being used to define the globalized configurations for the application.
/// </summary>
public class ApplicationConfigurationOptions
{
/// <summary>
/// Gets or sets the value that defines the conversion logic when converting an instance of <see cref="DbDataReader"/> into a .NET CLR class.
/// </summary>
public ConversionType ConversionType { get; set; } = ConversionType.Default;

/// <summary>
/// Gets or sets the default value of the batch operation size. The value defines on this property mainly affects the batch size of the InsertAll, MergeAll and UpdateAll operations.
/// </summary>
public int DefaultBatchOperationSize { get; set; } = Constant.DefaultBatchOperationSize;

/// <summary>
/// Gets of sets the default value of the cache expiration in minutes.
/// </summary>
public int DefaultCacheItemExpirationInMinutes { get; set; } = Constant.DefaultCacheItemExpirationInMinutes;

/// <summary>
/// Gets or sets the default equivalent <see cref="DbType"/> of an enumeration if it is being used as a parameter to the execution of any non-entity-based operations.
/// </summary>
public DbType EnumDefaultDatabaseType { get; set; } = DbType.String;
}
}
4 changes: 2 additions & 2 deletions RepoDb.Core/RepoDb/Reflection/Compiler/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,7 @@ internal static Expression GetClassPropertyParameterInfoIsDbNullFalseValueExpres
var valueExpression = (Expression)GetDbReaderGetValueExpression(readerParameterExpression,
readerGetValueMethod, readerField.Ordinal);
var targetTypeUnderlyingType = targetType.GetUnderlyingType();
var isAutomaticConversion = Converter.ConversionType == ConversionType.Automatic ||
var isAutomaticConversion = ApplicationConfiguration.Options.ConversionType == ConversionType.Automatic ||
targetTypeUnderlyingType == StaticType.TimeSpan ||
/* SQLite: Guid/String (Vice-Versa) : Enforce automatic conversion for the Primary/Identity fields */
readerField.DbField?.IsPrimary == true || readerField.DbField?.IsIdentity == true;
Expand Down Expand Up @@ -1460,7 +1460,7 @@ internal static Expression GetEntityInstancePropertyValueExpression(Expression e
var targetType = GetPropertyHandlerSetParameter(handlerInstance)?.ParameterType ?? dbField.Type;

// Auto-conversion Handling
if (Converter.ConversionType == ConversionType.Automatic || dbField?.IsPrimary == true || dbField?.IsIdentity == true)
if (ApplicationConfiguration.Options.ConversionType == ConversionType.Automatic || dbField?.IsPrimary == true || dbField?.IsIdentity == true)
{
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ internal static Action<DbCommand, object> GetPlainTypeToDbParametersCompiledFunc
ConvertExpressionToTypeExpression(createParameterExpression, StaticType.DbParameter)));

// Convert
if (Converter.ConversionType == ConversionType.Automatic && dbField?.Type != null)
if (ApplicationConfiguration.Options.ConversionType == ConversionType.Automatic && dbField?.Type != null)
{
valueType = dbField.Type.GetUnderlyingType();
valueExpression = ConvertExpressionWithAutomaticConversion(valueExpression, valueType);
Expand All @@ -92,7 +92,7 @@ internal static Action<DbCommand, object> GetPlainTypeToDbParametersCompiledFunc
paramProperty.GetDbType() ??
valueType.GetDbType() ??
(dbField != null ? new ClientTypeToDbTypeResolver().Resolve(dbField.Type) : null) ??
(DbType?)Converter.EnumDefaultDatabaseType;
(DbType?)ApplicationConfiguration.Options.EnumDefaultDatabaseType;
}
else
{
Expand Down

0 comments on commit 306d8b0

Please sign in to comment.