@@ -10,10 +10,10 @@ public static class PropertiesHelper
10
10
{
11
11
public static bool GetBoolean ( string property , IDictionary < string , string > properties , bool defaultValue )
12
12
{
13
- string toParse ;
14
- properties . TryGetValue ( property , out toParse ) ;
15
- bool result ;
16
- return bool . TryParse ( toParse , out result ) ? result : defaultValue ;
13
+ if ( properties == null )
14
+ throw new ArgumentNullException ( nameof ( properties ) ) ;
15
+ properties . TryGetValue ( property , out var toParse ) ;
16
+ return bool . TryParse ( toParse , out var result ) ? result : defaultValue ;
17
17
}
18
18
19
19
public static bool GetBoolean ( string property , IDictionary < string , string > properties )
@@ -23,33 +23,34 @@ public static bool GetBoolean(string property, IDictionary<string, string> prope
23
23
24
24
public static byte ? GetByte ( string property , IDictionary < string , string > properties , byte ? defaultValue )
25
25
{
26
- string toParse ;
27
- properties . TryGetValue ( property , out toParse ) ;
28
- byte result ;
29
- return byte . TryParse ( toParse , out result ) ? result : defaultValue ;
26
+ if ( properties == null )
27
+ throw new ArgumentNullException ( nameof ( properties ) ) ;
28
+ properties . TryGetValue ( property , out var toParse ) ;
29
+ return byte . TryParse ( toParse , out var result ) ? result : defaultValue ;
30
30
}
31
31
32
32
public static int GetInt32 ( string property , IDictionary < string , string > properties , int defaultValue )
33
33
{
34
- string toParse ;
35
- properties . TryGetValue ( property , out toParse ) ;
36
- int result ;
37
- return int . TryParse ( toParse , out result ) ? result : defaultValue ;
34
+ if ( properties == null )
35
+ throw new ArgumentNullException ( nameof ( properties ) ) ;
36
+ properties . TryGetValue ( property , out var toParse ) ;
37
+ return int . TryParse ( toParse , out var result ) ? result : defaultValue ;
38
38
}
39
39
40
40
public static long GetInt64 ( string property , IDictionary < string , string > properties , long defaultValue )
41
41
{
42
- string toParse ;
43
- properties . TryGetValue ( property , out toParse ) ;
44
- long result ;
45
- return long . TryParse ( toParse , out result ) ? result : defaultValue ;
42
+ if ( properties == null )
43
+ throw new ArgumentNullException ( nameof ( properties ) ) ;
44
+ properties . TryGetValue ( property , out var toParse ) ;
45
+ return long . TryParse ( toParse , out var result ) ? result : defaultValue ;
46
46
}
47
47
48
48
public static string GetString ( string property , IDictionary < string , string > properties , string defaultValue )
49
49
{
50
- string value ;
51
- properties . TryGetValue ( property , out value ) ;
52
- if ( value == string . Empty )
50
+ if ( properties == null )
51
+ throw new ArgumentNullException ( nameof ( properties ) ) ;
52
+ properties . TryGetValue ( property , out var value ) ;
53
+ if ( value == string . Empty )
53
54
{
54
55
value = null ;
55
56
}
@@ -58,10 +59,11 @@ public static string GetString(string property, IDictionary<string, string> prop
58
59
59
60
public static IDictionary < string , string > ToDictionary ( string property , string delim , IDictionary < string , string > properties )
60
61
{
61
- IDictionary < string , string > map = new Dictionary < string , string > ( ) ;
62
+ if ( properties == null )
63
+ throw new ArgumentNullException ( nameof ( properties ) ) ;
64
+ var map = new Dictionary < string , string > ( ) ;
62
65
63
- string propValue ;
64
- if ( properties . TryGetValue ( property , out propValue ) )
66
+ if ( properties . TryGetValue ( property , out var propValue ) )
65
67
{
66
68
var tokens = new StringTokenizer ( propValue , delim , false ) ;
67
69
using ( var en = tokens . GetEnumerator ( ) )
@@ -84,28 +86,32 @@ public static IDictionary<string, string> ToDictionary(string property, string d
84
86
/// <param name="property">The configuration property name.</param>
85
87
/// <param name="properties">The configuration properties.</param>
86
88
/// <param name="defaultType">The default type to instantiate.</param>
87
- /// <returns>The instance of the <typeparamref name="TService"/> type.</returns>
88
- public static TService GetInstance < TService > ( string property , IDictionary < string , string > properties , System . Type defaultType )
89
+ /// <returns>The instance of the <typeparamref name="TService"/> type, or <see langword="null" /> if none is
90
+ /// configured and <paramref name="defaultType"/> is <see langword="null" />.</returns>
91
+ public static TService GetInstance < TService > (
92
+ string property , IDictionary < string , string > properties , System . Type defaultType ) where TService : class
89
93
{
90
94
var className = GetString ( property , properties , null ) ;
95
+ System . Type type = null ;
91
96
try
92
97
{
93
- if ( className != null )
94
- {
95
- return ( TService ) Cfg . Environment . ServiceProvider . GetInstance ( ReflectHelper . ClassForName ( className ) ) ;
96
- }
98
+ type = className != null
99
+ ? ReflectHelper . ClassForName ( className )
100
+ : typeof ( TService ) ;
97
101
98
- var instance = ( TService ) Cfg . Environment . ServiceProvider . GetService ( typeof ( TService ) ) ;
102
+ var instance = ( TService ) Cfg . Environment . ServiceProvider . GetService ( type ) ;
99
103
if ( instance != null )
100
104
{
101
105
return instance ;
102
106
}
103
107
104
- return ( TService ) Cfg . Environment . ServiceProvider . GetInstance ( defaultType ) ;
108
+ type = defaultType ;
109
+ return defaultType != null ? ( TService ) Cfg . Environment . ServiceProvider . GetInstance ( defaultType ) : null ;
105
110
}
106
111
catch ( Exception e )
107
112
{
108
- throw new HibernateException ( $ "could not instantiate { typeof ( TService ) . Name } : { className ?? defaultType . AssemblyQualifiedName } ", e ) ;
113
+ throw new HibernateException (
114
+ $ "Could not instantiate { typeof ( TService ) . Name } : { type ? . AssemblyQualifiedName ?? className } ", e ) ;
109
115
}
110
116
}
111
117
}
0 commit comments