Advertisement
minafaw3

GenericSqlite

Sep 22nd, 2018
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. public class EntityController<T> : IEntityController<T> where T : Entity, new()
  2. {
  3. private SQLiteAsyncConnection _db;
  4.  
  5. public EntityController(SQLiteAsyncConnection db)
  6. {
  7. this._db = db;
  8. }
  9.  
  10. public AsyncTableQuery<T> AsQueryable()
  11. {
  12. return _db.Table<T>();
  13. }
  14.  
  15. public async Task<int> Count(Expression<Func<T, bool>> predicate = null)
  16. {
  17. var query = _db.Table<T>();
  18.  
  19. if (predicate != null)
  20. {
  21. query = query.Where(predicate);
  22. }
  23.  
  24. return await query.CountAsync();
  25. }
  26.  
  27. public async Task<int> Delete(T entity)
  28. {
  29. return await _db.DeleteAsync(entity);
  30. }
  31.  
  32. public async Task<List<T>> Get()
  33. {
  34. return await _db.Table<T>().ToListAsync();
  35. }
  36.  
  37. public async Task<T> Get(Expression<Func<T, bool>> predicate)
  38. {
  39. return await _db.FindAsync<T>(predicate);
  40. }
  41.  
  42. public async Task<T> Get(int id)
  43. {
  44. return await _db.FindAsync<T>(id);
  45. }
  46.  
  47. public async Task<ObservableCollection<T>> Get<TValue>(Expression<Func<T, bool>> predicate = null, Expression<Func<T, TValue>> orderBy = null)
  48. {
  49. var query = _db.Table<T>();
  50.  
  51. if (predicate != null)
  52. {
  53. query = query.Where(predicate);
  54. }
  55. if (orderBy != null)
  56. {
  57. query = query.OrderBy<TValue>(orderBy);
  58. }
  59.  
  60. var collection = new ObservableCollection<T>();
  61. var items = await query.ToListAsync();
  62. foreach (var item in items)
  63. {
  64. collection.Add(item);
  65. }
  66.  
  67. return collection;
  68. }
  69.  
  70. public async Task<int> Insert(T entity)
  71. {
  72. return await _db.InsertAsync(entity);
  73. }
  74.  
  75. public async Task<int> Update(T entity)
  76. {
  77. return await _db.UpdateAsync(entity);
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement