diff --git a/Dapper.Contrib/SqlMapperExtensions.Async.cs b/Dapper.Contrib/SqlMapperExtensions.Async.cs index c93e39a4..cb152cf5 100644 --- a/Dapper.Contrib/SqlMapperExtensions.Async.cs +++ b/Dapper.Contrib/SqlMapperExtensions.Async.cs @@ -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); diff --git a/Dapper.Contrib/SqlMapperExtensions.cs b/Dapper.Contrib/SqlMapperExtensions.cs index eb05d190..6416aea5 100644 --- a/Dapper.Contrib/SqlMapperExtensions.cs +++ b/Dapper.Contrib/SqlMapperExtensions.cs @@ -9,6 +9,7 @@ using System.Threading; using Dapper; +using System.Collections; namespace Dapper.Contrib.Extensions { @@ -322,25 +323,7 @@ public static long Insert(this IDbConnection connection, T entityToInsert, ID var isList = false; var type = typeof(T); - - 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); @@ -386,6 +369,7 @@ public static long Insert(this IDbConnection connection, T entityToInsert, ID if (wasClosed) connection.Close(); return returnVal; } + /// /// Updates entity in table "Ts", checks if the entity is modified if the entity is tracked by the Get() extension. @@ -404,23 +388,7 @@ public static bool Update(this IDbConnection connection, T entityToUpdate, ID } 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(); //added ToList() due to issue #418, must work on a list copy var explicitKeyProperties = ExplicitKeyPropertiesCache(type); @@ -474,22 +442,7 @@ public static bool Delete(this IDbConnection connection, T entityToDelete, ID 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(); //added ToList() due to issue #418, must work on a list copy var explicitKeyProperties = ExplicitKeyPropertiesCache(type); @@ -532,6 +485,30 @@ public static bool DeleteAll(this IDbConnection connection, IDbTransaction tr return deleted > 0; } + private static bool IsArrayAndGetElementType(ref Type type) + { + 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]; + } + } + return isList; + } + /// /// Specifies a custom callback that detects the database type instead of relying on the default strategy (the name of the connection type object). /// Please note that this callback is global and will be used by all the calls that require a database specific adapter.