Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Dapper
提交
49e797a4
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,发现更多精彩内容 >>
提交
49e797a4
编写于
2月 17, 2016
作者:
M
Marc Gravell
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix #461 - change FindConstructor to allow parameters that have a type-handler defined
上级
1797a3c0
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
48 addition
and
35 deletion
+48
-35
Dapper.Tests/Tests.Constructors.cs
Dapper.Tests/Tests.Constructors.cs
+42
-34
Dapper/DefaultTypeMap.cs
Dapper/DefaultTypeMap.cs
+1
-1
Dapper/SqlMapper.cs
Dapper/SqlMapper.cs
+5
-0
未找到文件。
Dapper.Tests/Tests.Constructors.cs
浏览文件 @
49e797a4
...
...
@@ -138,71 +138,79 @@ public void TestNoDefaultConstructorBinary()
[
Fact
]
public
void
Issue461_TypeHandlerWorksInConstructor
()
{
// SqlMapper.AddTypeHandler(new Issue461_DateTimeOffset
Handler());
SqlMapper
.
AddTypeHandler
(
new
Issue461_Blarg
Handler
());
connection
.
Execute
(
@"CREATE TABLE #Issue461 (
Id
VARCHAR(50
),
SomeValue
VARCHAR
(50),
Some
DateValue DATETIMEOFFSET
Id
int not null IDENTITY(1,1
),
SomeValue
nvarchar
(50),
Some
BlargValue nvarchar(200),
)"
);
var
when
=
new
DateTimeOffset
(
2016
,
02
,
15
,
16
,
0
,
0
,
TimeSpan
.
FromHours
(
2
));
const
string
Expected
=
"abc123def"
;
var
blarg
=
new
Blarg
(
Expected
);
connection
.
Execute
(
"INSERT INTO #Issue461 (Id, SomeValue, SomeDateValue) VALUES (@Id, @SomeValue, @SomeDateValue)"
,
new
{
Id
=
"id"
,
SomeValue
=
"what up?"
,
SomeDateValue
=
when
});
"INSERT INTO #Issue461 (SomeValue, SomeBlargValue) VALUES (@value, @blarg)"
,
new
{
value
=
"what up?"
,
blarg
});
// test: without constructor
var
parameterlessWorks
=
connection
.
QuerySingle
<
Issue461_ParameterlessTypeConstructor
>(
"SELECT * FROM #Issue461"
);
parameterlessWorks
.
Id
.
IsEqualTo
(
"id"
);
parameterlessWorks
.
Id
.
IsEqualTo
(
1
);
parameterlessWorks
.
SomeValue
.
IsEqualTo
(
"what up?"
);
parameterlessWorks
.
Some
DateValue
.
IsEqualTo
(
when
);
parameterlessWorks
.
Some
BlargValue
.
Value
.
IsEqualTo
(
Expected
);
//
throws about not being able to find constructor (It expects the DateTime field to be a string still)
//
test: via constructor
var
parameterDoesNot
=
connection
.
QuerySingle
<
Issue461_ParameterisedTypeConstructor
>(
"SELECT * FROM #Issue461"
);
parameterDoesNot
.
Id
.
IsEqualTo
(
"id"
);
parameterDoesNot
.
Id
.
IsEqualTo
(
1
);
parameterDoesNot
.
SomeValue
.
IsEqualTo
(
"what up?"
);
parameterDoesNot
.
Some
DateValue
.
IsEqualTo
(
when
);
parameterDoesNot
.
Some
BlargValue
.
Value
.
IsEqualTo
(
Expected
);
}
//class Issue461_DateTimeOffsetHandler : SqlMapper.TypeHandler<DateTimeOffset>
//{
// public override void SetValue(IDbDataParameter parameter, DateTimeOffset value)
// {
// parameter.Value = value.ToString();
// }
class
Blarg
// I would usually expect this to be a struct; using a class
{
// so that we can't pass unexpectedly due to forcing an unsafe cast - want
// to see an InvalidCastException if it is wrong
public
Blarg
(
string
value
)
{
Value
=
value
;
}
public
string
Value
{
get
;
}
public
override
string
ToString
()
{
return
Value
;
}
}
class
Issue461_BlargHandler
:
SqlMapper
.
TypeHandler
<
Blarg
>
{
public
override
void
SetValue
(
IDbDataParameter
parameter
,
Blarg
value
)
{
parameter
.
Value
=
((
object
)
value
.
Value
)
??
DBNull
.
Value
;
}
// public override DateTimeOffset Parse(object value)
// {
// return DateTimeOffset.Parse(value.ToString());
// }
//}
public
override
Blarg
Parse
(
object
value
)
{
string
s
=
(
value
==
null
||
value
is
DBNull
)
?
null
:
Convert
.
ToString
(
value
);
return
new
Blarg
(
s
);
}
}
class
Issue461_ParameterlessTypeConstructor
{
public
string
Id
{
get
;
set
;
}
public
int
Id
{
get
;
set
;
}
public
string
SomeValue
{
get
;
set
;
}
public
DateTimeOffset
SomeDate
Value
{
get
;
set
;
}
public
Blarg
SomeBlarg
Value
{
get
;
set
;
}
}
class
Issue461_ParameterisedTypeConstructor
{
public
Issue461_ParameterisedTypeConstructor
(
string
id
,
string
someValue
,
DateTimeOffset
someDate
Value
)
public
Issue461_ParameterisedTypeConstructor
(
int
id
,
string
someValue
,
Blarg
someBlarg
Value
)
{
Id
=
id
;
SomeValue
=
someValue
;
Some
DateValue
=
someDate
Value
;
Some
BlargValue
=
someBlarg
Value
;
}
public
string
Id
{
get
;
}
public
int
Id
{
get
;
}
public
string
SomeValue
{
get
;
}
public
DateTimeOffset
SomeDate
Value
{
get
;
}
public
Blarg
SomeBlarg
Value
{
get
;
}
}
public
class
AbstractInheritance
...
...
Dapper/DefaultTypeMap.cs
浏览文件 @
49e797a4
...
...
@@ -96,7 +96,7 @@ public ConstructorInfo FindConstructor(string[] names, Type[] types)
if
(
types
[
i
]
==
typeof
(
byte
[])
&&
ctorParameters
[
i
].
ParameterType
.
FullName
==
SqlMapper
.
LinqBinary
)
continue
;
var
unboxedType
=
Nullable
.
GetUnderlyingType
(
ctorParameters
[
i
].
ParameterType
)
??
ctorParameters
[
i
].
ParameterType
;
if
(
unboxedType
!=
types
[
i
]
if
(
(
unboxedType
!=
types
[
i
]
&&
!
SqlMapper
.
HasTypeHandler
(
unboxedType
))
&&
!(
unboxedType
.
IsEnum
()
&&
Enum
.
GetUnderlyingType
(
unboxedType
)
==
types
[
i
])
&&
!(
unboxedType
==
typeof
(
char
)
&&
types
[
i
]
==
typeof
(
string
))
&&
!(
unboxedType
.
IsEnum
()
&&
types
[
i
]
==
typeof
(
string
)))
...
...
Dapper/SqlMapper.cs
浏览文件 @
49e797a4
...
...
@@ -270,6 +270,11 @@ public static void AddTypeHandler(Type type, ITypeHandler handler)
AddTypeHandlerImpl
(
type
,
handler
,
true
);
}
internal
static
bool
HasTypeHandler
(
Type
type
)
{
return
typeHandlers
.
ContainsKey
(
type
);
}
/// <summary>
/// Configure the specified type to be processed by a custom handler
/// </summary>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录