From 0f983c4e8d55cfc07a098f929e15a3cbba0ccdcd Mon Sep 17 00:00:00 2001 From: AlexTian2012 <48512799+AlexTian2012@users.noreply.github.com> Date: Mon, 22 Jan 2024 16:47:29 +0800 Subject: [PATCH] Update SqlMapperExtensions.Async.cs Extract type judgment to private method --- Dapper.Contrib/SqlMapperExtensions.Async.cs | 55 ++------------------- 1 file changed, 4 insertions(+), 51 deletions(-) diff --git a/Dapper.Contrib/SqlMapperExtensions.Async.cs b/Dapper.Contrib/SqlMapperExtensions.Async.cs index c93e39a48..cb1103fb7 100644 --- a/Dapper.Contrib/SqlMapperExtensions.Async.cs +++ b/Dapper.Contrib/SqlMapperExtensions.Async.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Data; using System.Linq; @@ -143,24 +143,7 @@ public static Task InsertAsync(this IDbConnection connection, T entityTo sqlAdapter ??= GetFormatter(connection); var isList = false; - if (type.IsArray) - { - isList = true; - type = type.GetElementType(); - } - else if (type.IsGenericType) - { - var typeInfo = type.GetTypeInfo(); - bool implementsGenericIEnumerableOrIsGenericIEnumerable = - typeInfo.ImplementedInterfaces.Any(ti => ti.IsGenericType && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)) || - typeInfo.GetGenericTypeDefinition() == typeof(IEnumerable<>); - - if (implementsGenericIEnumerableOrIsGenericIEnumerable) - { - isList = true; - type = type.GetGenericArguments()[0]; - } - } + isList = IsArrayAndGetElementType(ref type); var name = GetTableName(type); var sbColumnList = new StringBuilder(null); @@ -215,22 +198,7 @@ public static async Task UpdateAsync(this IDbConnection connection, T e var type = typeof(T); - if (type.IsArray) - { - type = type.GetElementType(); - } - else if (type.IsGenericType) - { - var typeInfo = type.GetTypeInfo(); - bool implementsGenericIEnumerableOrIsGenericIEnumerable = - typeInfo.ImplementedInterfaces.Any(ti => ti.IsGenericType && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)) || - typeInfo.GetGenericTypeDefinition() == typeof(IEnumerable<>); - - if (implementsGenericIEnumerableOrIsGenericIEnumerable) - { - type = type.GetGenericArguments()[0]; - } - } + _ = IsArrayAndGetElementType(ref type); var keyProperties = KeyPropertiesCache(type).ToList(); var explicitKeyProperties = ExplicitKeyPropertiesCache(type); @@ -284,22 +252,7 @@ public static async Task DeleteAsync(this IDbConnection connection, T e var type = typeof(T); - if (type.IsArray) - { - type = type.GetElementType(); - } - else if (type.IsGenericType) - { - var typeInfo = type.GetTypeInfo(); - bool implementsGenericIEnumerableOrIsGenericIEnumerable = - typeInfo.ImplementedInterfaces.Any(ti => ti.IsGenericType && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)) || - typeInfo.GetGenericTypeDefinition() == typeof(IEnumerable<>); - - if (implementsGenericIEnumerableOrIsGenericIEnumerable) - { - type = type.GetGenericArguments()[0]; - } - } + _ = IsArrayAndGetElementType(ref type); var keyProperties = KeyPropertiesCache(type); var explicitKeyProperties = ExplicitKeyPropertiesCache(type);