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
5b1134a3
Commit
5b1134a3
authored
Aug 08, 2014
by
Adam Szabo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Async CRUD extension methods added to Dapper.Contrib .NET 4.5
parent
fda7cb83
Changes
10
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
1499 additions
and
4 deletions
+1499
-4
Dapper - VS2012.sln
Dapper - VS2012.sln
+13
-1
Dapper.Contrib NET45.csproj
Dapper.Contrib NET45/Dapper.Contrib NET45.csproj
+1
-3
SqlMapperExtensions.cs
Dapper.Contrib NET45/SqlMapperExtensions.cs
+854
-0
App.config
Dapper.Contrib.Tests NET45/App.config
+6
-0
Assert.cs
Dapper.Contrib.Tests NET45/Assert.cs
+53
-0
Dapper.Contrib.Tests NET45.csproj
Dapper.Contrib.Tests NET45/Dapper.Contrib.Tests NET45.csproj
+76
-0
Program.cs
Dapper.Contrib.Tests NET45/Program.cs
+95
-0
AssemblyInfo.cs
Dapper.Contrib.Tests NET45/Properties/AssemblyInfo.cs
+36
-0
Tests.cs
Dapper.Contrib.Tests NET45/Tests.cs
+198
-0
TestsAsync.cs
Dapper.Contrib.Tests NET45/TestsAsync.cs
+167
-0
No files found.
Dapper - VS2012.sln
View file @
5b1134a3
Microsoft Visual Studio Solution File, Format Version 12.00
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
# Visual Studio 2013
VisualStudioVersion = 12.0.30
723
.0
VisualStudioVersion = 12.0.30
324
.0
MinimumVisualStudioVersion = 10.0.40219.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DapperTests NET40", "Tests\DapperTests NET40.csproj", "{A2A80512-11F4-4028-A995-505463632C84}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DapperTests NET40", "Tests\DapperTests NET40.csproj", "{A2A80512-11F4-4028-A995-505463632C84}"
EndProject
EndProject
...
@@ -42,6 +42,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapper.EntityFramework NET4
...
@@ -42,6 +42,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapper.EntityFramework NET4
EndProject
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapper.Contrib NET45", "Dapper.Contrib NET45\Dapper.Contrib NET45.csproj", "{302EC82F-A81B-48C5-B653-B5C75D2BD103}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapper.Contrib NET45", "Dapper.Contrib NET45\Dapper.Contrib NET45.csproj", "{302EC82F-A81B-48C5-B653-B5C75D2BD103}"
EndProject
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapper.Contrib.Tests NET45", "Dapper.Contrib.Tests NET45\Dapper.Contrib.Tests NET45.csproj", "{7A85178F-4ADC-4E4C-BF08-17FC99488A9A}"
EndProject
Global
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|Any CPU = Debug|Any CPU
...
@@ -182,6 +184,16 @@ Global
...
@@ -182,6 +184,16 @@ Global
{302EC82F-A81B-48C5-B653-B5C75D2BD103}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{302EC82F-A81B-48C5-B653-B5C75D2BD103}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{302EC82F-A81B-48C5-B653-B5C75D2BD103}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{302EC82F-A81B-48C5-B653-B5C75D2BD103}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{302EC82F-A81B-48C5-B653-B5C75D2BD103}.Release|x86.ActiveCfg = Release|Any CPU
{302EC82F-A81B-48C5-B653-B5C75D2BD103}.Release|x86.ActiveCfg = Release|Any CPU
{7A85178F-4ADC-4E4C-BF08-17FC99488A9A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7A85178F-4ADC-4E4C-BF08-17FC99488A9A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7A85178F-4ADC-4E4C-BF08-17FC99488A9A}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{7A85178F-4ADC-4E4C-BF08-17FC99488A9A}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{7A85178F-4ADC-4E4C-BF08-17FC99488A9A}.Debug|x86.ActiveCfg = Debug|Any CPU
{7A85178F-4ADC-4E4C-BF08-17FC99488A9A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7A85178F-4ADC-4E4C-BF08-17FC99488A9A}.Release|Any CPU.Build.0 = Release|Any CPU
{7A85178F-4ADC-4E4C-BF08-17FC99488A9A}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{7A85178F-4ADC-4E4C-BF08-17FC99488A9A}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{7A85178F-4ADC-4E4C-BF08-17FC99488A9A}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
HideSolutionNode = FALSE
...
...
Dapper.Contrib NET45/Dapper.Contrib NET45.csproj
View file @
5b1134a3
...
@@ -44,9 +44,7 @@
...
@@ -44,9 +44,7 @@
<Compile
Include=
"..\Dapper.Contrib\Properties\AssemblyInfo.cs"
>
<Compile
Include=
"..\Dapper.Contrib\Properties\AssemblyInfo.cs"
>
<Link>
AssemblyInfo.cs
</Link>
<Link>
AssemblyInfo.cs
</Link>
</Compile>
</Compile>
<Compile
Include=
"..\Dapper.Contrib\SqlMapperExtensions.cs"
>
<Compile
Include=
"SqlMapperExtensions.cs"
/>
<Link>
SqlMapperExtensions.cs
</Link>
</Compile>
</ItemGroup>
</ItemGroup>
<ItemGroup>
<ItemGroup>
<Folder
Include=
"Properties\"
/>
<Folder
Include=
"Properties\"
/>
...
...
Dapper.Contrib NET45/SqlMapperExtensions.cs
0 → 100644
View file @
5b1134a3
This diff is collapsed.
Click to expand it.
Dapper.Contrib.Tests NET45/App.config
0 → 100644
View file @
5b1134a3
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
configuration
>
<
startup
>
<
supportedRuntime
version
=
"v4.0"
sku
=
".NETFramework,Version=v4.5"
/>
</
startup
>
</
configuration
>
\ No newline at end of file
Dapper.Contrib.Tests NET45/Assert.cs
0 → 100644
View file @
5b1134a3
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
namespace
Dapper.Contrib.Tests_NET45
{
/// <summary>
/// Assert extensions borrowed from Sam's code in DapperTests
/// </summary>
static
class
Assert
{
public
static
void
IsEqualTo
<
T
>(
this
T
obj
,
T
other
)
{
if
(!
obj
.
Equals
(
other
))
{
throw
new
ApplicationException
(
string
.
Format
(
"{0} should be equals to {1}"
,
obj
,
other
));
}
}
public
static
void
IsSequenceEqualTo
<
T
>(
this
IEnumerable
<
T
>
obj
,
IEnumerable
<
T
>
other
)
{
if
(!
obj
.
SequenceEqual
(
other
))
{
throw
new
ApplicationException
(
string
.
Format
(
"{0} should be equals to {1}"
,
obj
,
other
));
}
}
public
static
void
IsFalse
(
this
bool
b
)
{
if
(
b
)
{
throw
new
ApplicationException
(
"Expected false"
);
}
}
public
static
void
IsTrue
(
this
bool
b
)
{
if
(!
b
)
{
throw
new
ApplicationException
(
"Expected true"
);
}
}
public
static
void
IsNull
(
this
object
obj
)
{
if
(
obj
!=
null
)
{
throw
new
ApplicationException
(
"Expected null"
);
}
}
}
}
\ No newline at end of file
Dapper.Contrib.Tests NET45/Dapper.Contrib.Tests NET45.csproj
0 → 100644
View file @
5b1134a3
<?xml version="1.0" encoding="utf-8"?>
<Project
ToolsVersion=
"12.0"
DefaultTargets=
"Build"
xmlns=
"http://schemas.microsoft.com/developer/msbuild/2003"
>
<Import
Project=
"$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props"
Condition=
"Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')"
/>
<PropertyGroup>
<Configuration
Condition=
" '$(Configuration)' == '' "
>
Debug
</Configuration>
<Platform
Condition=
" '$(Platform)' == '' "
>
AnyCPU
</Platform>
<ProjectGuid>
{7A85178F-4ADC-4E4C-BF08-17FC99488A9A}
</ProjectGuid>
<OutputType>
Exe
</OutputType>
<AppDesignerFolder>
Properties
</AppDesignerFolder>
<RootNamespace>
Dapper.Contrib.Tests_NET45
</RootNamespace>
<AssemblyName>
Dapper.Contrib.Tests NET45
</AssemblyName>
<TargetFrameworkVersion>
v4.5
</TargetFrameworkVersion>
<FileAlignment>
512
</FileAlignment>
</PropertyGroup>
<PropertyGroup
Condition=
" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "
>
<PlatformTarget>
AnyCPU
</PlatformTarget>
<DebugSymbols>
true
</DebugSymbols>
<DebugType>
full
</DebugType>
<Optimize>
false
</Optimize>
<OutputPath>
bin\Debug\
</OutputPath>
<DefineConstants>
DEBUG;TRACE
</DefineConstants>
<ErrorReport>
prompt
</ErrorReport>
<WarningLevel>
4
</WarningLevel>
</PropertyGroup>
<PropertyGroup
Condition=
" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "
>
<PlatformTarget>
AnyCPU
</PlatformTarget>
<DebugType>
pdbonly
</DebugType>
<Optimize>
true
</Optimize>
<OutputPath>
bin\Release\
</OutputPath>
<DefineConstants>
TRACE
</DefineConstants>
<ErrorReport>
prompt
</ErrorReport>
<WarningLevel>
4
</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference
Include=
"System"
/>
<Reference
Include=
"System.Core"
/>
<Reference
Include=
"System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL"
/>
<Reference
Include=
"System.Xml.Linq"
/>
<Reference
Include=
"System.Data.DataSetExtensions"
/>
<Reference
Include=
"Microsoft.CSharp"
/>
<Reference
Include=
"System.Data"
/>
<Reference
Include=
"System.Xml"
/>
</ItemGroup>
<ItemGroup>
<Compile
Include=
"Assert.cs"
/>
<Compile
Include=
"Program.cs"
/>
<Compile
Include=
"Properties\AssemblyInfo.cs"
/>
<Compile
Include=
"Tests.cs"
/>
<Compile
Include=
"TestsAsync.cs"
/>
</ItemGroup>
<ItemGroup>
<None
Include=
"App.config"
/>
</ItemGroup>
<ItemGroup>
<ProjectReference
Include=
"..\Dapper NET45\Dapper NET45.csproj"
>
<Project>
{0fff5bc7-0a4b-4d87-835e-4fad70937507}
</Project>
<Name>
Dapper NET45
</Name>
</ProjectReference>
<ProjectReference
Include=
"..\Dapper.Contrib NET45\Dapper.Contrib NET45.csproj"
>
<Project>
{302ec82f-a81b-48c5-b653-b5c75d2bd103}
</Project>
<Name>
Dapper.Contrib NET45
</Name>
</ProjectReference>
<ProjectReference
Include=
"..\Dapper.SqlBuilder\Dapper.SqlBuilder.csproj"
>
<Project>
{bf782ef1-2b0f-42fa-9dd0-928454a94c6d}
</Project>
<Name>
Dapper.SqlBuilder
</Name>
</ProjectReference>
</ItemGroup>
<Import
Project=
"$(MSBuildToolsPath)\Microsoft.CSharp.targets"
/>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
\ No newline at end of file
Dapper.Contrib.Tests NET45/Program.cs
0 → 100644
View file @
5b1134a3
using
System
;
using
System.Collections.Generic
;
using
System.Data.SqlServerCe
;
using
System.Diagnostics
;
using
System.IO
;
using
System.Linq
;
using
System.Reflection
;
using
System.Text
;
using
System.Threading.Tasks
;
namespace
Dapper.Contrib.Tests_NET45
{
class
Program
{
static
void
Main
(
string
[]
args
)
{
Setup
();
RunTests
();
Setup
();
RunAsyncTests
();
Console
.
ReadKey
();
}
private
static
void
Setup
()
{
var
projLoc
=
Assembly
.
GetAssembly
(
typeof
(
Program
)).
Location
;
var
projFolder
=
Path
.
GetDirectoryName
(
projLoc
);
if
(
File
.
Exists
(
projFolder
+
"\\Test.sdf"
))
File
.
Delete
(
projFolder
+
"\\Test.sdf"
);
var
connectionString
=
"Data Source = "
+
projFolder
+
"\\Test.sdf;"
;
var
engine
=
new
SqlCeEngine
(
connectionString
);
engine
.
CreateDatabase
();
using
(
var
connection
=
new
SqlCeConnection
(
connectionString
))
{
connection
.
Open
();
connection
.
Execute
(
@" create table Users (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null, Age int not null) "
);
connection
.
Execute
(
@" create table Automobiles (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null) "
);
connection
.
Execute
(
@" create table Results (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null, [Order] int not null) "
);
}
Console
.
WriteLine
(
"Created database"
);
}
private
static
void
RunTests
()
{
var
tester
=
new
Tests
();
foreach
(
var
method
in
typeof
(
Tests
).
GetMethods
(
BindingFlags
.
Public
|
BindingFlags
.
Instance
|
BindingFlags
.
DeclaredOnly
))
{
if
(
method
.
ReturnType
!=
typeof
(
Task
))
{
Console
.
Write
(
"Running "
+
method
.
Name
);
method
.
Invoke
(
tester
,
null
);
Console
.
WriteLine
(
" - OK!"
);
}
}
}
private
static
void
RunAsyncTests
()
{
var
tester
=
new
TestsAsync
();
Console
.
Write
(
"Running TableNameAsync"
);
Task
.
WaitAll
(
tester
.
TableNameAsync
());
Console
.
WriteLine
(
" - OK!"
);
Console
.
Write
(
"Running TestSimpleGetAsync"
);
Task
.
WaitAll
(
tester
.
TestSimpleGetAsync
());
Console
.
WriteLine
(
" - OK!"
);
Console
.
Write
(
"Running InsertGetUpdateAsync"
);
Task
.
WaitAll
(
tester
.
InsertGetUpdateAsync
());
Console
.
WriteLine
(
" - OK!"
);
Console
.
Write
(
"Running InsertCheckKeyAsync"
);
Task
.
WaitAll
(
tester
.
InsertCheckKeyAsync
());
Console
.
WriteLine
(
" - OK!"
);
Console
.
Write
(
"Running BuilderSelectClauseAsync"
);
Task
.
WaitAll
(
tester
.
BuilderSelectClauseAsync
());
Console
.
WriteLine
(
" - OK!"
);
Console
.
Write
(
"Running BuilderTemplateWOCompositionAsync"
);
Task
.
WaitAll
(
tester
.
BuilderTemplateWOCompositionAsync
());
Console
.
WriteLine
(
" - OK!"
);
Console
.
Write
(
"Running InsertFieldWithReservedNameAsync"
);
Task
.
WaitAll
(
tester
.
InsertFieldWithReservedNameAsync
());
Console
.
WriteLine
(
" - OK!"
);
Console
.
Write
(
"Running DeleteAllAsync"
);
Task
.
WaitAll
(
tester
.
DeleteAllAsync
());
Console
.
WriteLine
(
" - OK!"
);
}
}
}
Dapper.Contrib.Tests NET45/Properties/AssemblyInfo.cs
0 → 100644
View file @
5b1134a3
using
System.Reflection
;
using
System.Runtime.CompilerServices
;
using
System.Runtime.InteropServices
;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[
assembly
:
AssemblyTitle
(
"Dapper.Contrib.Tests NET45"
)]
[
assembly
:
AssemblyDescription
(
""
)]
[
assembly
:
AssemblyConfiguration
(
""
)]
[
assembly
:
AssemblyCompany
(
""
)]
[
assembly
:
AssemblyProduct
(
"Dapper.Contrib.Tests NET45"
)]
[
assembly
:
AssemblyCopyright
(
"Copyright © 2014"
)]
[
assembly
:
AssemblyTrademark
(
""
)]
[
assembly
:
AssemblyCulture
(
""
)]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[
assembly
:
ComVisible
(
false
)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[
assembly
:
Guid
(
"2d6cdd3d-b94b-4cd4-842a-60c6e4c93c5c"
)]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[
assembly
:
AssemblyVersion
(
"1.0.0.0"
)]
[
assembly
:
AssemblyFileVersion
(
"1.0.0.0"
)]
Dapper.Contrib.Tests NET45/Tests.cs
0 → 100644
View file @
5b1134a3
using
System.Data
;
using
System.Data.SqlServerCe
;
using
System.IO
;
using
System.Linq
;
using
System.Reflection
;
using
Dapper.Contrib.Extensions
;
using
System.Collections.Generic
;
using
System
;
using
Dapper
;
namespace
Dapper.Contrib.Tests_NET45
{
public
interface
IUser
{
[
Key
]
int
Id
{
get
;
set
;
}
string
Name
{
get
;
set
;
}
int
Age
{
get
;
set
;
}
}
public
class
User
:
IUser
{
public
int
Id
{
get
;
set
;
}
public
string
Name
{
get
;
set
;
}
public
int
Age
{
get
;
set
;
}
}
[
Table
(
"Automobiles"
)]
public
class
Car
{
public
int
Id
{
get
;
set
;
}
public
string
Name
{
get
;
set
;
}
}
[
Table
(
"Results"
)]
public
class
Result
{
public
int
Id
{
get
;
set
;
}
public
string
Name
{
get
;
set
;
}
public
int
Order
{
get
;
set
;
}
}
public
class
Tests
{
private
IDbConnection
GetOpenConnection
()
{
var
projLoc
=
Assembly
.
GetAssembly
(
GetType
()).
Location
;
var
projFolder
=
Path
.
GetDirectoryName
(
projLoc
);
var
connection
=
new
SqlCeConnection
(
"Data Source = "
+
projFolder
+
"\\Test.sdf;"
);
connection
.
Open
();
return
connection
;
}
public
void
TableName
()
{
using
(
var
connection
=
GetOpenConnection
())
{
// tests against "Automobiles" table (Table attribute)
connection
.
Insert
(
new
Car
{
Name
=
"Volvo"
});
connection
.
Get
<
Car
>(
1
).
Name
.
IsEqualTo
(
"Volvo"
);
connection
.
Update
(
new
Car
()
{
Id
=
1
,
Name
=
"Saab"
}).
IsEqualTo
(
true
);
connection
.
Get
<
Car
>(
1
).
Name
.
IsEqualTo
(
"Saab"
);
connection
.
Delete
(
new
Car
()
{
Id
=
1
}).
IsEqualTo
(
true
);
connection
.
Get
<
Car
>(
1
).
IsNull
();
}
}
public
void
TestSimpleGet
()
{
using
(
var
connection
=
GetOpenConnection
())
{
var
id
=
connection
.
Insert
(
new
User
{
Name
=
"Adama"
,
Age
=
10
});
var
user
=
connection
.
Get
<
User
>(
id
);
user
.
Id
.
IsEqualTo
((
int
)
id
);
user
.
Name
.
IsEqualTo
(
"Adama"
);
connection
.
Delete
(
user
);
}
}
public
void
InsertGetUpdate
()
{
using
(
var
connection
=
GetOpenConnection
())
{
connection
.
Get
<
User
>(
3
).
IsNull
();
var
id
=
connection
.
Insert
(
new
User
{
Name
=
"Adam"
,
Age
=
10
});
//get a user with "isdirty" tracking
var
user
=
connection
.
Get
<
IUser
>(
id
);
user
.
Name
.
IsEqualTo
(
"Adam"
);
connection
.
Update
(
user
).
IsEqualTo
(
false
);
//returns false if not updated, based on tracking
user
.
Name
=
"Bob"
;
connection
.
Update
(
user
).
IsEqualTo
(
true
);
//returns true if updated, based on tracking
user
=
connection
.
Get
<
IUser
>(
id
);
user
.
Name
.
IsEqualTo
(
"Bob"
);
//get a user with no tracking
var
notrackedUser
=
connection
.
Get
<
User
>(
id
);
notrackedUser
.
Name
.
IsEqualTo
(
"Bob"
);
connection
.
Update
(
notrackedUser
).
IsEqualTo
(
true
);
//returns true, even though user was not changed
notrackedUser
.
Name
=
"Cecil"
;
connection
.
Update
(
notrackedUser
).
IsEqualTo
(
true
);
connection
.
Get
<
User
>(
id
).
Name
.
IsEqualTo
(
"Cecil"
);
connection
.
Query
<
User
>(
"select * from Users"
).
Count
().
IsEqualTo
(
1
);
connection
.
Delete
(
user
).
IsEqualTo
(
true
);
connection
.
Query
<
User
>(
"select * from Users"
).
Count
().
IsEqualTo
(
0
);
connection
.
Update
(
notrackedUser
).
IsEqualTo
(
false
);
//returns false, user not found
}
}
public
void
InsertCheckKey
()
{
using
(
var
connection
=
GetOpenConnection
())
{
connection
.
Get
<
IUser
>(
3
).
IsNull
();
User
user
=
new
User
{
Name
=
"Adamb"
,
Age
=
10
};
int
id
=
(
int
)
connection
.
Insert
(
user
);
user
.
Id
.
IsEqualTo
(
id
);
}
}
public
void
BuilderSelectClause
()
{
using
(
var
connection
=
GetOpenConnection
())
{
var
rand
=
new
Random
(
8675309
);
var
data
=
new
List
<
User
>();
for
(
int
i
=
0
;
i
<
100
;
i
++)
{
var
nU
=
new
User
{
Age
=
rand
.
Next
(
70
),
Id
=
i
,
Name
=
Guid
.
NewGuid
().
ToString
()
};
data
.
Add
(
nU
);
nU
.
Id
=
(
int
)
connection
.
Insert
<
User
>(
nU
);
}
var
builder
=
new
SqlBuilder
();
var
justId
=
builder
.
AddTemplate
(
"SELECT /**select**/ FROM Users"
);
var
all
=
builder
.
AddTemplate
(
"SELECT Name, /**select**/, Age FROM Users"
);
builder
.
Select
(
"Id"
);
var
ids
=
connection
.
Query
<
int
>(
justId
.
RawSql
,
justId
.
Parameters
);
var
users
=
connection
.
Query
<
User
>(
all
.
RawSql
,
all
.
Parameters
);
foreach
(
var
u
in
data
)
{
if
(!
ids
.
Any
(
i
=>
u
.
Id
==
i
))
throw
new
Exception
(
"Missing ids in select"
);
if
(!
users
.
Any
(
a
=>
a
.
Id
==
u
.
Id
&&
a
.
Name
==
u
.
Name
&&
a
.
Age
==
u
.
Age
))
throw
new
Exception
(
"Missing users in select"
);
}
}
}
public
void
BuilderTemplateWOComposition
()
{
var
builder
=
new
SqlBuilder
();
var
template
=
builder
.
AddTemplate
(
"SELECT COUNT(*) FROM Users WHERE Age = @age"
,
new
{
age
=
5
});
if
(
template
.
RawSql
==
null
)
throw
new
Exception
(
"RawSql null"
);
if
(
template
.
Parameters
==
null
)
throw
new
Exception
(
"Parameters null"
);
using
(
var
connection
=
GetOpenConnection
())
{
connection
.
Insert
(
new
User
{
Age
=
5
,
Name
=
"Testy McTestington"
});
if
(
connection
.
Query
<
int
>(
template
.
RawSql
,
template
.
Parameters
).
Single
()
!=
1
)
throw
new
Exception
(
"Query failed"
);
}
}
public
void
InsertFieldWithReservedName
()
{
using
(
var
conneciton
=
GetOpenConnection
())
{
var
id
=
conneciton
.
Insert
(
new
Result
()
{
Name
=
"Adam"
,
Order
=
1
});
var
result
=
conneciton
.
Get
<
Result
>(
id
);
result
.
Order
.
IsEqualTo
(
1
);
}
}
public
void
DeleteAll
()
{
using
(
var
connection
=
GetOpenConnection
())
{
var
id1
=
connection
.
Insert
(
new
User
()
{
Name
=
"Alice"
,
Age
=
32
});
var
id2
=
connection
.
Insert
(
new
User
()
{
Name
=
"Bob"
,
Age
=
33
});
connection
.
DeleteAll
<
User
>();
connection
.
Get
<
User
>(
id1
).
IsNull
();
connection
.
Get
<
User
>(
id2
).
IsNull
();
}
}
}
}
Dapper.Contrib.Tests NET45/TestsAsync.cs
0 → 100644
View file @
5b1134a3
using
System.Data
;
using
System.Data.SqlServerCe
;
using
System.IO
;
using
System.Linq
;
using
System.Reflection
;
using
Dapper.Contrib.Extensions
;
using
System.Collections.Generic
;
using
System
;
using
Dapper
;
using
System.Threading.Tasks
;
namespace
Dapper.Contrib.Tests_NET45
{
public
class
TestsAsync
{
private
IDbConnection
GetOpenConnection
()
{
var
projLoc
=
Assembly
.
GetAssembly
(
GetType
()).
Location
;
var
projFolder
=
Path
.
GetDirectoryName
(
projLoc
);
var
connection
=
new
SqlCeConnection
(
"Data Source = "
+
projFolder
+
"\\Test.sdf;"
);
connection
.
Open
();
return
connection
;
}
public
async
Task
TableNameAsync
()
{
using
(
var
connection
=
GetOpenConnection
())
{
// tests against "Automobiles" table (Table attribute)
await
connection
.
InsertAsync
(
new
Car
{
Name
=
"Volvo"
});
(
await
connection
.
GetAsync
<
Car
>(
1
)).
Name
.
IsEqualTo
(
"Volvo"
);
(
await
connection
.
UpdateAsync
(
new
Car
()
{
Id
=
1
,
Name
=
"Saab"
})).
IsEqualTo
(
true
);
(
await
connection
.
GetAsync
<
Car
>(
1
)).
Name
.
IsEqualTo
(
"Saab"
);
(
await
connection
.
DeleteAsync
(
new
Car
()
{
Id
=
1
})).
IsEqualTo
(
true
);
(
await
connection
.
GetAsync
<
Car
>(
1
)).
IsNull
();
}
}
public
async
Task
TestSimpleGetAsync
()
{
using
(
var
connection
=
GetOpenConnection
())
{
var
id
=
await
connection
.
InsertAsync
(
new
User
{
Name
=
"Adama"
,
Age
=
10
});
var
user
=
await
connection
.
GetAsync
<
User
>(
id
);
user
.
Id
.
IsEqualTo
((
int
)
id
);
user
.
Name
.
IsEqualTo
(
"Adama"
);
await
connection
.
DeleteAsync
(
user
);
}
}
public
async
Task
InsertGetUpdateAsync
()
{
using
(
var
connection
=
GetOpenConnection
())
{
(
await
connection
.
GetAsync
<
User
>(
3
)).
IsNull
();
var
id
=
await
connection
.
InsertAsync
(
new
User
{
Name
=
"Adam"
,
Age
=
10
});
//get a user with "isdirty" tracking
var
user
=
await
connection
.
GetAsync
<
IUser
>(
id
);
user
.
Name
.
IsEqualTo
(
"Adam"
);
(
await
connection
.
UpdateAsync
(
user
)).
IsEqualTo
(
false
);
//returns false if not updated, based on tracking
user
.
Name
=
"Bob"
;
(
await
connection
.
UpdateAsync
(
user
)).
IsEqualTo
(
true
);
//returns true if updated, based on tracking
user
=
await
connection
.
GetAsync
<
IUser
>(
id
);
user
.
Name
.
IsEqualTo
(
"Bob"
);
//get a user with no tracking
var
notrackedUser
=
await
connection
.
GetAsync
<
User
>(
id
);
notrackedUser
.
Name
.
IsEqualTo
(
"Bob"
);
(
await
connection
.
UpdateAsync
(
notrackedUser
)).
IsEqualTo
(
true
);
//returns true, even though user was not changed
notrackedUser
.
Name
=
"Cecil"
;
(
await
connection
.
UpdateAsync
(
notrackedUser
)).
IsEqualTo
(
true
);
(
await
connection
.
GetAsync
<
User
>(
id
)).
Name
.
IsEqualTo
(
"Cecil"
);
(
await
connection
.
QueryAsync
<
User
>(
"select * from Users"
)).
Count
().
IsEqualTo
(
1
);
(
await
connection
.
DeleteAsync
(
user
)).
IsEqualTo
(
true
);
(
await
connection
.
QueryAsync
<
User
>(
"select * from Users"
)).
Count
().
IsEqualTo
(
0
);
(
await
connection
.
UpdateAsync
(
notrackedUser
)).
IsEqualTo
(
false
);
//returns false, user not found
}
}
public
async
Task
InsertCheckKeyAsync
()
{
using
(
var
connection
=
GetOpenConnection
())
{
(
await
connection
.
GetAsync
<
IUser
>(
3
)).
IsNull
();
User
user
=
new
User
{
Name
=
"Adamb"
,
Age
=
10
};
int
id
=
(
int
)
await
connection
.
InsertAsync
(
user
);
user
.
Id
.
IsEqualTo
(
id
);
}
}
public
async
Task
BuilderSelectClauseAsync
()
{
using
(
var
connection
=
GetOpenConnection
())
{
var
rand
=
new
Random
(
8675309
);
var
data
=
new
List
<
User
>();
for
(
int
i
=
0
;
i
<
100
;
i
++)
{
var
nU
=
new
User
{
Age
=
rand
.
Next
(
70
),
Id
=
i
,
Name
=
Guid
.
NewGuid
().
ToString
()
};
data
.
Add
(
nU
);
nU
.
Id
=
(
int
)
await
connection
.
InsertAsync
<
User
>(
nU
);
}
var
builder
=
new
SqlBuilder
();
var
justId
=
builder
.
AddTemplate
(
"SELECT /**select**/ FROM Users"
);
var
all
=
builder
.
AddTemplate
(
"SELECT Name, /**select**/, Age FROM Users"
);
builder
.
Select
(
"Id"
);
var
ids
=
await
connection
.
QueryAsync
<
int
>(
justId
.
RawSql
,
justId
.
Parameters
);
var
users
=
await
connection
.
QueryAsync
<
User
>(
all
.
RawSql
,
all
.
Parameters
);
foreach
(
var
u
in
data
)
{
if
(!
ids
.
Any
(
i
=>
u
.
Id
==
i
))
throw
new
Exception
(
"Missing ids in select"
);
if
(!
users
.
Any
(
a
=>
a
.
Id
==
u
.
Id
&&
a
.
Name
==
u
.
Name
&&
a
.
Age
==
u
.
Age
))
throw
new
Exception
(
"Missing users in select"
);
}
}
}
public
async
Task
BuilderTemplateWOCompositionAsync
()
{
var
builder
=
new
SqlBuilder
();
var
template
=
builder
.
AddTemplate
(
"SELECT COUNT(*) FROM Users WHERE Age = @age"
,
new
{
age
=
5
});
if
(
template
.
RawSql
==
null
)
throw
new
Exception
(
"RawSql null"
);
if
(
template
.
Parameters
==
null
)
throw
new
Exception
(
"Parameters null"
);
using
(
var
connection
=
GetOpenConnection
())
{
await
connection
.
InsertAsync
(
new
User
{
Age
=
5
,
Name
=
"Testy McTestington"
});
if
((
await
connection
.
QueryAsync
<
int
>(
template
.
RawSql
,
template
.
Parameters
)).
Single
()
!=
1
)
throw
new
Exception
(
"Query failed"
);
}
}
public
async
Task
InsertFieldWithReservedNameAsync
()
{
using
(
var
connection
=
GetOpenConnection
())
{
var
id
=
await
connection
.
InsertAsync
(
new
Result
()
{
Name
=
"Adam"
,
Order
=
1
});
var
result
=
await
connection
.
GetAsync
<
Result
>(
id
);
result
.
Order
.
IsEqualTo
(
1
);
}
}
public
async
Task
DeleteAllAsync
()
{
using
(
var
connection
=
GetOpenConnection
())
{
var
id1
=
await
connection
.
InsertAsync
(
new
User
()
{
Name
=
"Alice"
,
Age
=
32
});
var
id2
=
await
connection
.
InsertAsync
(
new
User
()
{
Name
=
"Bob"
,
Age
=
33
});
await
connection
.
DeleteAllAsync
<
User
>();
(
await
connection
.
GetAsync
<
User
>(
id1
)).
IsNull
();
(
await
connection
.
GetAsync
<
User
>(
id2
)).
IsNull
();
}
}
}
}
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