forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReflectionBasedDriver.cs
77 lines (69 loc) · 3.36 KB
/
ReflectionBasedDriver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Data.Common;
using NHibernate.Util;
namespace NHibernate.Driver
{
public abstract class ReflectionBasedDriver : DriverBase
{
protected const string ReflectionTypedProviderExceptionMessageTemplate = "The DbCommand and DbConnection implementation in the assembly {0} could not be found. "
+ "Ensure that the assembly {0} is located in the application directory or in the Global "
+ "Assembly Cache. If the assembly is in the GAC, use <qualifyAssembly/> element in the "
+ "application configuration file to specify the full name of the assembly.";
private readonly IDriveConnectionCommandProvider connectionCommandProvider;
/// <summary>
/// If the driver use a third party driver (not a .Net Framework DbProvider), its assembly version.
/// </summary>
protected Version DriverVersion { get; }
/// <summary>
/// Initializes a new instance of <see cref="ReflectionBasedDriver" /> with
/// type names that are loaded from the specified assembly.
/// </summary>
/// <param name="driverAssemblyName">Assembly to load the types from.</param>
/// <param name="connectionTypeName">Connection type name.</param>
/// <param name="commandTypeName">Command type name.</param>
protected ReflectionBasedDriver(string driverAssemblyName, string connectionTypeName, string commandTypeName)
: this(null, driverAssemblyName, connectionTypeName, commandTypeName)
{
}
/// <summary>
/// Initializes a new instance of <see cref="ReflectionBasedDriver" /> with
/// type names that are loaded from the specified assembly.
/// </summary>
/// <param name="providerInvariantName">The Invariant name of a provider.</param>
/// <param name="driverAssemblyName">Assembly to load the types from.</param>
/// <param name="connectionTypeName">Connection type name.</param>
/// <param name="commandTypeName">Command type name.</param>
protected ReflectionBasedDriver(string providerInvariantName, string driverAssemblyName, string connectionTypeName, string commandTypeName)
{
// Try to get the types from an already loaded assembly
var connectionType = ReflectHelper.TypeFromAssembly(connectionTypeName, driverAssemblyName, false);
var commandType = ReflectHelper.TypeFromAssembly(commandTypeName, driverAssemblyName, false);
if (connectionType == null || commandType == null)
{
#if NETFX || NETSTANDARD2_1_OR_GREATER
if (string.IsNullOrEmpty(providerInvariantName))
{
#endif
throw new HibernateException(string.Format(ReflectionTypedProviderExceptionMessageTemplate, driverAssemblyName));
#if NETFX || NETSTANDARD2_1_OR_GREATER
}
var factory = DbProviderFactories.GetFactory(providerInvariantName);
connectionCommandProvider = new DbProviderFactoryDriveConnectionCommandProvider(factory);
#endif
}
else
{
connectionCommandProvider = new ReflectionDriveConnectionCommandProvider(connectionType, commandType);
DriverVersion = connectionType.Assembly.GetName().Version;
}
}
public override DbConnection CreateConnection()
{
return connectionCommandProvider.CreateConnection();
}
public override DbCommand CreateCommand()
{
return connectionCommandProvider.CreateCommand();
}
}
}