Skip to content

Commit 2bb0dc7

Browse files
authored
Merge/Cleanup | GreenMethods (#3254)
* Eliminate GreenMethods and roll the implementation into Lazy implementation in SqlClientFactory * Light movement of properties, and changing #if class declaration. Feel free to argue with me.
1 parent 5dfe397 commit 2bb0dc7

File tree

3 files changed

+58
-89
lines changed

3 files changed

+58
-89
lines changed

src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,6 @@
870870
<Compile Include="Common\Microsoft\Data\Common\NameValuePermission.cs" />
871871
<Compile Include="Microsoft\Data\Common\DbConnectionOptions.cs" />
872872
<Compile Include="Microsoft\Data\Common\DbConnectionString.cs" />
873-
<Compile Include="Microsoft\Data\Common\GreenMethods.cs" />
874873
<Compile Include="Microsoft\Data\SqlClient\BufferWriterExtensions.cs" />
875874
<Compile Include="Microsoft\Data\SqlClient\Server\SmiConnection.cs" />
876875
<Compile Include="Microsoft\Data\SqlClient\Server\SmiContext.cs" />

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Common/GreenMethods.cs

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientFactory.cs

Lines changed: 58 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,72 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Data.Common;
56
using Microsoft.Data.Sql;
7+
8+
#if NETFRAMEWORK
69
using System;
7-
using System.Data.Common;
10+
using System.Reflection;
811
using System.Security.Permissions;
912
using System.Security;
10-
using Microsoft.Data.Common;
13+
#endif
1114

1215
namespace Microsoft.Data.SqlClient
1316
{
1417
/// <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
1521
public sealed class SqlClientFactory : DbProviderFactory
16-
#if NETFRAMEWORK
17-
, IServiceProvider
18-
#endif
22+
#endif
1923
{
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;
2035

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+
2154
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlClientFactory.xml' path='docs/members[@name="SqlClientFactory"]/Instance/*'/>
2255
public static readonly SqlClientFactory Instance = new SqlClientFactory();
2356

2457
private SqlClientFactory()
2558
{
2659
}
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
2771

2872
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlClientFactory.xml' path='docs/members[@name="SqlClientFactory"]/CanCreateDataSourceEnumerator/*'/>
2973
public override bool CanCreateDataSourceEnumerator => true;
@@ -64,45 +108,28 @@ public override DbParameter CreateParameter()
64108
return new SqlParameter();
65109
}
66110

67-
#if NETFRAMEWORK
111+
#if NETFRAMEWORK
68112
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlClientFactory.xml' path='docs/members[@name="SqlClientFactory"]/CreatePermission/*'/>
69113
public override CodeAccessPermission CreatePermission(PermissionState state)
70114
{
71115
return new SqlClientPermission(state);
72116
}
73117

74118
/// <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.
77121
/// </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
89128

90129
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlClientFactory.xml' path='docs/members[@name="SqlClientFactory"]/CreateDataSourceEnumerator/*'/>
91130
public override DbDataSourceEnumerator CreateDataSourceEnumerator()
92131
{
93132
return SqlDataSourceEnumerator.Instance;
94133
}
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-
107134
}
108135
}

0 commit comments

Comments
 (0)