Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Dapper
提交
d6022ba9
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,发现更多精彩内容 >>
提交
d6022ba9
编写于
6月 02, 2011
作者:
M
mgravell
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
enable BindByName when available
上级
05fe38d9
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
84 addition
and
3 deletion
+84
-3
Dapper/SqlMapper.cs
Dapper/SqlMapper.cs
+83
-2
Tests/Tests.cs
Tests/Tests.cs
+1
-1
未找到文件。
Dapper/SqlMapper.cs
浏览文件 @
d6022ba9
...
...
@@ -14,6 +14,7 @@
using
System.Reflection
;
using
System.Reflection.Emit
;
using
System.Text
;
using
System.Threading
;
namespace
Dapper
{
...
...
@@ -23,7 +24,86 @@ public interface IDynamicParameters
{
void
AddParameter
(
IDbCommand
command
);
}
static
Link
<
Type
,
Action
<
IDbCommand
,
bool
>>
bindByNameCache
;
static
Action
<
IDbCommand
,
bool
>
GetBindByName
(
Type
commandType
)
{
if
(
commandType
==
null
)
return
null
;
// GIGO
Action
<
IDbCommand
,
bool
>
action
;
if
(
Link
<
Type
,
Action
<
IDbCommand
,
bool
>>.
TryGet
(
bindByNameCache
,
commandType
,
out
action
))
{
return
action
;
}
var
prop
=
commandType
.
GetProperty
(
"BindByName"
,
BindingFlags
.
Public
|
BindingFlags
.
Instance
);
action
=
null
;
ParameterInfo
[]
indexers
;
MethodInfo
setter
;
if
(
prop
!=
null
&&
prop
.
CanWrite
&&
prop
.
PropertyType
==
typeof
(
bool
)
&&
((
indexers
=
prop
.
GetIndexParameters
())
==
null
||
indexers
.
Length
==
0
)
&&
(
setter
=
prop
.
GetSetMethod
())
!=
null
)
{
var
method
=
new
DynamicMethod
(
commandType
.
Name
+
"_BindByName"
,
null
,
new
Type
[]
{
typeof
(
IDbCommand
),
typeof
(
bool
)
});
var
il
=
method
.
GetILGenerator
();
il
.
Emit
(
OpCodes
.
Ldarg_0
);
il
.
Emit
(
OpCodes
.
Castclass
,
commandType
);
il
.
Emit
(
OpCodes
.
Ldarg_1
);
il
.
EmitCall
(
OpCodes
.
Callvirt
,
setter
,
null
);
il
.
Emit
(
OpCodes
.
Ret
);
action
=
(
Action
<
IDbCommand
,
bool
>)
method
.
CreateDelegate
(
typeof
(
Action
<
IDbCommand
,
bool
>));
}
// cache it
Link
<
Type
,
Action
<
IDbCommand
,
bool
>>.
TryAdd
(
ref
bindByNameCache
,
commandType
,
ref
action
);
return
action
;
}
/// <summary>
/// This is a micro-cache; suitable when the number of terms is controllable (a few hundred, for example),
/// and strictly append-only; you cannot change existing values. All key matches are on **REFERENCE**
/// equality. The type is fully thread-safe.
/// </summary>
class
Link
<
TKey
,
TValue
>
where
TKey
:
class
{
public
static
bool
TryGet
(
Link
<
TKey
,
TValue
>
link
,
TKey
key
,
out
TValue
value
)
{
while
(
link
!=
null
)
{
if
((
object
)
key
==(
object
)
link
.
Key
)
{
value
=
link
.
Value
;
return
true
;
}
link
=
link
.
Tail
;
}
value
=
default
(
TValue
);
return
false
;
}
public
static
bool
TryAdd
(
ref
Link
<
TKey
,
TValue
>
head
,
TKey
key
,
ref
TValue
value
)
{
bool
tryAgain
;
do
{
var
snapshot
=
Interlocked
.
CompareExchange
(
ref
head
,
null
,
null
);
TValue
found
;
if
(
TryGet
(
snapshot
,
key
,
out
found
))
{
// existing match; report the existing value instead
value
=
found
;
return
false
;
}
var
newNode
=
new
Link
<
TKey
,
TValue
>(
key
,
value
,
snapshot
);
// did somebody move our cheese?
tryAgain
=
Interlocked
.
CompareExchange
(
ref
head
,
newNode
,
snapshot
)
!=
snapshot
;
}
while
(
tryAgain
);
return
true
;
}
private
Link
(
TKey
key
,
TValue
value
,
Link
<
TKey
,
TValue
>
tail
)
{
Key
=
key
;
Value
=
value
;
Tail
=
tail
;
}
public
TKey
Key
{
get
;
private
set
;
}
public
TValue
Value
{
get
;
private
set
;
}
public
Link
<
TKey
,
TValue
>
Tail
{
get
;
private
set
;
}
}
class
CacheInfo
{
public
object
Deserializer
{
get
;
set
;
}
...
...
@@ -855,7 +935,8 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj
private
static
IDbCommand
SetupCommand
(
IDbConnection
cnn
,
IDbTransaction
transaction
,
string
sql
,
Action
<
IDbCommand
,
object
>
paramReader
,
object
obj
,
int
?
commandTimeout
,
CommandType
?
commandType
)
{
var
cmd
=
cnn
.
CreateCommand
();
var
bindByName
=
GetBindByName
(
cmd
.
GetType
());
if
(
bindByName
!=
null
)
bindByName
(
cmd
,
true
);
cmd
.
Transaction
=
transaction
;
cmd
.
CommandText
=
sql
;
if
(
commandTimeout
.
HasValue
)
...
...
Tests/Tests.cs
浏览文件 @
d6022ba9
...
...
@@ -686,7 +686,7 @@ public void TestDapperSetsPrivates()
{
connection
.
Query
<
PrivateDan
>(
"select 'one' ShadowInDB"
).
First
().
Shadow
.
IsEqualTo
(
1
);
}
/* TODO:
*
public void TestMagicParam()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录