Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
Dapper
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
Dapper
Commits
62cb035d
Commit
62cb035d
authored
Jun 06, 2015
by
johandanforth
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Custom table name mapper for Contrib
parent
beb675d1
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
65 additions
and
13 deletions
+65
-13
Program.cs
Dapper.Contrib.Tests NET45/Program.cs
+1
-0
Program.cs
Dapper.Contrib.Tests/Program.cs
+1
-0
Tests.cs
Dapper.Contrib.Tests/Tests.cs
+32
-1
SqlMapperExtensions.cs
Dapper.Contrib/SqlMapperExtensions.cs
+31
-12
No files found.
Dapper.Contrib.Tests NET45/Program.cs
View file @
62cb035d
...
...
@@ -31,6 +31,7 @@ private static void Setup()
using
(
var
connection
=
new
SqlCeConnection
(
connectionString
))
{
connection
.
Open
();
connection
.
Execute
(
@" create table People (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null) "
);
connection
.
Execute
(
@" create table Users (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null, Age int not null) "
);
connection
.
Execute
(
@" create table Automobiles (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null) "
);
connection
.
Execute
(
@" create table Results (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null, [Order] int not null) "
);
...
...
Dapper.Contrib.Tests/Program.cs
View file @
62cb035d
...
...
@@ -28,6 +28,7 @@ private static void Setup()
using
(
var
connection
=
new
SqlCeConnection
(
connectionString
))
{
connection
.
Open
();
connection
.
Execute
(
@" create table People (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null) "
);
connection
.
Execute
(
@" create table Users (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null, Age int not null) "
);
connection
.
Execute
(
@" create table Automobiles (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null) "
);
connection
.
Execute
(
@" create table Results (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null, [Order] int not null) "
);
...
...
Dapper.Contrib.Tests/Tests.cs
View file @
62cb035d
...
...
@@ -27,6 +27,12 @@ public class User : IUser
public
int
Age
{
get
;
set
;
}
}
public
class
Person
{
public
int
Id
{
get
;
set
;
}
public
string
Name
{
get
;
set
;
}
}
[
Table
(
"Automobiles"
)]
public
class
Car
{
...
...
@@ -202,7 +208,7 @@ public void InsertGetUpdate()
public
void
InsertWithCustomDbType
()
{
SqlMapperExtensions
.
GetDatabaseType
=
(
conn
)
=>
"SQLiteConnection"
;
SqlMapperExtensions
.
GetDatabaseType
=
connection
=>
"SQLiteConnection"
;
bool
sqliteCodeCalled
=
false
;
using
(
var
connection
=
GetOpenConnection
())
...
...
@@ -229,6 +235,31 @@ public void InsertWithCustomDbType()
}
}
public
void
InsertWithCustomTableNameMapper
()
{
SqlMapperExtensions
.
TableNameMapper
=
(
type
)
=>
{
switch
(
type
.
Name
)
{
case
"Person"
:
return
"People"
;
default
:
var
name
=
type
.
Name
+
"s"
;
if
(
type
.
IsInterface
&&
name
.
StartsWith
(
"I"
))
name
=
name
.
Substring
(
1
);
return
name
;
}
};
using
(
var
connection
=
GetOpenConnection
())
{
var
id
=
connection
.
Insert
(
new
Person
()
{
Name
=
"Mr Mapper"
});
id
.
IsEqualTo
(
1
);
var
people
=
connection
.
GetAll
<
Person
>();
}
}
public
void
GetAll
()
{
const
int
numberOfEntities
=
100
;
...
...
Dapper.Contrib/SqlMapperExtensions.cs
View file @
62cb035d
using
System
;
using
System.Collections
;
using
System.Collections.Generic
;
using
System.Data
;
using
System.Linq
;
...
...
@@ -23,7 +22,14 @@ public interface IProxy //must be kept public
{
bool
IsDirty
{
get
;
set
;
}
}
public
interface
ITableNameMapper
{
string
GetTableName
(
Type
type
);
}
public
delegate
string
GetDatabaseTypeDelegate
(
IDbConnection
connection
);
public
delegate
string
TableNameMapperDelegate
(
Type
type
);
private
static
readonly
ConcurrentDictionary
<
RuntimeTypeHandle
,
IEnumerable
<
PropertyInfo
>>
KeyProperties
=
new
ConcurrentDictionary
<
RuntimeTypeHandle
,
IEnumerable
<
PropertyInfo
>>();
private
static
readonly
ConcurrentDictionary
<
RuntimeTypeHandle
,
IEnumerable
<
PropertyInfo
>>
TypeProperties
=
new
ConcurrentDictionary
<
RuntimeTypeHandle
,
IEnumerable
<
PropertyInfo
>>();
...
...
@@ -208,22 +214,35 @@ private static bool IsWriteable(PropertyInfo pi)
return
list
;
}
/// <summary>
/// Specify a custom table name mapper based on the POCO type name
/// </summary>
public
static
TableNameMapperDelegate
TableNameMapper
;
private
static
string
GetTableName
(
Type
type
)
{
string
name
;
if
(!
TypeTableName
.
TryGetValue
(
type
.
TypeHandle
,
out
name
))
if
(
TypeTableName
.
TryGetValue
(
type
.
TypeHandle
,
out
name
))
return
name
;
if
(
TableNameMapper
!=
null
)
{
name
=
TableNameMapper
(
type
);
}
else
{
name
=
type
.
Name
+
"s"
;
if
(
type
.
IsInterface
&&
name
.
StartsWith
(
"I"
))
name
=
name
.
Substring
(
1
);
//NOTE: This as dynamic trick should be able to handle both our own Table-attribute as well as the one in EntityFramework
var
tableattr
=
type
.
GetCustomAttributes
(
false
).
SingleOrDefault
(
attr
=>
attr
.
GetType
().
Name
==
"TableAttribute"
)
as
dynamic
;
var
tableattr
=
type
.
GetCustomAttributes
(
false
).
SingleOrDefault
(
attr
=>
attr
.
GetType
().
Name
==
"TableAttribute"
)
as
dynamic
;
if
(
tableattr
!=
null
)
name
=
tableattr
.
Name
;
TypeTableName
[
type
.
TypeHandle
]
=
name
;
else
{
name
=
type
.
Name
+
"s"
;
if
(
type
.
IsInterface
&&
name
.
StartsWith
(
"I"
))
name
=
name
.
Substring
(
1
);
}
}
TypeTableName
[
type
.
TypeHandle
]
=
name
;
return
name
;
}
...
...
@@ -391,14 +410,14 @@ private static string GetTableName(Type type)
/// Please note that this callback is global and will be used by all the calls that require a database specific adapter.
/// </summary>
public
static
GetDatabaseTypeDelegate
GetDatabaseType
;
private
static
ISqlAdapter
GetFormatter
(
IDbConnection
connection
)
{
string
name
;
var
getDatabaseType
=
GetDatabaseType
;
if
(
getDatabaseType
!=
null
)
if
(
GetDatabaseType
!=
null
)
{
name
=
g
etDatabaseType
(
connection
);
name
=
G
etDatabaseType
(
connection
);
if
(
name
!=
null
)
name
=
name
.
ToLower
();
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment