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
49e797a4
Commit
49e797a4
authored
Feb 17, 2016
by
Marc Gravell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix #461 - change FindConstructor to allow parameters that have a type-handler defined
parent
1797a3c0
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
35 deletions
+48
-35
Tests.Constructors.cs
Dapper.Tests/Tests.Constructors.cs
+42
-34
DefaultTypeMap.cs
Dapper/DefaultTypeMap.cs
+1
-1
SqlMapper.cs
Dapper/SqlMapper.cs
+5
-0
No files found.
Dapper.Tests/Tests.Constructors.cs
View file @
49e797a4
...
@@ -138,71 +138,79 @@ public void TestNoDefaultConstructorBinary()
...
@@ -138,71 +138,79 @@ public void TestNoDefaultConstructorBinary()
[
Fact
]
[
Fact
]
public
void
Issue461_TypeHandlerWorksInConstructor
()
public
void
Issue461_TypeHandlerWorksInConstructor
()
{
{
// SqlMapper.AddTypeHandler(new Issue461_DateTimeOffset
Handler());
SqlMapper
.
AddTypeHandler
(
new
Issue461_Blarg
Handler
());
connection
.
Execute
(
@"CREATE TABLE #Issue461 (
connection
.
Execute
(
@"CREATE TABLE #Issue461 (
Id
VARCHAR(50
),
Id
int not null IDENTITY(1,1
),
SomeValue
VARCHAR
(50),
SomeValue
nvarchar
(50),
Some
DateValue DATETIMEOFFSET
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
(
connection
.
Execute
(
"INSERT INTO #Issue461 (Id, SomeValue, SomeDateValue) VALUES (@Id, @SomeValue, @SomeDateValue)"
,
"INSERT INTO #Issue461 (SomeValue, SomeBlargValue) VALUES (@value, @blarg)"
,
new
new
{
value
=
"what up?"
,
blarg
});
{
Id
=
"id"
,
SomeValue
=
"what up?"
,
SomeDateValue
=
when
});
// test: without constructor
var
parameterlessWorks
=
connection
.
QuerySingle
<
Issue461_ParameterlessTypeConstructor
>(
"SELECT * FROM #Issue461"
);
var
parameterlessWorks
=
connection
.
QuerySingle
<
Issue461_ParameterlessTypeConstructor
>(
"SELECT * FROM #Issue461"
);
parameterlessWorks
.
Id
.
IsEqualTo
(
"id"
);
parameterlessWorks
.
Id
.
IsEqualTo
(
1
);
parameterlessWorks
.
SomeValue
.
IsEqualTo
(
"what up?"
);
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"
);
var
parameterDoesNot
=
connection
.
QuerySingle
<
Issue461_ParameterisedTypeConstructor
>(
"SELECT * FROM #Issue461"
);
parameterDoesNot
.
Id
.
IsEqualTo
(
"id"
);
parameterDoesNot
.
Id
.
IsEqualTo
(
1
);
parameterDoesNot
.
SomeValue
.
IsEqualTo
(
"what up?"
);
parameterDoesNot
.
SomeValue
.
IsEqualTo
(
"what up?"
);
parameterDoesNot
.
Some
DateValue
.
IsEqualTo
(
when
);
parameterDoesNot
.
Some
BlargValue
.
Value
.
IsEqualTo
(
Expected
);
}
}
//class Issue461_DateTimeOffsetHandler : SqlMapper.TypeHandler<DateTimeOffset>
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
// public override void SetValue(IDbDataParameter parameter, DateTimeOffset value)
// to see an InvalidCastException if it is wrong
// {
public
Blarg
(
string
value
)
{
Value
=
value
;
}
// parameter.Value = value.ToString();
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)
public
override
Blarg
Parse
(
object
value
)
// {
{
// return DateTimeOffset.Parse(value.ToString());
string
s
=
(
value
==
null
||
value
is
DBNull
)
?
null
:
Convert
.
ToString
(
value
);
// }
return
new
Blarg
(
s
);
//}
}
}
class
Issue461_ParameterlessTypeConstructor
class
Issue461_ParameterlessTypeConstructor
{
{
public
string
Id
{
get
;
set
;
}
public
int
Id
{
get
;
set
;
}
public
string
SomeValue
{
get
;
set
;
}
public
string
SomeValue
{
get
;
set
;
}
public
DateTimeOffset
SomeDate
Value
{
get
;
set
;
}
public
Blarg
SomeBlarg
Value
{
get
;
set
;
}
}
}
class
Issue461_ParameterisedTypeConstructor
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
;
Id
=
id
;
SomeValue
=
someValue
;
SomeValue
=
someValue
;
Some
DateValue
=
someDate
Value
;
Some
BlargValue
=
someBlarg
Value
;
}
}
public
string
Id
{
get
;
}
public
int
Id
{
get
;
}
public
string
SomeValue
{
get
;
}
public
string
SomeValue
{
get
;
}
public
DateTimeOffset
SomeDate
Value
{
get
;
}
public
Blarg
SomeBlarg
Value
{
get
;
}
}
}
public
class
AbstractInheritance
public
class
AbstractInheritance
...
...
Dapper/DefaultTypeMap.cs
View file @
49e797a4
...
@@ -96,7 +96,7 @@ public ConstructorInfo FindConstructor(string[] names, Type[] types)
...
@@ -96,7 +96,7 @@ public ConstructorInfo FindConstructor(string[] names, Type[] types)
if
(
types
[
i
]
==
typeof
(
byte
[])
&&
ctorParameters
[
i
].
ParameterType
.
FullName
==
SqlMapper
.
LinqBinary
)
if
(
types
[
i
]
==
typeof
(
byte
[])
&&
ctorParameters
[
i
].
ParameterType
.
FullName
==
SqlMapper
.
LinqBinary
)
continue
;
continue
;
var
unboxedType
=
Nullable
.
GetUnderlyingType
(
ctorParameters
[
i
].
ParameterType
)
??
ctorParameters
[
i
].
ParameterType
;
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
.
IsEnum
()
&&
Enum
.
GetUnderlyingType
(
unboxedType
)
==
types
[
i
])
&&
!(
unboxedType
==
typeof
(
char
)
&&
types
[
i
]
==
typeof
(
string
))
&&
!(
unboxedType
==
typeof
(
char
)
&&
types
[
i
]
==
typeof
(
string
))
&&
!(
unboxedType
.
IsEnum
()
&&
types
[
i
]
==
typeof
(
string
)))
&&
!(
unboxedType
.
IsEnum
()
&&
types
[
i
]
==
typeof
(
string
)))
...
...
Dapper/SqlMapper.cs
View file @
49e797a4
...
@@ -270,6 +270,11 @@ public static void AddTypeHandler(Type type, ITypeHandler handler)
...
@@ -270,6 +270,11 @@ public static void AddTypeHandler(Type type, ITypeHandler handler)
AddTypeHandlerImpl
(
type
,
handler
,
true
);
AddTypeHandlerImpl
(
type
,
handler
,
true
);
}
}
internal
static
bool
HasTypeHandler
(
Type
type
)
{
return
typeHandlers
.
ContainsKey
(
type
);
}
/// <summary>
/// <summary>
/// Configure the specified type to be processed by a custom handler
/// Configure the specified type to be processed by a custom handler
/// </summary>
/// </summary>
...
...
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