Advertisement
Guest User

C# SQLite example (DbService)

a guest
Feb 12th, 2020
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.85 KB | None | 0 0
  1. using ConsoleApp1.Models;
  2. using SQLite;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq.Expressions;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace ConsoleApp1.Database
  10. {
  11.     public class DatabaseService<T, TId> : IDbService<T>
  12.         where T : IDbModel, new()
  13.     {
  14.         public SQLiteAsyncConnection Connection { get; }
  15.  
  16.         public DatabaseService(IDbContext context)
  17.         {
  18.             Connection = context.Connection;
  19.         }
  20.  
  21.  
  22.         public async Task Add(T data)
  23.         {
  24.             try
  25.             {
  26.                 await Connection.InsertAsync(data)
  27.                     .ConfigureAwait(false);
  28.             }
  29.             catch (Exception e)
  30.             {
  31.                 throw new Exception(e.ToString());
  32.             }
  33.         }
  34.  
  35.         public async Task AddRange(IEnumerable<T> data)
  36.         {
  37.             try
  38.             {
  39.                 await Connection.InsertAllAsync(data)
  40.                     .ConfigureAwait(false);
  41.             }
  42.             catch (Exception e)
  43.             {
  44.                 throw new Exception(e.ToString());
  45.             }
  46.         }
  47.  
  48.         public async Task<T> GetById(object id)
  49.         {
  50.             try
  51.             {
  52.                 return await Connection.GetAsync<T>(id)
  53.                     .ConfigureAwait(false);
  54.             }
  55.             catch (Exception e)
  56.             {
  57.                 throw new Exception(e.ToString());
  58.             }
  59.         }
  60.  
  61.         public async Task<bool> Exists(object id)
  62.         {
  63.             return await Connection.FindAsync<T>(id)
  64.                 .ConfigureAwait(false) != null;
  65.         }
  66.  
  67.         public async Task<List<T>> GetAll()
  68.         {
  69.             try
  70.             {
  71.                 return await Connection.Table<T>()
  72.                     .ToListAsync()
  73.                     .ConfigureAwait(false);
  74.             }
  75.             catch (Exception e)
  76.             {
  77.                 throw new Exception(e.ToString());
  78.             }
  79.         }
  80.  
  81.         public async Task<List<T>> Where(Expression<Func<T, bool>> predicateExpression)
  82.         {
  83.             try
  84.             {
  85.                 return await Connection.Table<T>()
  86.                     .Where(predicateExpression)
  87.                     .ToListAsync()
  88.                     .ConfigureAwait(false);
  89.             }
  90.             catch (Exception e)
  91.             {
  92.                 throw new Exception(e.ToString());
  93.             }
  94.         }
  95.  
  96.         public async Task<T> FirstOrDefault(Expression<Func<T, bool>> predicateExpression)
  97.         {
  98.             try
  99.             {
  100.                 return await Connection.Table<T>()
  101.                     .FirstOrDefaultAsync(predicateExpression)
  102.                     .ConfigureAwait(false);
  103.             }
  104.             catch (Exception e)
  105.             {
  106.                 throw new Exception(e.ToString());
  107.             }
  108.         }
  109.  
  110.         public async Task<T> FirstOrDefault()
  111.         {
  112.             try
  113.             {
  114.                 return await Connection.Table<T>()
  115.                     .FirstOrDefaultAsync()
  116.                     .ConfigureAwait(false);
  117.             }
  118.             catch (Exception e)
  119.             {
  120.                 throw new Exception(e.ToString());
  121.             }
  122.         }
  123.  
  124.         public async Task Delete(T data)
  125.         {
  126.             try
  127.             {
  128.                 await Connection.DeleteAsync(data)
  129.                     .ConfigureAwait(false);
  130.             }
  131.             catch (Exception e)
  132.             {
  133.                 throw new Exception(e.ToString());
  134.             }
  135.         }
  136.  
  137.         public async Task DeleteAll()
  138.         {
  139.             try
  140.             {
  141.                 await Connection.DeleteAllAsync<T>()
  142.                     .ConfigureAwait(false);
  143.             }
  144.             catch (Exception e)
  145.             {
  146.                 throw new Exception(e.ToString());
  147.             }
  148.         }
  149.  
  150.         public async Task DeleteAll(IEnumerable<T> data)
  151.         {
  152.             try
  153.             {
  154.                 foreach (T item in data)
  155.                 {
  156.                     await Connection.DeleteAsync(item)
  157.                         .ConfigureAwait(false);
  158.                 }
  159.             }
  160.             catch (Exception e)
  161.             {
  162.                 throw new Exception(e.ToString());
  163.             }
  164.         }
  165.  
  166.         public async Task Update(T data)
  167.         {
  168.             try
  169.             {
  170.                 await Connection.UpdateAsync(data)
  171.                     .ConfigureAwait(false);
  172.             }
  173.             catch (Exception e)
  174.             {
  175.                 throw new Exception(e.ToString());
  176.             }
  177.         }
  178.  
  179.         public async Task UpdateAll(IEnumerable<T> data)
  180.         {
  181.             try
  182.             {
  183.                 await Connection.UpdateAllAsync(data)
  184.                     .ConfigureAwait(false);
  185.             }
  186.             catch (Exception e)
  187.             {
  188.                 throw new Exception(e.ToString());
  189.             }
  190.         }
  191.  
  192.         public async Task AddOrUpdate(T data)
  193.         {
  194.             if (await Exists(data.ID))
  195.             {
  196.                 await Update(data)
  197.                     .ConfigureAwait(false);
  198.             }
  199.             else
  200.             {
  201.                 await Add(data)
  202.                     .ConfigureAwait(false);
  203.             }
  204.         }
  205.  
  206.         public async Task AddOrUpdateAll(IEnumerable<T> rangeData)
  207.         {
  208.             foreach (T data in rangeData)
  209.             {
  210.                 await AddOrUpdate(data)
  211.                     .ConfigureAwait(false);
  212.             }
  213.         }
  214.  
  215.         public async Task DropTable()
  216.         {
  217.             try
  218.             {
  219.                 await Connection.DropTableAsync<T>()
  220.                     .ConfigureAwait(false);
  221.             }
  222.             catch (Exception e)
  223.             {
  224.                 throw new Exception(e.ToString());
  225.             }
  226.         }
  227.     }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement