2 // System.Data.Common.DbProviderFactories.cs
5 // Sureshkumar T (tsureshkumar@novell.com)
6 // Tim Coleman (tim@timcoleman.com)
8 // Copyright (C) Tim Coleman, 2003
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System
.Threading
;
35 using System
.Reflection
;
36 using System
.Collections
;
37 using System
.Configuration
;
39 namespace System
.Data
.Common
{
40 public static class DbProviderFactories
42 private static object configEntries
; // DataSet
44 internal const string CONFIG_SECTION_NAME
= "system.data";
45 internal const string CONFIG_SEC_TABLE_NAME
= "DbProviderFactories";
49 public static DbProviderFactory
GetFactory (DataRow providerRow
)
51 string assemblyType
= (string) providerRow
["AssemblyQualifiedName"];
52 Type type
= Type
.GetType (assemblyType
, false, true);
53 if (type
!= null && type
.IsSubclassOf (typeof (DbProviderFactory
))) {
54 // Provider factories are singletons with Instance field having
56 FieldInfo field
= type
.GetField ("Instance", BindingFlags
.Public
|
59 return field
.GetValue (null) as DbProviderFactory
;
63 throw new ConfigurationErrorsException("Failed to find or load the registered .Net Framework Data Provider.");
66 public static DbProviderFactory
GetFactory (string providerInvariantName
)
68 if (providerInvariantName
== null)
69 throw new ArgumentNullException ("providerInvariantName");
71 DataTable table
= GetFactoryClasses ();
73 DataRow row
= table
.Rows
.Find (providerInvariantName
);
75 return GetFactory (row
);
78 throw new ConfigurationErrorsException (String
.Format("Failed to find or load the registered .Net Framework Data Provider '{0}'.", providerInvariantName
));
82 public static DbProviderFactory
GetFactory (DbConnection connection
)
84 if (connection
== null)
85 throw new ArgumentNullException ("connection");
87 return connection
.DbProviderFactory
;
91 public static DataTable
GetFactoryClasses ()
93 DataSet ds
= GetConfigEntries ();
94 DataTable table
= ds
!= null ? ds
.Tables
[CONFIG_SEC_TABLE_NAME
] : null;
96 table
= table
.Copy (); // avoid modifications by user
100 #endregion // Methods
102 #region Internal Methods
104 internal static DataSet
GetConfigEntries ()
106 if (configEntries
!= null)
107 return configEntries
as DataSet
;
109 DataSet ds
= (DataSet
) ConfigurationManager
.GetSection (CONFIG_SECTION_NAME
);
110 Interlocked
.CompareExchange (ref configEntries
, ds
, null);
111 return configEntries
as DataSet
;
114 #endregion Internal Methods