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
5b0517ca
Commit
5b0517ca
authored
Aug 13, 2012
by
Anatoly Zhmur
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added support for custom property/field mapping in TypeDeserializer
parent
4a7e7fb5
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
479 additions
and
87 deletions
+479
-87
SqlMapper.cs
Dapper/SqlMapper.cs
+446
-87
Tests.cs
Tests/Tests.cs
+33
-0
No files found.
Dapper/SqlMapper.cs
View file @
5b0517ca
This diff is collapsed.
Click to expand it.
Tests/Tests.cs
View file @
5b0517ca
...
...
@@ -10,6 +10,7 @@
using
System.Collections
;
using
System.Reflection
;
using
System.Dynamic
;
using
System.ComponentModel
;
#if POSTGRESQL
using
Npgsql
;
#endif
...
...
@@ -1863,6 +1864,38 @@ class ResultsChangeType
public
int
Z
{
get
;
set
;
}
}
public
void
TestCustomTypeMap
()
{
// default mapping
var
item
=
connection
.
Query
<
TypeWithMapping
>(
"Select 'AVal' as A, 'BVal' as B"
).
Single
();
item
.
A
.
IsEqualTo
(
"AVal"
);
item
.
B
.
IsEqualTo
(
"BVal"
);
// custom mapping
var
map
=
new
CustomPropertyTypeMap
(
typeof
(
TypeWithMapping
),
(
type
,
columnName
)
=>
type
.
GetProperties
().
Where
(
prop
=>
prop
.
GetCustomAttributes
(
false
).
OfType
<
DescriptionAttribute
>().
Any
(
attr
=>
attr
.
Description
==
columnName
)).
FirstOrDefault
());
Dapper
.
SqlMapper
.
SetTypeMap
(
typeof
(
TypeWithMapping
),
map
);
item
=
connection
.
Query
<
TypeWithMapping
>(
"Select 'AVal' as A, 'BVal' as B"
).
Single
();
item
.
A
.
IsEqualTo
(
"BVal"
);
item
.
B
.
IsEqualTo
(
"AVal"
);
// reset to default
Dapper
.
SqlMapper
.
SetTypeMap
(
typeof
(
TypeWithMapping
),
null
);
item
=
connection
.
Query
<
TypeWithMapping
>(
"Select 'AVal' as A, 'BVal' as B"
).
Single
();
item
.
A
.
IsEqualTo
(
"AVal"
);
item
.
B
.
IsEqualTo
(
"BVal"
);
}
public
class
TypeWithMapping
{
[
Description
(
"B"
)]
public
string
A
{
get
;
set
;
}
[
Description
(
"A"
)]
public
string
B
{
get
;
set
;
}
}
class
TransactedConnection
:
IDbConnection
{
IDbConnection
_conn
;
...
...
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