Commit d269a5a4 authored by Brian Drupieski's avatar Brian Drupieski Committed by Nick Craver

when inserting, check if type implements IEnumerable<T> to determine if it's a collection (#880)

* when inserting check if type implements IEnumerable<T> to determine if it's a collection
* when inserting, check if type is a collection by comparing its implemented interfaces to unbound generic type IEnumerable<> instead of the name "IEnumerable`1"
parent dc374ad1
......@@ -130,7 +130,7 @@ public static partial class SqlMapperExtensions
isList = true;
type = type.GetElementType();
}
else if (type.IsGenericType())
else if (type.IsGenericType() && type.GetTypeInfo().ImplementedInterfaces.Any(ti => ti.IsGenericType() && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)))
{
isList = true;
type = type.GetGenericArguments()[0];
......
......@@ -295,7 +295,7 @@ private static string GetTableName(Type type)
}
/// <summary>
/// Inserts an entity into table "Ts" and returns identity id or number if inserted rows if inserting a list.
/// Inserts an entity into table "Ts" and returns identity id or number of inserted rows if inserting a list.
/// </summary>
/// <typeparam name="T">The type to insert.</typeparam>
/// <param name="connection">Open SqlConnection</param>
......@@ -314,7 +314,7 @@ private static string GetTableName(Type type)
isList = true;
type = type.GetElementType();
}
else if (type.IsGenericType())
else if (type.IsGenericType() && type.GetTypeInfo().ImplementedInterfaces.Any(ti => ti.IsGenericType() && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)))
{
isList = true;
type = type.GetGenericArguments()[0];
......
......@@ -11,6 +11,41 @@ namespace Dapper.Tests.Contrib
{
public abstract partial class TestSuite
{
[Fact]
public async Task TypeWithGenericParameterCanBeInsertedAsync()
{
using (var connection = GetOpenConnection())
{
await connection.DeleteAllAsync<GenericType<string>>();
var objectToInsert = new GenericType<string>
{
Id = Guid.NewGuid().ToString(),
Name = "something"
};
await connection.InsertAsync(objectToInsert);
Assert.Single(connection.GetAll<GenericType<string>>());
var objectsToInsert = new List<GenericType<string>>
{
new GenericType<string>
{
Id = Guid.NewGuid().ToString(),
Name = "1",
},
new GenericType<string>
{
Id = Guid.NewGuid().ToString(),
Name = "2",
}
};
await connection.InsertAsync(objectsToInsert);
var list = connection.GetAll<GenericType<string>>();
Assert.Equal(3, list.Count());
}
}
/// <summary>
/// Tests for issue #351
/// </summary>
......
......@@ -85,6 +85,14 @@ public class Result
public int Order { get; set; }
}
[Table("GenericType")]
public class GenericType<T>
{
[ExplicitKey]
public string Id { get; set; }
public string Name { get; set; }
}
public abstract partial class TestSuite
{
protected static readonly bool IsAppVeyor = Environment.GetEnvironmentVariable("Appveyor")?.ToUpperInvariant() == "TRUE";
......@@ -98,6 +106,41 @@ private IDbConnection GetOpenConnection()
return connection;
}
[Fact]
public void TypeWithGenericParameterCanBeInserted()
{
using (var connection = GetOpenConnection())
{
connection.DeleteAll<GenericType<string>>();
var objectToInsert = new GenericType<string>
{
Id = Guid.NewGuid().ToString(),
Name = "something"
};
connection.Insert(objectToInsert);
Assert.Single(connection.GetAll<GenericType<string>>());
var objectsToInsert = new List<GenericType<string>>
{
new GenericType<string>
{
Id = Guid.NewGuid().ToString(),
Name = "1",
},
new GenericType<string>
{
Id = Guid.NewGuid().ToString(),
Name = "2",
}
};
connection.Insert(objectsToInsert);
var list = connection.GetAll<GenericType<string>>();
Assert.Equal(3, list.Count());
}
}
[Fact]
public void Issue418()
{
......
......@@ -55,6 +55,8 @@ static SqlServerTestSuite()
connection.Execute("CREATE TABLE ObjectY (ObjectYId int not null, Name nvarchar(100) not null);");
dropTable("ObjectZ");
connection.Execute("CREATE TABLE ObjectZ (Id int not null, Name nvarchar(100) not null);");
dropTable("GenericType");
connection.Execute("CREATE TABLE GenericType (Id nvarchar(100) not null, Name nvarchar(100) not null);");
}
}
}
......@@ -102,6 +104,8 @@ static MySqlServerTestSuite()
connection.Execute("CREATE TABLE ObjectY (ObjectYId int not null, Name nvarchar(100) not null);");
dropTable("ObjectZ");
connection.Execute("CREATE TABLE ObjectZ (Id int not null, Name nvarchar(100) not null);");
dropTable("GenericType");
connection.Execute("CREATE TABLE GenericType (Id nvarchar(100) not null, Name nvarchar(100) not null);");
}
}
catch (MySqlException e)
......@@ -137,6 +141,7 @@ static SQLiteTestSuite()
connection.Execute("CREATE TABLE ObjectX (ObjectXId nvarchar(100) not null, Name nvarchar(100) not null) ");
connection.Execute("CREATE TABLE ObjectY (ObjectYId integer not null, Name nvarchar(100) not null) ");
connection.Execute("CREATE TABLE ObjectZ (Id integer not null, Name nvarchar(100) not null) ");
connection.Execute("CREATE TABLE GenericType (Id nvarchar(100) not null, Name nvarchar(100) not null) ");
}
}
}
......@@ -167,6 +172,7 @@ static SqlCETestSuite()
connection.Execute(@"CREATE TABLE ObjectX (ObjectXId nvarchar(100) not null, Name nvarchar(100) not null) ");
connection.Execute(@"CREATE TABLE ObjectY (ObjectYId int not null, Name nvarchar(100) not null) ");
connection.Execute(@"CREATE TABLE ObjectZ (Id int not null, Name nvarchar(100) not null) ");
connection.Execute(@"CREATE TABLE GenericType (Id nvarchar(100) not null, Name nvarchar(100) not null) ");
}
Console.WriteLine("Created database");
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment