Skip to content

Commit 6ab170c

Browse files
authored
.Net 5 (#1572)
* initial net5.0 tweaks; also fix current IDE warnings * lib updates and fixes * more cleanup * fix snafu * skip locals init * disable xunit shadow copy in tests * test records * explore scenario that currently works re #1571
1 parent c773880 commit 6ab170c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+425
-340
lines changed

Dapper.Contrib/Dapper.Contrib.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
<PackageTags>orm;sql;micro-orm;dapper</PackageTags>
66
<Description>The official collection of get, insert, update and delete helpers for Dapper.net. Also handles lists of entities and optional "dirty" tracking of interface-based entities.</Description>
77
<Authors>Sam Saffron;Johan Danforth</Authors>
8-
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
8+
<TargetFrameworks>net461;netstandard2.0;net5.0</TargetFrameworks>
99
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
10+
<NoWarn>$(NoWarn);CA1050</NoWarn>
1011
</PropertyGroup>
1112
<ItemGroup>
1213
<ProjectReference Include="..\Dapper\Dapper.csproj" />

Dapper.Contrib/SqlMapperExtensions.Async.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ public static async Task<T> GetAsync<T>(this IDbConnection connection, dynamic i
4040
if (!type.IsInterface)
4141
return (await connection.QueryAsync<T>(sql, dynParams, transaction, commandTimeout).ConfigureAwait(false)).FirstOrDefault();
4242

43-
var res = (await connection.QueryAsync<dynamic>(sql, dynParams).ConfigureAwait(false)).FirstOrDefault() as IDictionary<string, object>;
44-
45-
if (res == null)
43+
if (!((await connection.QueryAsync<dynamic>(sql, dynParams).ConfigureAwait(false)).FirstOrDefault() is IDictionary<string, object> res))
44+
{
4645
return null;
46+
}
4747

4848
var obj = ProxyGenerator.GetInterfaceProxy<T>();
4949

@@ -101,7 +101,7 @@ public static Task<IEnumerable<T>> GetAllAsync<T>(this IDbConnection connection,
101101

102102
private static async Task<IEnumerable<T>> GetAllAsyncImpl<T>(IDbConnection connection, IDbTransaction transaction, int? commandTimeout, string sql, Type type) where T : class
103103
{
104-
var result = await connection.QueryAsync(sql).ConfigureAwait(false);
104+
var result = await connection.QueryAsync(sql, transaction: transaction, commandTimeout: commandTimeout).ConfigureAwait(false);
105105
var list = new List<T>();
106106
foreach (IDictionary<string, object> res in result)
107107
{
@@ -140,7 +140,7 @@ public static Task<int> InsertAsync<T>(this IDbConnection connection, T entityTo
140140
int? commandTimeout = null, ISqlAdapter sqlAdapter = null) where T : class
141141
{
142142
var type = typeof(T);
143-
sqlAdapter = sqlAdapter ?? GetFormatter(connection);
143+
sqlAdapter ??= GetFormatter(connection);
144144

145145
var isList = false;
146146
if (type.IsArray)

Dapper.Contrib/SqlMapperExtensions.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,10 @@ public static T Get<T>(this IDbConnection connection, dynamic id, IDbTransaction
187187

188188
if (type.IsInterface)
189189
{
190-
var res = connection.Query(sql, dynParams).FirstOrDefault() as IDictionary<string, object>;
191-
192-
if (res == null)
190+
if (!(connection.Query(sql, dynParams).FirstOrDefault() is IDictionary<string, object> res))
191+
{
193192
return null;
193+
}
194194

195195
obj = ProxyGenerator.GetInterfaceProxy<T>();
196196

@@ -273,7 +273,9 @@ public static IEnumerable<T> GetAll<T>(this IDbConnection connection, IDbTransac
273273
/// <summary>
274274
/// Specify a custom table name mapper based on the POCO type name
275275
/// </summary>
276+
#pragma warning disable CA2211 // Non-constant fields should not be visible - I agree with you, but we can't do that until we break the API
276277
public static TableNameMapperDelegate TableNameMapper;
278+
#pragma warning restore CA2211 // Non-constant fields should not be visible
277279

278280
private static string GetTableName(Type type)
279281
{
@@ -534,7 +536,9 @@ public static bool DeleteAll<T>(this IDbConnection connection, IDbTransaction tr
534536
/// Specifies a custom callback that detects the database type instead of relying on the default strategy (the name of the connection type object).
535537
/// Please note that this callback is global and will be used by all the calls that require a database specific adapter.
536538
/// </summary>
539+
#pragma warning disable CA2211 // Non-constant fields should not be visible - I agree with you, but we can't do that until we break the API
537540
public static GetDatabaseTypeDelegate GetDatabaseType;
541+
#pragma warning restore CA2211 // Non-constant fields should not be visible
538542

539543
private static ISqlAdapter GetFormatter(IDbConnection connection)
540544
{
@@ -552,7 +556,7 @@ private static class ProxyGenerator
552556

553557
private static AssemblyBuilder GetAsmBuilder(string name)
554558
{
555-
#if NETSTANDARD2_0
559+
#if !NET461
556560
return AssemblyBuilder.DefineDynamicAssembly(new AssemblyName { Name = name }, AssemblyBuilderAccess.Run);
557561
#else
558562
return Thread.GetDomain().DefineDynamicAssembly(new AssemblyName { Name = name }, AssemblyBuilderAccess.Run);
@@ -681,8 +685,8 @@ private static void CreateProperty<T>(TypeBuilder typeBuilder, string propertyNa
681685
if (isIdentity)
682686
{
683687
var keyAttribute = typeof(KeyAttribute);
684-
var myConstructorInfo = keyAttribute.GetConstructor(new Type[] { });
685-
var attributeBuilder = new CustomAttributeBuilder(myConstructorInfo, new object[] { });
688+
var myConstructorInfo = keyAttribute.GetConstructor(Type.EmptyTypes);
689+
var attributeBuilder = new CustomAttributeBuilder(myConstructorInfo, Array.Empty<object>());
686690
property.SetCustomAttribute(attributeBuilder);
687691
}
688692

Dapper.EntityFramework/DbGeometryHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public override void SetValue(IDbDataParameter parameter, DbGeometry value)
3535
parsed = SqlGeometry.STGeomFromWKB(new SqlBytes(value.AsBinary()), value.CoordinateSystemId);
3636
}
3737
parameter.Value = parsed ?? DBNull.Value;
38-
if (parameter is SqlParameter)
38+
if (parameter is SqlParameter sqlP)
3939
{
40-
((SqlParameter)parameter).UdtTypeName = "geometry";
40+
sqlP.UdtTypeName = "geometry";
4141
}
4242
}
4343

Dapper.ProviderTools/BulkCopy.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ private static readonly ConcurrentDictionary<Type, Func<DbConnection, object>?>
136136
/// <summary>
137137
/// Release any resources associated with this instance
138138
/// </summary>
139-
public void Dispose() => Dispose(true);
139+
public void Dispose()
140+
{
141+
Dispose(true);
142+
GC.SuppressFinalize(this);
143+
}
140144

141145
/// <summary>
142146
/// Release any resources associated with this instance

Dapper.ProviderTools/Dapper.ProviderTools.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<Title>Dapper Provider Tools</Title>
66
<Description>Provider-agnostic ADO.NET helper utilities</Description>
77
<Authors>Marc Gravell</Authors>
8-
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
8+
<TargetFrameworks>net461;netstandard2.0;net5.0</TargetFrameworks>
99
<SignAssembly>true</SignAssembly>
1010
<Nullable>enable</Nullable>
1111
</PropertyGroup>

Dapper.Rainbow/Dapper.Rainbow.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<Description>Trivial micro-orm implemented on Dapper, provides with CRUD helpers.</Description>
77
<Authors>Sam Saffron</Authors>
88
<Copyright>2017 Sam Saffron</Copyright>
9-
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
9+
<TargetFrameworks>net461;netstandard2.0;net5.0</TargetFrameworks>
1010
<!-- TODO: Docs -->
1111
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
1212
</PropertyGroup>

Dapper.Rainbow/Database.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public string TableName
4545
{
4646
get
4747
{
48-
tableName = tableName ?? database.DetermineTableName<T>(likelyTableName);
48+
tableName ??= database.DetermineTableName<T>(likelyTableName);
4949
return tableName;
5050
}
5151
}
@@ -142,7 +142,9 @@ internal static List<string> GetParamNames(object o)
142142
foreach (var prop in o.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.GetGetMethod(false) != null))
143143
{
144144
var attribs = prop.GetCustomAttributes(typeof(IgnorePropertyAttribute), true);
145+
#pragma warning disable IDE0019 // Use pattern matching - complex enough here
145146
var attr = attribs.FirstOrDefault() as IgnorePropertyAttribute;
147+
#pragma warning restore IDE0019 // Use pattern matching
146148
if (attr == null || (!attr.Value))
147149
{
148150
paramNames.Add(prop.Name);
@@ -478,6 +480,7 @@ public virtual void Dispose()
478480
_connection.Close();
479481
_connection = null;
480482
}
483+
GC.SuppressFinalize(this);
481484
}
482485
}
483486
}

Dapper.Rainbow/Snapshotter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ public DynamicParameters Diff()
6969

7070
private static T Clone(T myObject)
7171
{
72-
cloner = cloner ?? GenerateCloner();
72+
cloner ??= GenerateCloner();
7373
return cloner(myObject);
7474
}
7575

7676
private static DynamicParameters Diff(T original, T current)
7777
{
7878
var dm = new DynamicParameters();
79-
differ = differ ?? GenerateDiffer();
79+
differ ??= GenerateDiffer();
8080
foreach (var pair in differ(original, current))
8181
{
8282
dm.Add(pair.Name, pair.NewValue);
@@ -98,8 +98,8 @@ private static List<PropertyInfo> RelevantProperties()
9898

9999
private static bool AreEqual<U>(U first, U second)
100100
{
101-
if (EqualityComparer<U>.Default.Equals(first, default(U)) && EqualityComparer<U>.Default.Equals(second, default(U))) return true;
102-
if (EqualityComparer<U>.Default.Equals(first, default(U))) return false;
101+
if (EqualityComparer<U>.Default.Equals(first, default) && EqualityComparer<U>.Default.Equals(second, default)) return true;
102+
if (EqualityComparer<U>.Default.Equals(first, default)) return false;
103103
return first.Equals(second);
104104
}
105105

@@ -191,7 +191,7 @@ private static Func<T, T, List<Change>> GenerateDiffer()
191191
private static Func<T, T> GenerateCloner()
192192
{
193193
var dm = new DynamicMethod("DoClone", typeof(T), new Type[] { typeof(T) }, true);
194-
var ctor = typeof(T).GetConstructor(new Type[] { });
194+
var ctor = typeof(T).GetConstructor(Type.EmptyTypes);
195195

196196
var il = dm.GetILGenerator();
197197

Dapper.SqlBuilder/Dapper.SqlBuilder.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<Title>Dapper SqlBuilder component</Title>
66
<Description>The Dapper SqlBuilder component, for building SQL queries dynamically.</Description>
77
<Authors>Sam Saffron, Johan Danforth</Authors>
8-
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
8+
<TargetFrameworks>net461;netstandard2.0;net5.0</TargetFrameworks>
99
<GenerateDocumentationFile>false</GenerateDocumentationFile>
1010
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
1111
</PropertyGroup>

0 commit comments

Comments
 (0)