Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dapper Contrib - Column mapping #623

Closed
wants to merge 14 commits into from
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ NuGet.exe
Test.DB.*
TestResults/
Dapper.Tests/*.sdf
.dotnet/*
.dotnet/*
.idea.*/
Dapper.userprefs
148 changes: 83 additions & 65 deletions Dapper.Contrib/SqlMapperExtensions.Async.cs

Large diffs are not rendered by default.

344 changes: 215 additions & 129 deletions Dapper.Contrib/SqlMapperExtensions.cs

Large diffs are not rendered by default.

59 changes: 54 additions & 5 deletions Dapper.Tests.Contrib/TestSuite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Data;
using System.Linq;

using System.Reflection;
using Dapper.Contrib.Extensions;

#if !COREFX
Expand Down Expand Up @@ -88,6 +88,13 @@ public class Result
public int Order { get; set; }
}

[Table("StrangelyMappedThing")]
public class StrangelyMappedThing
{
public int WeirdId { get; set; }
public string WonderfullName { get; set; }
}

public abstract partial class TestSuite
{
protected static readonly bool IsAppVeyor = Environment.GetEnvironmentVariable("Appveyor")?.ToUpperInvariant() == "TRUE";
Expand Down Expand Up @@ -171,7 +178,49 @@ public void InsertGetUpdateDeleteWithExplicitKey()
o2.IsNull();
}
}


[Fact]
public void InsertGetUpdateDeleteWithOverriddenColumnMaps()
{
using (var connection = GetOpenConnection())
{
var idCol = new PropertyMap("strangeId", typeof(StrangelyMappedThing).GetProperty("WeirdId"));
var nameCol = new PropertyMap("strangeName", typeof(StrangelyMappedThing).GetProperty("WonderfullName"));

var origGetAll = SqlMapperExtensions.GetAllColumns;
SqlMapperExtensions.GetAllColumns =
t => t == typeof(StrangelyMappedThing) ? new List<PropertyMap> {idCol, nameCol}
: origGetAll(t);

var origGetPersistent = SqlMapperExtensions.GetPersistentColumns;
SqlMapperExtensions.GetPersistentColumns =
t => t == typeof(StrangelyMappedThing) ? new List<PropertyMap> {idCol, nameCol}
: origGetPersistent(t);

var origGetKey = SqlMapperExtensions.GetKeyColumns;
SqlMapperExtensions.GetKeyColumns =
t => t == typeof(StrangelyMappedThing) ? new List<PropertyMap> {idCol}
: origGetKey(t);

const int id = 1;
const string wonderfulName = "foo";
var o1 = new StrangelyMappedThing {WeirdId = id, WonderfullName = wonderfulName};
var originalxCount = connection.Query<int>("Select Count(*) From StrangelyMappedThing").First();
connection.Insert(o1);
var list1 = connection.Query<StrangelyMappedThing>("select * from StrangelyMappedThing").ToList();
list1.Count.IsEqualTo(originalxCount + 1);
o1 = connection.Get<StrangelyMappedThing>(id);
o1.WeirdId.IsEqualTo(id);
o1.WonderfullName = "bar";
connection.Update(o1);
o1 = connection.Get<StrangelyMappedThing>(id);
o1.WonderfullName.IsEqualTo("bar");
connection.Delete(o1);
o1 = connection.Get<StrangelyMappedThing>(id);
o1.IsNull();
}
}

[Fact]
public void GetAllWithExplicitKey()
{
Expand All @@ -187,7 +236,7 @@ public void GetAllWithExplicitKey()
}
}

[Fact]
[Fact]
public void InsertGetUpdateDeleteWithExplicitKeyNamedId()
{
using (var connection = GetOpenConnection())
Expand All @@ -208,8 +257,8 @@ public void InsertGetUpdateDeleteWithExplicitKeyNamedId()
//o2.IsNull();
}
}

[Fact]
[Fact]
public void ShortIdentity()
{
using (var connection = GetOpenConnection())
Expand Down
8 changes: 7 additions & 1 deletion Dapper.Tests.Contrib/TestSuites.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,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("StrangelyMappedThing");
connection.Execute(@"CREATE TABLE StrangelyMappedThing (strangeId int not null, strangeName nvarchar(100) not null);");
}
}
}
Expand Down Expand Up @@ -104,6 +106,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("StrangelyMappedThing");
connection.Execute(@"CREATE TABLE StrangelyMappedThing (strangeId int not null, strangeName nvarchar(100) not null);");
}
}
catch (MySqlException e)
Expand Down Expand Up @@ -145,6 +149,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 StrangelyMappedThing (strangeId int not null, strangeName nvarchar(100) not null);");
}
}
}
Expand All @@ -156,7 +161,7 @@ public class SqlCETestSuite : TestSuite
const string FileName = "Test.DB.sdf";
public static string ConnectionString => $"Data Source={FileName};";
public override IDbConnection GetConnection() => new SqlCeConnection(ConnectionString);

static SqlCETestSuite()
{
if (File.Exists(FileName))
Expand All @@ -176,6 +181,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 StrangelyMappedThing (strangeId int not null, strangeName nvarchar(100) not null);");
}
Console.WriteLine("Created database");
}
Expand Down
4 changes: 2 additions & 2 deletions Dapper.Tests.Contrib/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"../Dapper.Tests/XunitSkippable.cs",
"../Dapper/TypeExtensions.cs"
]
}
}
},
"testRunner": "xunit",
"frameworks": {
Expand Down Expand Up @@ -112,4 +112,4 @@
}
}
}
}
}
2 changes: 1 addition & 1 deletion Dapper.Tests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@
}
}
}
}
}