Created
October 5, 2012 01:29
-
-
Save micahasmith/3837529 to your computer and use it in GitHub Desktop.
TPL Async Db w/ ServiceStack and Insight.Database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* WagStack | |
* | |
* TPL Async DAL using ServiceStack's Expression/SQL Building | |
* and Insight.Database's async features | |
*/ | |
public class WagStackAsyncRepository<T>:IAsyncRepository<T> | |
{ | |
static WagStackAsyncRepository() | |
{ | |
OrmLiteConfig.DialectProvider = SqlServerDialect.Provider; | |
} | |
private WagStackUnitOfWork _U; | |
public WagStackAsyncRepository(WagStackUnitOfWork u) | |
{ | |
_U = u; | |
} | |
public System.Threading.Tasks.Task<IList<T>> Get() | |
{ | |
var s = new SqlServerExpressionVisitor<T>(); | |
var sql = s.SelectExpression; | |
return _U.Db.QueryAsync<T>(sql | |
,commandType:System.Data.CommandType.Text | |
, transaction: _U.Transaction); | |
} | |
public Task<int> Insert(T item) | |
{ | |
var sql = SqlServerOrmLiteDialectProvider.Instance.ToInsertRowStatement(item, null); | |
return _U.Db.ExecuteAsync(sql ,commandType:System.Data.CommandType.Text | |
, transaction: _U.Transaction); | |
} | |
public Task<int> Update(T item) | |
{ | |
var sql = SqlServerOrmLiteDialectProvider.Instance.ToUpdateRowStatement(item); | |
return _U.Db.ExecuteAsync(sql, commandType: System.Data.CommandType.Text | |
, transaction: _U.Transaction); | |
} | |
public Task<int> Delete(T item) | |
{ | |
var sql = SqlServerOrmLiteDialectProvider.Instance.ToDeleteRowStatement(item); | |
return _U.Db.ExecuteAsync(sql ,commandType:System.Data.CommandType.Text | |
, transaction: _U.Transaction); | |
} | |
public System.Threading.Tasks.Task<IList<T>> Where(string whereClause) | |
{ | |
var s = new SqlServerExpressionVisitor<T>(); | |
var sql=string.Format("{0} {1}", s.SelectExpression, whereClause); | |
return _U.Db.QueryAsync<T>(sql ,commandType:System.Data.CommandType.Text | |
, transaction: _U.Transaction); | |
} | |
public System.Threading.Tasks.Task<IList<T>> Where(System.Linq.Expressions.Expression<Func<T, bool>> whereClause) | |
{ | |
var s = new SqlServerExpressionVisitor<T>(); | |
var exp = s.Where(whereClause); | |
var sql = string.Format("{0} {1}", s.SelectExpression, exp.WhereExpression); | |
return _U.Db.QueryAsync<T>(sql ,commandType:System.Data.CommandType.Text | |
, transaction: _U.Transaction); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment