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
f1c1d297
Commit
f1c1d297
authored
May 12, 2015
by
Johan Danforth
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added GetAllAsync in Dapper.Contrib
parent
c4905c57
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
81 additions
and
9 deletions
+81
-9
SqlMapperExtensionsAsync.cs
Dapper.Contrib NET45/SqlMapperExtensionsAsync.cs
+54
-3
TestsAsync.cs
Dapper.Contrib.Tests NET45/TestsAsync.cs
+27
-6
No files found.
Dapper.Contrib NET45/SqlMapperExtensionsAsync.cs
View file @
f1c1d297
...
...
@@ -15,9 +15,7 @@ namespace Dapper.Contrib.Extensions
public
static
partial
class
SqlMapperExtensions
{
/// <summary>
/// Returns a single entity by a single id from table "Ts" asynchronously using .NET 4.5 Task. T must be of interface type.
/// Id must be marked with [Key] attribute.
...
...
@@ -78,7 +76,60 @@ public static partial class SqlMapperExtensions
return
obj
;
}
/// <summary>
/// Returns a list of entites from table "Ts".
/// Id of T must be marked with [Key] attribute.
/// Entities created from interfaces are tracked/intercepted for changes and used by the Update() extension
/// for optimal performance.
/// </summary>
/// <typeparam name="T">Interface or type to create and populate</typeparam>
/// <param name="connection">Open SqlConnection</param>
/// <returns>Entity of T</returns>
public
static
async
Task
<
IEnumerable
<
T
>>
GetAllAsync
<
T
>(
this
IDbConnection
connection
,
IDbTransaction
transaction
=
null
,
int
?
commandTimeout
=
null
)
where
T
:
class
{
var
type
=
typeof
(
T
);
var
cacheType
=
typeof
(
List
<
T
>);
string
sql
;
if
(!
GetQueries
.
TryGetValue
(
cacheType
.
TypeHandle
,
out
sql
))
{
var
keys
=
KeyPropertiesCache
(
type
);
if
(
keys
.
Count
()
>
1
)
throw
new
DataException
(
"Get<T> only supports an entity with a single [Key] property"
);
if
(!
keys
.
Any
())
throw
new
DataException
(
"Get<T> only supports en entity with a [Key] property"
);
var
onlyKey
=
keys
.
First
();
var
name
=
GetTableName
(
type
);
// TODO: query information schema and only select fields that are both in information schema and underlying class / interface
sql
=
"select * from "
+
name
;
GetQueries
[
cacheType
.
TypeHandle
]
=
sql
;
}
if
(!
type
.
IsInterface
)
{
return
await
connection
.
QueryAsync
<
T
>(
sql
,
null
,
transaction
,
commandTimeout
:
commandTimeout
);
}
var
result
=
await
connection
.
QueryAsync
(
sql
);
var
list
=
new
List
<
T
>();
Parallel
.
ForEach
(
result
,
r
=>
{
var
res
=
r
as
IDictionary
<
string
,
object
>;
var
obj
=
ProxyGenerator
.
GetInterfaceProxy
<
T
>();
foreach
(
var
property
in
TypePropertiesCache
(
type
))
{
var
val
=
res
[
property
.
Name
];
property
.
SetValue
(
obj
,
val
,
null
);
}
((
IProxy
)
obj
).
IsDirty
=
false
;
//reset change tracking and return
list
.
Add
(
obj
);
});
return
list
;
}
/// <summary>
...
...
Dapper.Contrib.Tests NET45/TestsAsync.cs
View file @
f1c1d297
...
...
@@ -44,7 +44,7 @@ public async Task TestSimpleGetAsync()
{
var
id
=
await
connection
.
InsertAsync
(
new
User
{
Name
=
"Adama"
,
Age
=
10
});
var
user
=
await
connection
.
GetAsync
<
User
>(
id
);
user
.
The
Id
.
IsEqualTo
((
int
)
id
);
user
.
Id
.
IsEqualTo
((
int
)
id
);
user
.
Name
.
IsEqualTo
(
"Adama"
);
await
connection
.
DeleteAsync
(
user
);
}
...
...
@@ -90,7 +90,7 @@ public async Task InsertCheckKeyAsync()
(
await
connection
.
GetAsync
<
IUser
>(
3
)).
IsNull
();
User
user
=
new
User
{
Name
=
"Adamb"
,
Age
=
10
};
int
id
=
(
int
)
await
connection
.
InsertAsync
(
user
);
user
.
The
Id
.
IsEqualTo
(
id
);
user
.
Id
.
IsEqualTo
(
id
);
}
}
...
...
@@ -102,9 +102,9 @@ public async Task BuilderSelectClauseAsync()
var
data
=
new
List
<
User
>();
for
(
int
i
=
0
;
i
<
100
;
i
++)
{
var
nU
=
new
User
{
Age
=
rand
.
Next
(
70
),
The
Id
=
i
,
Name
=
Guid
.
NewGuid
().
ToString
()
};
var
nU
=
new
User
{
Age
=
rand
.
Next
(
70
),
Id
=
i
,
Name
=
Guid
.
NewGuid
().
ToString
()
};
data
.
Add
(
nU
);
nU
.
The
Id
=
(
int
)
await
connection
.
InsertAsync
<
User
>(
nU
);
nU
.
Id
=
(
int
)
await
connection
.
InsertAsync
<
User
>(
nU
);
}
var
builder
=
new
SqlBuilder
();
...
...
@@ -118,8 +118,8 @@ public async Task BuilderSelectClauseAsync()
foreach
(
var
u
in
data
)
{
if
(!
ids
.
Any
(
i
=>
u
.
The
Id
==
i
))
throw
new
Exception
(
"Missing ids in select"
);
if
(!
users
.
Any
(
a
=>
a
.
TheId
==
u
.
The
Id
&&
a
.
Name
==
u
.
Name
&&
a
.
Age
==
u
.
Age
))
throw
new
Exception
(
"Missing users in select"
);
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"
);
}
}
}
...
...
@@ -141,6 +141,27 @@ public async Task BuilderTemplateWOCompositionAsync()
}
}
public
async
Task
GetAllAsync
()
{
const
int
numberOfEntities
=
100
;
var
users
=
new
List
<
User
>();
for
(
var
i
=
0
;
i
<
numberOfEntities
;
i
++)
users
.
Add
(
new
User
{
Name
=
"User "
+
i
,
Age
=
i
});
using
(
var
connection
=
GetOpenConnection
())
{
connection
.
DeleteAll
<
User
>();
var
total
=
connection
.
Insert
(
users
);
total
.
IsEqualTo
(
numberOfEntities
);
users
=
(
List
<
User
>)
await
connection
.
GetAllAsync
<
User
>();
users
.
Count
.
IsEqualTo
(
numberOfEntities
);
var
iusers
=
await
connection
.
GetAllAsync
<
IUser
>();
//iusers.Count.IsEqualTo(numberOfEntities);
}
}
public
async
Task
InsertFieldWithReservedNameAsync
()
{
using
(
var
connection
=
GetOpenConnection
())
...
...
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