Commit f1c1d297 authored by Johan Danforth's avatar Johan Danforth

Added GetAllAsync in Dapper.Contrib

parent c4905c57
......@@ -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>
......
......@@ -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.TheId.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.TheId.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), TheId = i, Name = Guid.NewGuid().ToString() };
var nU = new User { Age = rand.Next(70), Id = i, Name = Guid.NewGuid().ToString() };
data.Add(nU);
nU.TheId = (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.TheId == i)) throw new Exception("Missing ids in select");
if (!users.Any(a => a.TheId == u.TheId && 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())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment