AbstractDAL.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using GSG.NET.Logging;
  9. namespace OHV.SqliteDAL.DAL
  10. {
  11. public interface IRepository<TEntity> where TEntity : class
  12. {
  13. void Create(TEntity entity);
  14. void Delete(TEntity entity);
  15. void Delete(Guid id);
  16. void Edit(TEntity entity);
  17. }
  18. public class GenericDAL<T> where T : class
  19. {
  20. public List<T> All
  21. {
  22. get
  23. {
  24. List<T> ll = new List<T>();
  25. using (var db = new OHVDbContext("OHVDb"))
  26. {
  27. ll = db.Set<T>().ToList();
  28. }
  29. return ll;
  30. }
  31. }
  32. public int Count
  33. {
  34. get
  35. {
  36. using (var db = new OHVDbContext("OHVDb"))
  37. {
  38. return db.Set<T>().Count();
  39. }
  40. }
  41. }
  42. public T GetK(object key)
  43. {
  44. using (var db = new OHVDbContext("OHVDb"))
  45. {
  46. return db.Set<T>().Find(key);
  47. }
  48. }
  49. public void Add(T entity)
  50. {
  51. using (var db = new OHVDbContext("OHVDb"))
  52. {
  53. db.Set<T>().Add(entity);
  54. db.SaveChanges();
  55. }
  56. }
  57. public void Delete(T entity)
  58. {
  59. using (var db = new OHVDbContext("OHVDb"))
  60. {
  61. db.Set<T>().Remove(entity);
  62. db.SaveChanges();
  63. }
  64. }
  65. //protected int DeleteN(Expression<Func<T, bool>> where)
  66. //{
  67. //}
  68. public void Clean()
  69. {
  70. using (var db = new OHVDbContext("OHVDb"))
  71. {
  72. db.Set<T>().RemoveRange(db.Set<T>());
  73. db.SaveChanges();
  74. }
  75. }
  76. }
  77. class GenericRepository<T> : IGenericRepository<T> where T : class
  78. {
  79. protected Logger logger = Logger.GetLogger();
  80. private OHVDbContext _context = null;
  81. private DbSet<T> table = null;
  82. public IEnumerable<T> GetAll()
  83. {
  84. return table.ToList();
  85. }
  86. public T GetById(object id)
  87. {
  88. return table.Find(id);
  89. }
  90. public void Insert(T obj)
  91. {
  92. table.Add(obj);
  93. }
  94. public void Update(T obj)
  95. {
  96. table.Attach(obj);
  97. _context.Entry(obj).State = EntityState.Modified;
  98. }
  99. public void Delete(object id)
  100. {
  101. T existing = table.Find(id);
  102. table.Remove(existing);
  103. }
  104. public void Save()
  105. {
  106. _context.SaveChanges();
  107. }
  108. }
  109. public interface IGenericRepository<T> where T : class
  110. {
  111. IEnumerable<T> GetAll();
  112. T GetById(object id);
  113. void Insert(T obj);
  114. void Update(T obj);
  115. void Delete(object id);
  116. void Save();
  117. }
  118. }