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
f38d1103
Commit
f38d1103
authored
May 21, 2011
by
johan@JOHAN-DELL-SSD.irm.se
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed bug on Get when there is no record found, cleaned up unused code
parent
a92c8091
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
23 deletions
+20
-23
Tests.cs
Dapper.Contrib.Tests/Tests.cs
+3
-2
SqlMapperExtensions.cs
Dapper.Contrib/Extensions/SqlMapperExtensions.cs
+17
-21
No files found.
Dapper.Contrib.Tests/Tests.cs
View file @
f38d1103
...
@@ -45,12 +45,11 @@ public void Get()
...
@@ -45,12 +45,11 @@ public void Get()
{
{
try
try
{
{
var
user
=
connection
.
Get
<
User
>(
1
);
connection
.
Get
<
User
>(
1
);
Debug
.
Fail
(
"Fail, should have thrown exception"
);
Debug
.
Fail
(
"Fail, should have thrown exception"
);
}
}
catch
(
Exception
)
catch
(
Exception
)
{
{
Console
.
WriteLine
(
"ok"
);
}
}
}
}
...
@@ -60,6 +59,8 @@ public void InsertGetUpdate()
...
@@ -60,6 +59,8 @@ public void InsertGetUpdate()
{
{
using
(
var
connection
=
GetOpenConnection
())
using
(
var
connection
=
GetOpenConnection
())
{
{
connection
.
Get
<
IUser
>(
3
).
IsNull
();
var
id
=
connection
.
Insert
(
new
User
{
Name
=
"Adam"
,
Age
=
10
});
var
id
=
connection
.
Insert
(
new
User
{
Name
=
"Adam"
,
Age
=
10
});
id
.
IsEqualTo
(
1
);
id
.
IsEqualTo
(
1
);
var
user
=
connection
.
Get
<
IUser
>(
id
);
var
user
=
connection
.
Get
<
IUser
>(
id
);
...
...
Dapper.Contrib/Extensions/SqlMapperExtensions.cs
View file @
f38d1103
...
@@ -49,10 +49,10 @@ private static IEnumerable<PropertyInfo> TypePropertiesCache(Type type)
...
@@ -49,10 +49,10 @@ private static IEnumerable<PropertyInfo> TypePropertiesCache(Type type)
/// <param name="connection">Open SqlConnection</param>
/// <param name="connection">Open SqlConnection</param>
/// <param name="id">Id of the entity to get, must be marked with [Key] attribute</param>
/// <param name="id">Id of the entity to get, must be marked with [Key] attribute</param>
/// <returns>Entity of T</returns>
/// <returns>Entity of T</returns>
public
static
T
Get
<
T
>(
this
IDbConnection
connection
,
object
id
)
public
static
T
Get
<
T
>(
this
IDbConnection
connection
,
object
id
)
{
{
var
type
=
typeof
(
T
);
var
type
=
typeof
(
T
);
if
(!
type
.
IsInterface
)
if
(!
type
.
IsInterface
)
throw
new
DataException
(
"This version of Get<T>() only supports interfaces."
);
throw
new
DataException
(
"This version of Get<T>() only supports interfaces."
);
var
keys
=
KeyPropertiesCache
(
type
);
var
keys
=
KeyPropertiesCache
(
type
);
...
@@ -68,24 +68,20 @@ public static T Get<T>(this IDbConnection connection, object id)
...
@@ -68,24 +68,20 @@ public static T Get<T>(this IDbConnection connection, object id)
var
sql
=
"select * from "
+
name
+
"s where "
+
onlyKey
.
Name
+
" = @"
+
onlyKey
.
Name
;
var
sql
=
"select * from "
+
name
+
"s where "
+
onlyKey
.
Name
+
" = @"
+
onlyKey
.
Name
;
var
dynParms
=
new
DynamicParameters
();
var
dynParms
=
new
DynamicParameters
();
dynParms
.
Add
(
"@"
+
onlyKey
.
Name
,
id
);
dynParms
.
Add
(
"@"
+
onlyKey
.
Name
,
id
);
var
res
=
connection
.
Query
(
sql
,
dynParms
).
FirstOrDefault
()
as
SqlMapper
.
FastExpando
;
if
(
type
.
IsInterface
)
{
if
(
res
==
null
)
var
proxy
=
ProxyGenerator
.
GetInterfaceProxy
<
T
>();
return
(
T
)
((
object
)
null
);
var
res
=
connection
.
Query
(
sql
,
dynParms
).
FirstOrDefault
()
as
SqlMapper
.
FastExpando
;
foreach
(
var
property
in
TypePropertiesCache
(
type
))
var
proxy
=
ProxyGenerator
.
GetInterfaceProxy
<
T
>();
{
foreach
(
var
property
in
TypePropertiesCache
(
type
))
var
val
=
res
.
GetProperty
(
property
.
Name
);
property
.
SetValue
(
proxy
,
val
,
null
);
}
((
IProxy
)
proxy
).
IsDirty
=
false
;
//reset change tracking and return
return
proxy
;
}
else
//for future support, will never be called now...
{
{
var
res
=
connection
.
Query
<
T
>(
sql
,
dynParms
);
var
val
=
res
.
GetProperty
(
property
.
Name
);
return
res
.
FirstOrDefault
(
);
property
.
SetValue
(
proxy
,
val
,
null
);
}
}
((
IProxy
)
proxy
).
IsDirty
=
false
;
//reset change tracking and return
return
proxy
;
}
}
/// <summary>
/// <summary>
...
@@ -102,7 +98,7 @@ public static long Insert<T>(this IDbConnection connection, T entityToInsert)
...
@@ -102,7 +98,7 @@ public static long Insert<T>(this IDbConnection connection, T entityToInsert)
sb
.
AppendFormat
(
"insert into {0}s ("
,
name
);
sb
.
AppendFormat
(
"insert into {0}s ("
,
name
);
var
allProperties
=
TypePropertiesCache
(
typeof
(
T
));
var
allProperties
=
TypePropertiesCache
(
typeof
(
T
));
var
keyProperties
=
KeyPropertiesCache
(
typeof
(
T
));
var
keyProperties
=
KeyPropertiesCache
(
typeof
(
T
));
for
(
var
i
=
0
;
i
<
allProperties
.
Count
();
i
++)
for
(
var
i
=
0
;
i
<
allProperties
.
Count
();
i
++)
{
{
...
@@ -126,7 +122,7 @@ public static long Insert<T>(this IDbConnection connection, T entityToInsert)
...
@@ -126,7 +122,7 @@ public static long Insert<T>(this IDbConnection connection, T entityToInsert)
sb
.
Append
(
") "
);
sb
.
Append
(
") "
);
connection
.
Execute
(
sb
.
ToString
(),
entityToInsert
);
connection
.
Execute
(
sb
.
ToString
(),
entityToInsert
);
//NOTE: would prefer to use IDENT_CURRENT('tablename') or IDENT_SCOPE but these are not available on SQLCE
//NOTE: would prefer to use IDENT_CURRENT('tablename') or IDENT_SCOPE but these are not available on SQLCE
var
r
=
connection
.
Query
(
"select @@IDENTITY id"
);
var
r
=
connection
.
Query
(
"select @@IDENTITY id"
);
tx
.
Commit
();
tx
.
Commit
();
return
(
int
)
r
.
First
().
id
;
return
(
int
)
r
.
First
().
id
;
}
}
...
@@ -140,7 +136,7 @@ public static long Insert<T>(this IDbConnection connection, T entityToInsert)
...
@@ -140,7 +136,7 @@ public static long Insert<T>(this IDbConnection connection, T entityToInsert)
/// <returns>true if updated, false if not found or not modified (tracked entities)</returns>
/// <returns>true if updated, false if not found or not modified (tracked entities)</returns>
public
static
bool
Update
<
T
>(
this
IDbConnection
connection
,
T
entityToUpdate
)
public
static
bool
Update
<
T
>(
this
IDbConnection
connection
,
T
entityToUpdate
)
{
{
var
proxy
=
((
IProxy
)
entityToUpdate
);
var
proxy
=
((
IProxy
)
entityToUpdate
);
if
(
proxy
!=
null
)
if
(
proxy
!=
null
)
{
{
if
(!
proxy
.
IsDirty
)
return
false
;
if
(!
proxy
.
IsDirty
)
return
false
;
...
...
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