@@ -7,7 +7,7 @@ namespace codeRR.Server.Infrastructure.Configuration
77 /// <summary>
88 /// Moves otherwise repeated conversions to a single place.
99 /// </summary>
10- public static class DictionaryExtensions
10+ public static class ConfigDictionaryExtensions
1111 {
1212 /// <summary>
1313 /// Convert dictionary item to a boolean.
@@ -17,19 +17,25 @@ public static class DictionaryExtensions
1717 /// <returns>Value</returns>
1818 /// <exception cref="ArgumentException">If key is not present. Key name is included in the exception message.</exception>
1919 /// <exception cref="FormatException">Value is not a boolean. Includes key name and source value in the exception message.</exception>
20- public static bool GetBoolean ( this IDictionary < string , string > dictionary , string name )
20+ public static bool GetBoolean ( this IDictionary < string , string > dictionary , string name , bool ? defaultValue = false )
2121 {
2222 if ( dictionary == null ) throw new ArgumentNullException ( "dictionary" ) ;
2323 if ( name == null ) throw new ArgumentNullException ( "name" ) ;
24- string value ;
25- if ( ! dictionary . TryGetValue ( name , out value ) )
26- throw new ArgumentException ( string . Format ( "Failed to find key '{0}' in dictionary." , name ) ) ;
27-
28- bool boolValue ;
29- if ( ! bool . TryParse ( value , out boolValue ) )
30- throw new FormatException ( string . Format ( "Failed to convert '{0}' from value '{1}' to a boolean." , name ,
31- value ) ) ;
32- return boolValue ;
24+
25+ if ( ! dictionary . TryGetValue ( name , out var value ) )
26+ {
27+ if ( defaultValue != null )
28+ return defaultValue . Value ;
29+ throw new ArgumentException ( $ "Failed to find key '{ name } ' in dictionary.") ;
30+ }
31+
32+ if ( bool . TryParse ( value , out var boolValue ) )
33+ return boolValue ;
34+
35+ if ( defaultValue != null )
36+ return defaultValue . Value ;
37+
38+ throw new FormatException ( $ "Failed to convert '{ name } ' from value '{ value } ' to a boolean.") ;
3339 }
3440
3541 /// <summary>
@@ -41,19 +47,26 @@ public static bool GetBoolean(this IDictionary<string, string> dictionary, strin
4147 /// <exception cref="ArgumentException">If key is not present. Key name is included in the exception message.</exception>
4248 /// <exception cref="FormatException">Value is not a boolean. Includes key name and source value in the exception message.</exception>
4349 [ SuppressMessage ( "Microsoft.Naming" , "CA1720:IdentifiersShouldNotContainTypeNames" , MessageId = "integer" ) ]
44- public static int GetInteger ( this IDictionary < string , string > dictionary , string name )
50+ public static int GetInteger ( this IDictionary < string , string > dictionary , string name , int ? defaultValue = 0 )
4551 {
4652 if ( dictionary == null ) throw new ArgumentNullException ( "dictionary" ) ;
4753 if ( name == null ) throw new ArgumentNullException ( "name" ) ;
48- string value ;
49- if ( ! dictionary . TryGetValue ( name , out value ) )
50- throw new ArgumentException ( string . Format ( "Failed to find key '{0}' in dictionary." , name ) ) ;
51-
52- int intValue ;
53- if ( ! int . TryParse ( value , out intValue ) )
54- throw new FormatException ( string . Format ( "Failed to convert '{0}' from value '{1}' to an integer." ,
55- name , value ) ) ;
56- return intValue ;
54+
55+ if ( ! dictionary . TryGetValue ( name , out var value ) )
56+ {
57+ if ( defaultValue != null )
58+ return defaultValue . Value ;
59+ throw new ArgumentException ( $ "Failed to find key '{ name } ' in dictionary.") ;
60+ }
61+
62+
63+ if ( int . TryParse ( value , out var intValue ) )
64+ return intValue ;
65+
66+ if ( defaultValue != null )
67+ return defaultValue . Value ;
68+
69+ throw new FormatException ( $ "Failed to convert '{ name } ' from value '{ value } ' to an integer.") ;
5770 }
5871
5972 /// <summary>
@@ -63,15 +76,18 @@ public static int GetInteger(this IDictionary<string, string> dictionary, string
6376 /// <param name="name">Key</param>
6477 /// <returns>Value</returns>
6578 /// <exception cref="ArgumentException">If key is not present. Key name is included in the exception message.</exception>
66- public static string GetString ( this IDictionary < string , string > dictionary , string name )
79+ public static string GetString ( this IDictionary < string , string > dictionary , string name , bool requireParameter = true )
6780 {
6881 if ( dictionary == null ) throw new ArgumentNullException ( "dictionary" ) ;
6982 if ( name == null ) throw new ArgumentNullException ( "name" ) ;
70- string value ;
71- if ( ! dictionary . TryGetValue ( name , out value ) )
72- throw new ArgumentException ( string . Format ( "Failed to find key '{0}' in dictionary." , name ) ) ;
7383
74- return value ;
84+ if ( dictionary . TryGetValue ( name , out var value ) )
85+ return value ;
86+
87+ if ( requireParameter )
88+ throw new ArgumentException ( $ "Failed to find key '{ name } ' in dictionary.") ;
89+
90+ return null ;
7591 }
7692
7793 /// <summary>
@@ -86,8 +102,9 @@ public static string GetString(this IDictionary<string, string> dictionary, stri
86102 {
87103 if ( dictionary == null ) throw new ArgumentNullException ( "dictionary" ) ;
88104 if ( name == null ) throw new ArgumentNullException ( "name" ) ;
89- string value ;
90- return ! dictionary . TryGetValue ( name , out value ) ? defaultValue : value ;
105+ return ! dictionary . TryGetValue ( name , out var value )
106+ ? defaultValue
107+ : value ;
91108 }
92109 }
93110}
0 commit comments