diff --git a/RepoDb.Core/RepoDb.Tests/RepoDb.IntegrationTests/TypeConversionsTest.cs b/RepoDb.Core/RepoDb.Tests/RepoDb.IntegrationTests/TypeConversionsTest.cs index 4b3bc965b..a269f33d4 100644 --- a/RepoDb.Core/RepoDb.Tests/RepoDb.IntegrationTests/TypeConversionsTest.cs +++ b/RepoDb.Core/RepoDb.Tests/RepoDb.IntegrationTests/TypeConversionsTest.cs @@ -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 diff --git a/RepoDb.Core/RepoDb/ApplicationConfiguration.cs b/RepoDb.Core/RepoDb/ApplicationConfiguration.cs new file mode 100644 index 000000000..c47f9b74a --- /dev/null +++ b/RepoDb.Core/RepoDb/ApplicationConfiguration.cs @@ -0,0 +1,30 @@ +using RepoDb.Options; + +namespace RepoDb +{ + /// + /// A static class that is being used to define the globalized configurations for the library. + /// + public static class ApplicationConfiguration + { + #region Methods + + /// + /// Setup the globalized configurations for the application. + /// + /// The option class that contains the value for the configurations. + public static void Setup(ApplicationConfigurationOptions options) => + Options = options; + + #endregion + + #region Properties + + /// + /// Gets the globalized configurations. + /// + public static ApplicationConfigurationOptions Options { get; private set; } = new ApplicationConfigurationOptions(); + + #endregion + } +} diff --git a/RepoDb.Core/RepoDb/Constant.cs b/RepoDb.Core/RepoDb/Constant.cs index 62a6cf8fa..211dd4f0e 100644 --- a/RepoDb.Core/RepoDb/Constant.cs +++ b/RepoDb.Core/RepoDb/Constant.cs @@ -14,10 +14,5 @@ public static class Constant /// The default value of the cache expiration in minutes. /// public const int DefaultCacheItemExpirationInMinutes = 180; - - /// - /// The maximum parameters of ADO.Net when executing a command. - /// - public const int MaxParametersCount = 2100; } } diff --git a/RepoDb.Core/RepoDb/Converter.cs b/RepoDb.Core/RepoDb/Converter.cs index ee20a2679..76d5a387f 100644 --- a/RepoDb.Core/RepoDb/Converter.cs +++ b/RepoDb.Core/RepoDb/Converter.cs @@ -16,12 +16,14 @@ public static class Converter /// Gets or sets the conversion type when converting the instance of object into its destination .NET CLR Types. /// The default value is . /// + [Obsolete("Use the definition of the ApplicationConfigurationOptions class instead.")] public static ConversionType ConversionType { get; set; } = ConversionType.Default; /// /// Gets or sets the default equivalent database type (of type ) of an enumeration if it is being used as a parameter to the /// execution of any non-entity-based operations. /// + [Obsolete("Use the definition of the ApplicationConfigurationOptions class instead.")] public static DbType EnumDefaultDatabaseType { get; set; } = DbType.String; #endregion @@ -34,7 +36,7 @@ public static class Converter /// The value to be checked for . /// The converted value. public static object NullToDbNull(object value) => - ReferenceEquals(null, value) ? DBNull.Value : value; + value is null ? DBNull.Value : value; /// /// Converts the value into null if the value is equals to . diff --git a/RepoDb.Core/RepoDb/Extensions/DbCommandExtension.cs b/RepoDb.Core/RepoDb/Extensions/DbCommandExtension.cs index 3623ecdfd..c58b86e16 100644 --- a/RepoDb.Core/RepoDb/Extensions/DbCommandExtension.cs +++ b/RepoDb.Core/RepoDb/Extensions/DbCommandExtension.cs @@ -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); @@ -814,7 +814,7 @@ private static void EnsureAutomaticConversion(DbField dbField, /// private static bool IsAutomaticConversion(DbField dbField) => ( - Converter.ConversionType == ConversionType.Automatic || + ApplicationConfiguration.Options.ConversionType == ConversionType.Automatic || dbField?.IsPrimary == true || dbField?.IsIdentity == true ) && diff --git a/RepoDb.Core/RepoDb/Operations/DbConnection/DeleteAll.cs b/RepoDb.Core/RepoDb/Operations/DbConnection/DeleteAll.cs index 48058d56d..d58613fcb 100644 --- a/RepoDb.Core/RepoDb/Operations/DbConnection/DeleteAll.cs +++ b/RepoDb.Core/RepoDb/Operations/DbConnection/DeleteAll.cs @@ -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 diff --git a/RepoDb.Core/RepoDb/Options/ApplicationConfigurationOptions.cs b/RepoDb.Core/RepoDb/Options/ApplicationConfigurationOptions.cs new file mode 100644 index 000000000..71d4763e3 --- /dev/null +++ b/RepoDb.Core/RepoDb/Options/ApplicationConfigurationOptions.cs @@ -0,0 +1,32 @@ +using RepoDb.Enumerations; +using System.Data; +using System.Data.Common; + +namespace RepoDb.Options +{ + /// + /// A class that is being used to define the globalized configurations for the application. + /// + public class ApplicationConfigurationOptions + { + /// + /// Gets or sets the value that defines the conversion logic when converting an instance of into a .NET CLR class. + /// + public ConversionType ConversionType { get; set; } = ConversionType.Default; + + /// + /// 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. + /// + public int DefaultBatchOperationSize { get; set; } = Constant.DefaultBatchOperationSize; + + /// + /// Gets of sets the default value of the cache expiration in minutes. + /// + public int DefaultCacheItemExpirationInMinutes { get; set; } = Constant.DefaultCacheItemExpirationInMinutes; + + /// + /// Gets or sets the default equivalent of an enumeration if it is being used as a parameter to the execution of any non-entity-based operations. + /// + public DbType EnumDefaultDatabaseType { get; set; } = DbType.String; + } +} diff --git a/RepoDb.Core/RepoDb/Reflection/Compiler/Compiler.cs b/RepoDb.Core/RepoDb/Reflection/Compiler/Compiler.cs index 08f431ee8..a452f00b8 100644 --- a/RepoDb.Core/RepoDb/Reflection/Compiler/Compiler.cs +++ b/RepoDb.Core/RepoDb/Reflection/Compiler/Compiler.cs @@ -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; @@ -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 { diff --git a/RepoDb.Core/RepoDb/Reflection/Compiler/PlainTypeToDbParameters.cs b/RepoDb.Core/RepoDb/Reflection/Compiler/PlainTypeToDbParameters.cs index 46ef27e9e..2aca309b9 100644 --- a/RepoDb.Core/RepoDb/Reflection/Compiler/PlainTypeToDbParameters.cs +++ b/RepoDb.Core/RepoDb/Reflection/Compiler/PlainTypeToDbParameters.cs @@ -78,7 +78,7 @@ internal static Action 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); @@ -92,7 +92,7 @@ internal static Action GetPlainTypeToDbParametersCompiledFunc paramProperty.GetDbType() ?? valueType.GetDbType() ?? (dbField != null ? new ClientTypeToDbTypeResolver().Resolve(dbField.Type) : null) ?? - (DbType?)Converter.EnumDefaultDatabaseType; + (DbType?)ApplicationConfiguration.Options.EnumDefaultDatabaseType; } else {