| 
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.  | 
3 | 3 | // See the LICENSE file in the project root for more information.  | 
4 | 4 | 
 
  | 
 | 5 | +using System.Data.Common;  | 
5 | 6 | using Microsoft.Data.Sql;  | 
 | 7 | + | 
 | 8 | +#if NETFRAMEWORK  | 
6 | 9 | using System;  | 
7 |  | -using System.Data.Common;  | 
 | 10 | +using System.Reflection;  | 
8 | 11 | using System.Security.Permissions;  | 
9 | 12 | using System.Security;  | 
10 |  | -using Microsoft.Data.Common;  | 
 | 13 | +#endif  | 
11 | 14 | 
 
  | 
12 | 15 | namespace Microsoft.Data.SqlClient  | 
13 | 16 | {  | 
14 | 17 |     /// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlClientFactory.xml' path='docs/members[@name="SqlClientFactory"]/SqlClientFactory/*'/>  | 
 | 18 | +    #if NETFRAMEWORK  | 
 | 19 | +    public sealed class SqlClientFactory : DbProviderFactory, IServiceProvider  | 
 | 20 | +    #else  | 
15 | 21 |     public sealed class SqlClientFactory : DbProviderFactory  | 
16 |  | -#if NETFRAMEWORK  | 
17 |  | -        , IServiceProvider  | 
18 |  | -#endif  | 
 | 22 | +    #endif  | 
19 | 23 |     {  | 
 | 24 | +        #if NETFRAMEWORK  | 
 | 25 | +        #region Constants / Member Variables  | 
 | 26 | + | 
 | 27 | +        private const string ExtensionAssemblyRef =   | 
 | 28 | +            "System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=" + AssemblyRef.EcmaPublicKey;  | 
 | 29 | + | 
 | 30 | +        private const string MicrosoftDataSqlClientSqlProviderServicesTypeName =  | 
 | 31 | +            "Microsoft.Data.SqlClient.SQLProviderServices, " + ExtensionAssemblyRef;  | 
 | 32 | +          | 
 | 33 | +        private const string SystemDataCommonDbProviderServicesTypeName =  | 
 | 34 | +            "System.Data.Common.DbProviderServices, " + ExtensionAssemblyRef;  | 
20 | 35 | 
 
  | 
 | 36 | +        private static readonly Lazy<object> MicrosoftDataSqlClientProviderServicesInstance =  | 
 | 37 | +            new(static () =>  | 
 | 38 | +            {  | 
 | 39 | +                FieldInfo instanceFieldInfo = MicrosoftDataSqlClientSqlProviderServicesType.Value?.GetField(  | 
 | 40 | +                    "Instance",  | 
 | 41 | +                    BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static);  | 
 | 42 | +                return instanceFieldInfo?.GetValue(null);  | 
 | 43 | +            });  | 
 | 44 | +          | 
 | 45 | +        private static readonly Lazy<Type> MicrosoftDataSqlClientSqlProviderServicesType =  | 
 | 46 | +            new (static () => Type.GetType(MicrosoftDataSqlClientSqlProviderServicesTypeName, throwOnError: false));  | 
 | 47 | + | 
 | 48 | +        private static readonly Lazy<Type> SystemDataCommonDbProviderServicesType =  | 
 | 49 | +            new(static () => Type.GetType(SystemDataCommonDbProviderServicesTypeName, throwOnError: false));  | 
 | 50 | +          | 
 | 51 | +        #endregion  | 
 | 52 | +        #endif  | 
 | 53 | +          | 
21 | 54 |         /// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlClientFactory.xml' path='docs/members[@name="SqlClientFactory"]/Instance/*'/>  | 
22 | 55 |         public static readonly SqlClientFactory Instance = new SqlClientFactory();  | 
23 | 56 | 
 
  | 
24 | 57 |         private SqlClientFactory()  | 
25 | 58 |         {  | 
26 | 59 |         }  | 
 | 60 | +          | 
 | 61 | +        #if NET  | 
 | 62 | +        /// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlClientFactory.xml' path='docs/members[@name="SqlClientFactory"]/CanCreateBatch/*'/>  | 
 | 63 | +        public override bool CanCreateBatch => true;  | 
 | 64 | + | 
 | 65 | +        /// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlClientFactory.xml' path='docs/members[@name="SqlClientFactory"]/CreateBatch/*'/>  | 
 | 66 | +        public override DbBatch CreateBatch() => new SqlBatch();  | 
 | 67 | + | 
 | 68 | +        /// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlClientFactory.xml' path='docs/members[@name="SqlClientFactory"]/CreateBatchCommand/*'/>  | 
 | 69 | +        public override DbBatchCommand CreateBatchCommand() => new SqlBatchCommand();  | 
 | 70 | +        #endif  | 
27 | 71 | 
 
  | 
28 | 72 |         /// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlClientFactory.xml' path='docs/members[@name="SqlClientFactory"]/CanCreateDataSourceEnumerator/*'/>  | 
29 | 73 |         public override bool CanCreateDataSourceEnumerator => true;  | 
@@ -64,45 +108,28 @@ public override DbParameter CreateParameter()  | 
64 | 108 |             return new SqlParameter();  | 
65 | 109 |         }  | 
66 | 110 | 
 
  | 
67 |  | -#if NETFRAMEWORK  | 
 | 111 | +        #if NETFRAMEWORK  | 
68 | 112 |         /// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlClientFactory.xml' path='docs/members[@name="SqlClientFactory"]/CreatePermission/*'/>  | 
69 | 113 |         public override CodeAccessPermission CreatePermission(PermissionState state)  | 
70 | 114 |         {  | 
71 | 115 |             return new SqlClientPermission(state);  | 
72 | 116 |         }  | 
73 | 117 | 
 
  | 
74 | 118 |         /// <summary>  | 
75 |  | -        /// Extension mechanism for additional services; currently the only service  | 
76 |  | -        /// supported is the DbProviderServices  | 
 | 119 | +        /// Extension mechanism for additional services; currently the only service supported is  | 
 | 120 | +        /// the <c>System.Data.Common.DbProviderServices</c> type.  | 
77 | 121 |         /// </summary>  | 
78 |  | -        /// <returns>requested service provider or null.</returns>  | 
79 |  | -        object IServiceProvider.GetService(Type serviceType)  | 
80 |  | -        {  | 
81 |  | -            object result = null;  | 
82 |  | -            if (serviceType == GreenMethods.SystemDataCommonDbProviderServices_Type)  | 
83 |  | -            {  | 
84 |  | -                result = GreenMethods.MicrosoftDataSqlClientSqlProviderServices_Instance();  | 
85 |  | -            }  | 
86 |  | -            return result;  | 
87 |  | -        }  | 
88 |  | -#endif  | 
 | 122 | +        /// <returns>Requested service provider or <c>null</c>.</returns>  | 
 | 123 | +        object IServiceProvider.GetService(Type serviceType) =>  | 
 | 124 | +            serviceType == SystemDataCommonDbProviderServicesType.Value  | 
 | 125 | +                ? MicrosoftDataSqlClientProviderServicesInstance.Value  | 
 | 126 | +                : null;  | 
 | 127 | +        #endif  | 
89 | 128 | 
 
  | 
90 | 129 |         /// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlClientFactory.xml' path='docs/members[@name="SqlClientFactory"]/CreateDataSourceEnumerator/*'/>  | 
91 | 130 |         public override DbDataSourceEnumerator CreateDataSourceEnumerator()  | 
92 | 131 |         {  | 
93 | 132 |             return SqlDataSourceEnumerator.Instance;  | 
94 | 133 |         }  | 
95 |  | - | 
96 |  | -#if NET  | 
97 |  | -        /// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlClientFactory.xml' path='docs/members[@name="SqlClientFactory"]/CanCreateBatch/*'/>  | 
98 |  | -        public override bool CanCreateBatch => true;  | 
99 |  | - | 
100 |  | -        /// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlClientFactory.xml' path='docs/members[@name="SqlClientFactory"]/CreateBatch/*'/>  | 
101 |  | -        public override DbBatch CreateBatch() => new SqlBatch();  | 
102 |  | - | 
103 |  | -        /// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlClientFactory.xml' path='docs/members[@name="SqlClientFactory"]/CreateBatchCommand/*'/>  | 
104 |  | -        public override DbBatchCommand CreateBatchCommand() => new SqlBatchCommand();  | 
105 |  | -#endif  | 
106 |  | - | 
107 | 134 |     }  | 
108 | 135 | }  | 
0 commit comments