Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Dapper
提交
f38d1103
D
Dapper
项目概览
int
/
Dapper
大约 1 年 前同步成功
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Dapper
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f38d1103
编写于
5月 21, 2011
作者:
J
johan@JOHAN-DELL-SSD.irm.se
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fixed bug on Get when there is no record found, cleaned up unused code
上级
a92c8091
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
20 addition
and
23 deletion
+20
-23
Dapper.Contrib.Tests/Tests.cs
Dapper.Contrib.Tests/Tests.cs
+3
-2
Dapper.Contrib/Extensions/SqlMapperExtensions.cs
Dapper.Contrib/Extensions/SqlMapperExtensions.cs
+17
-21
未找到文件。
Dapper.Contrib.Tests/Tests.cs
浏览文件 @
f38d1103
...
...
@@ -45,12 +45,11 @@ public void Get()
{
try
{
var
user
=
connection
.
Get
<
User
>(
1
);
connection
.
Get
<
User
>(
1
);
Debug
.
Fail
(
"Fail, should have thrown exception"
);
}
catch
(
Exception
)
{
Console
.
WriteLine
(
"ok"
);
}
}
...
...
@@ -60,6 +59,8 @@ public void InsertGetUpdate()
{
using
(
var
connection
=
GetOpenConnection
())
{
connection
.
Get
<
IUser
>(
3
).
IsNull
();
var
id
=
connection
.
Insert
(
new
User
{
Name
=
"Adam"
,
Age
=
10
});
id
.
IsEqualTo
(
1
);
var
user
=
connection
.
Get
<
IUser
>(
id
);
...
...
Dapper.Contrib/Extensions/SqlMapperExtensions.cs
浏览文件 @
f38d1103
...
...
@@ -49,10 +49,10 @@ private static IEnumerable<PropertyInfo> TypePropertiesCache(Type type)
/// <param name="connection">Open SqlConnection</param>
/// <param name="id">Id of the entity to get, must be marked with [Key] attribute</param>
/// <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
);
if
(!
type
.
IsInterface
)
if
(!
type
.
IsInterface
)
throw
new
DataException
(
"This version of Get<T>() only supports interfaces."
);
var
keys
=
KeyPropertiesCache
(
type
);
...
...
@@ -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
dynParms
=
new
DynamicParameters
();
dynParms
.
Add
(
"@"
+
onlyKey
.
Name
,
id
);
if
(
type
.
IsInterface
)
{
var
proxy
=
ProxyGenerator
.
GetInterfaceProxy
<
T
>();
var
res
=
connection
.
Query
(
sql
,
dynParms
).
FirstOrDefault
()
as
SqlMapper
.
FastExpando
;
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
(
sql
,
dynParms
).
FirstOrDefault
()
as
SqlMapper
.
FastExpando
;
if
(
res
==
null
)
return
(
T
)
((
object
)
null
);
var
proxy
=
ProxyGenerator
.
GetInterfaceProxy
<
T
>();
foreach
(
var
property
in
TypePropertiesCache
(
type
))
{
var
res
=
connection
.
Query
<
T
>(
sql
,
dynParms
);
return
res
.
FirstOrDefault
(
);
var
val
=
res
.
GetProperty
(
property
.
Name
);
property
.
SetValue
(
proxy
,
val
,
null
);
}
((
IProxy
)
proxy
).
IsDirty
=
false
;
//reset change tracking and return
return
proxy
;
}
/// <summary>
...
...
@@ -102,7 +98,7 @@ public static long Insert<T>(this IDbConnection connection, T entityToInsert)
sb
.
AppendFormat
(
"insert into {0}s ("
,
name
);
var
allProperties
=
TypePropertiesCache
(
typeof
(
T
));
var
keyProperties
=
KeyPropertiesCache
(
typeof
(
T
));
var
keyProperties
=
KeyPropertiesCache
(
typeof
(
T
));
for
(
var
i
=
0
;
i
<
allProperties
.
Count
();
i
++)
{
...
...
@@ -126,7 +122,7 @@ public static long Insert<T>(this IDbConnection connection, T entityToInsert)
sb
.
Append
(
") "
);
connection
.
Execute
(
sb
.
ToString
(),
entityToInsert
);
//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
();
return
(
int
)
r
.
First
().
id
;
}
...
...
@@ -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>
public
static
bool
Update
<
T
>(
this
IDbConnection
connection
,
T
entityToUpdate
)
{
var
proxy
=
((
IProxy
)
entityToUpdate
);
var
proxy
=
((
IProxy
)
entityToUpdate
);
if
(
proxy
!=
null
)
{
if
(!
proxy
.
IsDirty
)
return
false
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录