Skip to content

Commit 4f5cae4

Browse files
fixup! Replace IObjectsFactory with IServiceProvider interface
Refactor many instantiations
1 parent 98485e0 commit 4f5cae4

File tree

6 files changed

+81
-202
lines changed

6 files changed

+81
-202
lines changed

src/NHibernate/Cfg/SettingsFactory.cs

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -396,40 +396,12 @@ private static ITransactionFactory CreateTransactionFactory(IDictionary<string,
396396

397397
private static IQueryModelRewriterFactory CreateQueryModelRewriterFactory(IDictionary<string, string> properties)
398398
{
399-
var className = PropertiesHelper.GetString(Environment.QueryModelRewriterFactory, properties, null);
400-
401-
if (className == null)
402-
{
403-
try
404-
{
405-
var instance =
406-
(IQueryModelRewriterFactory)
407-
Environment.ServiceProvider.GetService(typeof(IQueryModelRewriterFactory));
408-
if (instance == null)
409-
{
410-
return null;
411-
}
412-
log.Info("Query model rewriter factory factory: {0}", instance.GetType().AssemblyQualifiedName);
413-
return instance;
414-
}
415-
catch (Exception cnfe)
416-
{
417-
throw new HibernateException("could not instantiate IQueryModelRewriterFactory", cnfe);
418-
}
419-
}
420-
421-
log.Info("Query model rewriter factory factory: {0}", className);
422-
423-
try
424-
{
425-
return
426-
(IQueryModelRewriterFactory)
427-
Environment.ServiceProvider.GetInstance(ReflectHelper.ClassForName(className));
428-
}
429-
catch (Exception cnfe)
430-
{
431-
throw new HibernateException("could not instantiate IQueryModelRewriterFactory: " + className, cnfe);
432-
}
399+
var instance = PropertiesHelper.GetInstance<IQueryModelRewriterFactory>(
400+
Environment.QueryModelRewriterFactory,
401+
properties,
402+
null);
403+
log.Info("Query model rewriter factory factory: '{0}'", instance?.GetType().AssemblyQualifiedName ?? "none");
404+
return instance;
433405
}
434406
}
435407
}

src/NHibernate/Connection/ConnectionProvider.cs

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -94,35 +94,16 @@ protected virtual string GetNamedConnectionString(IDictionary<string, string> se
9494
/// </exception>
9595
protected virtual void ConfigureDriver(IDictionary<string, string> settings)
9696
{
97-
if (!settings.TryGetValue(Environment.ConnectionDriver, out var driverClass))
98-
{
99-
try
100-
{
101-
driver = (IDriver) Environment.ServiceProvider.GetService(typeof(IDriver));
102-
if (driver != null)
103-
{
104-
driver.Configure(settings);
105-
return;
106-
}
107-
}
108-
catch (Exception e)
109-
{
110-
throw new HibernateException($"Could not create the driver from {typeof(IDriver)}.", e);
111-
}
97+
driver = PropertiesHelper.GetInstance<IDriver>(
98+
Environment.ConnectionDriver,
99+
settings,
100+
null);
101+
if (driver == null)
112102
throw new HibernateException(
113-
$"The {Environment.ConnectionDriver} must be specified in the NHibernate configuration section.");
114-
}
103+
$"The {Environment.ConnectionDriver} must be specified in the NHibernate configuration section," +
104+
"or Environment.ServiceProvider must be setup in order to resolve it.");
115105

116-
try
117-
{
118-
driver =
119-
(IDriver) Environment.ServiceProvider.GetInstance(ReflectHelper.ClassForName(driverClass));
120-
driver.Configure(settings);
121-
}
122-
catch (Exception e)
123-
{
124-
throw new HibernateException($"Could not create the driver from {driverClass}.", e);
125-
}
106+
driver.Configure(settings);
126107
}
127108

128109
/// <summary>
Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Collections.Generic;
32

43
using NHibernate.Util;
@@ -13,48 +12,20 @@ public static class ConnectionProviderFactory
1312
{
1413
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(ConnectionProviderFactory));
1514

16-
// cannot be instantiated
1715
public static IConnectionProvider NewConnectionProvider(IDictionary<string, string> settings)
1816
{
19-
IConnectionProvider connections;
20-
if (settings.TryGetValue(Environment.ConnectionProvider, out var providerClass))
21-
{
22-
try
23-
{
24-
log.Info("Initializing connection provider: {0}", providerClass);
25-
connections =
26-
(IConnectionProvider)
27-
Environment.ServiceProvider.GetInstance(ReflectHelper.ClassForName(providerClass));
28-
}
29-
catch (Exception e)
30-
{
31-
log.Fatal(e, "Could not instantiate connection provider");
32-
throw new HibernateException("Could not instantiate connection provider: " + providerClass, e);
33-
}
34-
}
35-
else if (settings.ContainsKey(Environment.ConnectionString) || settings.ContainsKey(Environment.ConnectionStringName))
36-
{
37-
connections = new DriverConnectionProvider();
38-
}
39-
else
40-
{
41-
try
42-
{
43-
connections = (IConnectionProvider) Environment.ServiceProvider.GetService(typeof(IConnectionProvider));
44-
}
45-
catch (Exception e)
46-
{
47-
log.Fatal(e, "Could not instantiate connection provider");
48-
throw new HibernateException($"Could not instantiate connection provider: {typeof(IConnectionProvider)}", e);
49-
}
50-
if (connections == null)
51-
{
52-
log.Info("No connection provider specified, UserSuppliedConnectionProvider will be used.");
53-
connections = new UserSuppliedConnectionProvider();
54-
}
55-
}
56-
connections.Configure(settings);
57-
return connections;
17+
var defaultConnectionProvider =
18+
settings.ContainsKey(Environment.ConnectionString) ||
19+
settings.ContainsKey(Environment.ConnectionStringName)
20+
? typeof(DriverConnectionProvider)
21+
: typeof(UserSuppliedConnectionProvider);
22+
var connectionProvider = PropertiesHelper.GetInstance<IConnectionProvider>(
23+
Environment.ConnectionProvider,
24+
settings,
25+
defaultConnectionProvider);
26+
log.Info("Connection provider: '{0}'", connectionProvider.GetType().AssemblyQualifiedName);
27+
connectionProvider.Configure(settings);
28+
return connectionProvider;
5829
}
5930
}
6031
}

src/NHibernate/Dialect/Dialect.cs

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,7 @@ protected Dialect()
146146
/// <returns> The specified Dialect </returns>
147147
public static Dialect GetDialect()
148148
{
149-
string dialectName;
150-
try
151-
{
152-
dialectName = Environment.Properties[Environment.Dialect];
153-
}
154-
catch (Exception e)
155-
{
156-
throw new HibernateException("The dialect was not set. Set the property 'dialect'.", e);
157-
}
158-
return InstantiateDialect(dialectName, Environment.Properties);
149+
return GetDialect(Environment.Properties);
159150
}
160151

161152
/// <summary>
@@ -169,42 +160,19 @@ public static Dialect GetDialect(IDictionary<string, string> props)
169160
{
170161
if (props == null)
171162
throw new ArgumentNullException(nameof(props));
172-
if (props.TryGetValue(Environment.Dialect, out var dialectName) == false)
173-
{
174-
try
175-
{
176-
var dialect = (Dialect) Environment.ServiceProvider.GetService(typeof(Dialect));
177-
if (dialect != null)
178-
{
179-
return dialect;
180-
}
181-
}
182-
catch (Exception e)
183-
{
184-
throw new HibernateException($"Could not instantiate dialect class {typeof(Dialect)}", e);
185-
}
163+
if (props.TryGetValue(Environment.Dialect, out var dialectName) && dialectName == null)
164+
props = Environment.Properties;
165+
166+
var dialect = PropertiesHelper.GetInstance<Dialect>(
167+
Environment.Dialect,
168+
props,
169+
null);
170+
171+
if (dialect == null)
186172
throw new InvalidOperationException("Could not find the dialect in the configuration");
187-
}
188-
if (dialectName == null)
189-
{
190-
return GetDialect();
191-
}
192-
193-
return InstantiateDialect(dialectName, props);
194-
}
195173

196-
private static Dialect InstantiateDialect(string dialectName, IDictionary<string, string> props)
197-
{
198-
try
199-
{
200-
var dialect = (Dialect) Environment.ServiceProvider.GetInstance(ReflectHelper.ClassForName(dialectName));
201-
dialect.Configure(props);
202-
return dialect;
203-
}
204-
catch (Exception e)
205-
{
206-
throw new HibernateException("Could not instantiate dialect class " + dialectName, e);
207-
}
174+
dialect.Configure(props);
175+
return dialect;
208176
}
209177

210178
/// <summary>
Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Collections.Generic;
32

43
using NHibernate.Util;
@@ -12,30 +11,12 @@ public sealed class LinqToHqlGeneratorsRegistryFactory
1211

1312
public static ILinqToHqlGeneratorsRegistry CreateGeneratorsRegistry(IDictionary<string, string> properties)
1413
{
15-
if (properties.TryGetValue(Environment.LinqToHqlGeneratorsRegistry, out var registry))
16-
{
17-
try
18-
{
19-
log.Info("Initializing LinqToHqlGeneratorsRegistry: {0}", registry);
20-
return (ILinqToHqlGeneratorsRegistry) Environment.ServiceProvider.GetInstance(ReflectHelper.ClassForName(registry));
21-
}
22-
catch (Exception e)
23-
{
24-
log.Fatal(e, "Could not instantiate LinqToHqlGeneratorsRegistry");
25-
throw new HibernateException("Could not instantiate LinqToHqlGeneratorsRegistry: " + registry, e);
26-
}
27-
}
28-
try
29-
{
30-
return (ILinqToHqlGeneratorsRegistry)
31-
Environment.ServiceProvider.GetService(typeof(ILinqToHqlGeneratorsRegistry)) ??
32-
new DefaultLinqToHqlGeneratorsRegistry();
33-
}
34-
catch (Exception e)
35-
{
36-
log.Fatal(e, "Could not instantiate LinqToHqlGeneratorsRegistry");
37-
throw new HibernateException($"Could not instantiate LinqToHqlGeneratorsRegistry: {typeof(ILinqToHqlGeneratorsRegistry)}", e);
38-
}
14+
var instance = PropertiesHelper.GetInstance<ILinqToHqlGeneratorsRegistry>(
15+
Environment.LinqToHqlGeneratorsRegistry,
16+
properties,
17+
typeof(DefaultLinqToHqlGeneratorsRegistry));
18+
log.Info("LinqToHqlGeneratorsRegistry: '{0}'", instance.GetType().AssemblyQualifiedName);
19+
return instance;
3920
}
4021
}
4122
}

src/NHibernate/Util/PropertiesHelper.cs

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ public static class PropertiesHelper
1010
{
1111
public static bool GetBoolean(string property, IDictionary<string, string> properties, bool defaultValue)
1212
{
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;
1717
}
1818

1919
public static bool GetBoolean(string property, IDictionary<string, string> properties)
@@ -23,33 +23,34 @@ public static bool GetBoolean(string property, IDictionary<string, string> prope
2323

2424
public static byte? GetByte(string property, IDictionary<string, string> properties, byte? defaultValue)
2525
{
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;
3030
}
3131

3232
public static int GetInt32(string property, IDictionary<string, string> properties, int defaultValue)
3333
{
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;
3838
}
3939

4040
public static long GetInt64(string property, IDictionary<string, string> properties, long defaultValue)
4141
{
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;
4646
}
4747

4848
public static string GetString(string property, IDictionary<string, string> properties, string defaultValue)
4949
{
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)
5354
{
5455
value = null;
5556
}
@@ -58,10 +59,11 @@ public static string GetString(string property, IDictionary<string, string> prop
5859

5960
public static IDictionary<string, string> ToDictionary(string property, string delim, IDictionary<string, string> properties)
6061
{
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>();
6265

63-
string propValue;
64-
if (properties.TryGetValue(property, out propValue))
66+
if (properties.TryGetValue(property, out var propValue))
6567
{
6668
var tokens = new StringTokenizer(propValue, delim, false);
6769
using (var en = tokens.GetEnumerator())
@@ -84,28 +86,32 @@ public static IDictionary<string, string> ToDictionary(string property, string d
8486
/// <param name="property">The configuration property name.</param>
8587
/// <param name="properties">The configuration properties.</param>
8688
/// <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
8993
{
9094
var className = GetString(property, properties, null);
95+
System.Type type = null;
9196
try
9297
{
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);
97101

98-
var instance = (TService) Cfg.Environment.ServiceProvider.GetService(typeof(TService));
102+
var instance = (TService) Cfg.Environment.ServiceProvider.GetService(type);
99103
if (instance != null)
100104
{
101105
return instance;
102106
}
103107

104-
return (TService) Cfg.Environment.ServiceProvider.GetInstance(defaultType);
108+
type = defaultType;
109+
return defaultType != null ? (TService) Cfg.Environment.ServiceProvider.GetInstance(defaultType) : null;
105110
}
106111
catch (Exception e)
107112
{
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);
109115
}
110116
}
111117
}

0 commit comments

Comments
 (0)