diff --git a/src/Common/ThrowHelper.cs b/src/Common/ThrowHelper.cs new file mode 100644 index 0000000..4d43fc8 --- /dev/null +++ b/src/Common/ThrowHelper.cs @@ -0,0 +1,43 @@ +namespace YeSql.Net; + +/// +/// Helper methods to efficiently throw exceptions. +/// +internal class ThrowHelper +{ + /// + /// Throws an if argument is null. + /// + /// + /// The reference type argument to validate as non-null. + /// + /// + /// The name of the parameter with which argument corresponds. + /// + /// + public static void ThrowIfNull(object argument, string paramName) + { + if (argument is null) + throw new ArgumentNullException(paramName); + } + + /// + /// Throws an exception if the contains elements with a null value, + /// an empty string, or consists only of white-space characters. + /// + /// + /// The collection argument to validate. + /// + /// + /// The name of the parameter with which argument corresponds. + /// + /// + public static void ThrowIfContainsNullOrWhiteSpace(IEnumerable argument, string paramName) + { + if (argument.ContainsNullOrWhiteSpace()) + { + var message = string.Format(ExceptionMessages.CollectionHasNullValueOrOnlyWhitespace, paramName); + throw new ArgumentException(message); + } + } +} diff --git a/src/Loader/YeSqlLoader.cs b/src/Loader/YeSqlLoader.cs index 90d2a8d..fd31a28 100644 --- a/src/Loader/YeSqlLoader.cs +++ b/src/Loader/YeSqlLoader.cs @@ -60,15 +60,11 @@ public ISqlCollection LoadFromDefaultDirectory() /// public ISqlCollection LoadFromFiles(params string[] sqlFiles) { - if (sqlFiles is null) - throw new ArgumentNullException(nameof(sqlFiles)); - + ThrowHelper.ThrowIfNull(sqlFiles, nameof(sqlFiles)); if (sqlFiles.IsEmpty()) return _parser.SqlStatements; - - if (sqlFiles.ContainsNullOrWhiteSpace()) - throw new ArgumentException(string.Format(ExceptionMessages.CollectionHasNullValueOrOnlyWhitespace, nameof(sqlFiles))); + ThrowHelper.ThrowIfContainsNullOrWhiteSpace(sqlFiles, nameof(sqlFiles)); foreach (var fileName in sqlFiles) { Result result = LoadFromFile(fileName); @@ -103,15 +99,11 @@ public ISqlCollection LoadFromFiles(params string[] sqlFiles) /// public ISqlCollection LoadFromDirectories(params string[] directories) { - if (directories is null) - throw new ArgumentNullException(nameof(directories)); - + ThrowHelper.ThrowIfNull(directories, nameof(directories)); if (directories.IsEmpty()) return _parser.SqlStatements; - if(directories.ContainsNullOrWhiteSpace()) - throw new ArgumentException(string.Format(ExceptionMessages.CollectionHasNullValueOrOnlyWhitespace, nameof(directories))); - + ThrowHelper.ThrowIfContainsNullOrWhiteSpace(directories, nameof(directories)); foreach (var directory in directories) { Result> result = LoadFromDirectory(directory); diff --git a/src/Parser/YeSqlParser.cs b/src/Parser/YeSqlParser.cs index bf1b5a3..00fc737 100644 --- a/src/Parser/YeSqlParser.cs +++ b/src/Parser/YeSqlParser.cs @@ -85,9 +85,7 @@ public ISqlCollection ParseAndThrow(string source) /// public ISqlCollection Parse(string source, out YeSqlValidationResult validationResult) { - if(source is null) - throw new ArgumentNullException(nameof(source)); - + ThrowHelper.ThrowIfNull(source, nameof(source)); validationResult = ValidationResult; if(string.IsNullOrWhiteSpace(source)) { diff --git a/src/Reader/YeSqlDictionary.cs b/src/Reader/YeSqlDictionary.cs index 73b243d..61fd5cf 100644 --- a/src/Reader/YeSqlDictionary.cs +++ b/src/Reader/YeSqlDictionary.cs @@ -18,9 +18,7 @@ public string this[string tagName] { get { - if(tagName is null) - throw new ArgumentNullException(nameof(tagName)); - + ThrowHelper.ThrowIfNull(tagName, nameof(tagName)); if(_sqlStatements.TryGetValue(tagName, out var sqlStatement)) return sqlStatement; @@ -35,9 +33,7 @@ public string this[string tagName] /// public bool TryGetStatement(string tagName, out string sqlStatement) { - if (tagName is null) - throw new ArgumentNullException(nameof(tagName)); - + ThrowHelper.ThrowIfNull(tagName, nameof (tagName)); return _sqlStatements.TryGetValue(tagName, out sqlStatement); }