AbstractDAL.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.Data.Entity.Core.Objects;
  5. using System.Data.Entity.Infrastructure;
  6. using System.Data.Entity.Validation;
  7. using System.Linq;
  8. using System.Linq.Expressions;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using GSG.NET.Logging;
  12. using GSG.NET.Utils;
  13. namespace OHV.SqliteDAL.DAL
  14. {
  15. public class GenericDAL<T> where T : class
  16. {
  17. public delegate void DlgChangedProperty();
  18. public event DlgChangedProperty ChangedProperty;
  19. protected virtual void OnChangedProperty()
  20. {
  21. ChangedProperty?.BeginInvoke( null, null );
  22. }
  23. public List<T> All
  24. {
  25. get
  26. {
  27. List<T> ll = new List<T>();
  28. using ( var db = new OHVDbContext( "OHVDb" ) )
  29. {
  30. ll = db.Set<T>().ToList();
  31. }
  32. return ll;
  33. }
  34. }
  35. public int Count
  36. {
  37. get
  38. {
  39. using ( var db = new OHVDbContext( "OHVDb" ) )
  40. {
  41. return db.Set<T>().Count();
  42. }
  43. }
  44. }
  45. public T GetK( object key )
  46. {
  47. using ( var db = new OHVDbContext( "OHVDb" ) )
  48. {
  49. return db.Set<T>().Find( key );
  50. }
  51. }
  52. public List<T> ListN( Expression<Func<T, bool>> where )
  53. {
  54. using ( var db = new OHVDbContext( "OHVDb" ) )
  55. {
  56. return db.Set<T>().Where( where ).ToList();
  57. }
  58. }
  59. public virtual bool HasK( object key )
  60. {
  61. return GetK( key ) == null ? false : true;
  62. }
  63. public virtual bool HasN( Expression<Func<T, bool>> where )
  64. {
  65. List<T> ll = new List<T>();
  66. using ( var db = new OHVDbContext( "OHVDb" ) )
  67. {
  68. ll = db.Set<T>().Where( where ).ToList();
  69. }
  70. return ll.Count > 0;
  71. }
  72. public void Add( T entity )
  73. {
  74. using ( var db = new OHVDbContext( "OHVDb" ) )
  75. {
  76. db.Set<T>().Add( entity );
  77. db.SaveChanges();
  78. }
  79. OnChangedProperty();
  80. }
  81. public void Delete( T entity )
  82. {
  83. using ( var db = new OHVDbContext( "OHVDb" ) )
  84. {
  85. db.Entry( entity ).State = EntityState.Deleted;
  86. //db.Set<T>().Remove(entity);
  87. db.SaveChanges();
  88. }
  89. OnChangedProperty();
  90. }
  91. public virtual void Update( T entity )
  92. {
  93. using ( var db = new OHVDbContext( "OHVDb" ) )
  94. {
  95. db.Entry( entity ).State = EntityState.Modified;
  96. db.SaveChanges();
  97. }
  98. OnChangedProperty();
  99. }
  100. public void Clean()
  101. {
  102. using ( var db = new OHVDbContext( "OHVDb" ) )
  103. {
  104. db.Set<T>().RemoveRange( db.Set<T>() );
  105. db.SaveChanges();
  106. }
  107. OnChangedProperty();
  108. }
  109. }
  110. ////////////////////////////////////////////////////////////////////////////////////////
  111. public interface IGenericRepository<T> where T : class
  112. {
  113. event Action OnChangeTable;
  114. int Count { get; }
  115. IEnumerable<T> GetAll();
  116. IEnumerable<T> Get( Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = "" );
  117. T GetById( object id );
  118. void Insert( T obj );
  119. void Update( T obj );
  120. void Delete( object id );
  121. void Delete( Expression<Func<T, bool>> filter = null );
  122. void Clean();
  123. void Save();
  124. void Dispose();
  125. }
  126. class GenericRepository<T> : IGenericRepository<T> where T : class
  127. {
  128. protected Logger logger = Logger.GetLogger();
  129. private OHVDbContext _context = null;
  130. private DbSet<T> table = null;
  131. public event Action OnChangeTable;
  132. object lockObj = new object();
  133. public int Count { get => GetAll().Count(); }
  134. public GenericRepository()
  135. {
  136. this._context = new OHVDbContext( "OHVDb" );
  137. this.table = this._context.Set<T>();
  138. }
  139. public GenericRepository( OHVDbContext dbContext )
  140. {
  141. this._context = dbContext;
  142. this.table = _context.Set<T>();
  143. }
  144. public void Dispose()
  145. {
  146. this._context.Dispose();
  147. }
  148. public IEnumerable<T> GetAll()
  149. {
  150. //lock ( lockObj )
  151. return table.ToList();
  152. }
  153. public virtual IEnumerable<T> Get( Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = "" )
  154. {
  155. lock ( lockObj )
  156. {
  157. IQueryable<T> query = this.table;
  158. if ( filter != null )
  159. query = query.Where( filter );
  160. if ( includeProperties != null )
  161. {
  162. foreach ( var includeProperty in includeProperties.Split( new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries ) )
  163. query = query.Include( includeProperty );
  164. }
  165. if ( orderBy != null )
  166. return orderBy( query ).ToList();
  167. else
  168. return query.ToList();
  169. }
  170. }
  171. public T GetById( object id )
  172. {
  173. lock ( lockObj )
  174. return table.Find( id );
  175. }
  176. public void Insert( T obj )
  177. {
  178. lock ( lockObj )
  179. {
  180. table.Add( obj );
  181. this.Save();
  182. }
  183. }
  184. public void Update( T obj )
  185. {
  186. if ( obj == null )
  187. {
  188. logger.E( $"[DataBase] - Update obj is Null {this.table.GetType().Name}" );
  189. return;
  190. }
  191. lock ( lockObj )
  192. {
  193. table.Attach( obj );
  194. _context.Entry( obj ).State = EntityState.Modified;
  195. this.Save();
  196. }
  197. }
  198. public void Delete( object id )
  199. {
  200. T existing = table.Find( id );
  201. if ( existing == null )
  202. return;
  203. lock ( lockObj )
  204. {
  205. if ( _context.Entry( existing ).State == EntityState.Detached )
  206. {
  207. table.Attach( existing );
  208. }
  209. table.Remove( existing );
  210. this.Save();
  211. }
  212. }
  213. public void Delete( Expression<Func<T, bool>> filter )
  214. {
  215. lock ( lockObj )
  216. {
  217. var delList = this.table.Where( filter ).ToList();
  218. if ( delList.Count > 0 )
  219. {
  220. this.table.RemoveRange( delList );
  221. Save();
  222. }
  223. }
  224. }
  225. public void Clean()
  226. {
  227. lock ( lockObj )
  228. {
  229. var ll = this.GetAll();
  230. if (ll.Count() <= 0)
  231. return;
  232. table.RemoveRange( ll );
  233. this.Save();
  234. }
  235. }
  236. public void Save()
  237. {
  238. try
  239. {
  240. _context.SaveChanges();
  241. }
  242. catch ( DbEntityValidationException dbEx )
  243. {
  244. foreach ( var validationErrors in dbEx.EntityValidationErrors )
  245. {
  246. foreach ( var validationError in validationErrors.ValidationErrors )
  247. {
  248. logger.E( "Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage );
  249. }
  250. }
  251. return;
  252. }
  253. //catch ( DbUpdateConcurrencyException ex )
  254. //{
  255. // ex.Entries.Single().Reload();
  256. // logger.E( $"[DataBase] - DbUpdateConcurrencyException {this.table.GetType().Name}" );
  257. // return;
  258. //}
  259. this.OnChangeTable?.Invoke();
  260. //DelegateUtils.Invoke(OnChangeTable);
  261. }
  262. }
  263. ////////////////////////////////////////////////////////////////////////////////////////
  264. public interface IGenericRepositoryType1<T> where T : class
  265. {
  266. IQueryable<T> GetAll();
  267. IQueryable<T> FindBy( Expression<Func<T, bool>> predicate );
  268. void Add( T entity );
  269. void Delete( T entity );
  270. void Edit( T entity );
  271. void Save();
  272. }
  273. public abstract class GenericRepository<C, T> : IGenericRepositoryType1<T> where T : class where C : DbContext, new()
  274. {
  275. private C _entities = new C();
  276. public C Context
  277. {
  278. get { return _entities; }
  279. set { _entities = value; }
  280. }
  281. public virtual IQueryable<T> GetAll()
  282. {
  283. IQueryable<T> query = _entities.Set<T>();
  284. return query;
  285. }
  286. public IQueryable<T> FindBy( System.Linq.Expressions.Expression<Func<T, bool>> predicate )
  287. {
  288. IQueryable<T> query = _entities.Set<T>().Where( predicate );
  289. return query;
  290. }
  291. public virtual void Add( T entity )
  292. {
  293. _entities.Set<T>().Add( entity );
  294. }
  295. public virtual void Delete( T entity )
  296. {
  297. _entities.Set<T>().Remove( entity );
  298. }
  299. public virtual void Edit( T entity )
  300. {
  301. _entities.Entry( entity ).State = EntityState.Modified;
  302. }
  303. public virtual void Save()
  304. {
  305. _entities.SaveChanges();
  306. }
  307. }
  308. ////////////////////////////////////////////////////////////////////////////////////////
  309. public interface IRepository<T> where T : class
  310. {
  311. IEnumerable<T> GetAll();
  312. T GetById( object Id );
  313. T Insert( T obj );
  314. void Delete( object Id );
  315. T Update( T obj );
  316. void Save();
  317. }
  318. public class Repository<T> : IRepository<T> where T : class
  319. {
  320. private OHVDbContext context;
  321. private DbSet<T> dbSet;
  322. public Repository()
  323. {
  324. context = new OHVDbContext( "OHVDb" );
  325. dbSet = context.Set<T>();
  326. }
  327. public IEnumerable<T> GetAll()
  328. {
  329. return dbSet.ToList();
  330. }
  331. public T GetById( object id )
  332. {
  333. return dbSet.Find( id );
  334. }
  335. public T Insert( T obj )
  336. {
  337. dbSet.Add( obj );
  338. Save();
  339. return obj;
  340. }
  341. public void Delete( object id )
  342. {
  343. T entityToDelete = dbSet.Find( id );
  344. Delete( entityToDelete );
  345. }
  346. public void Delete( T entityToDelete )
  347. {
  348. if ( context.Entry( entityToDelete ).State == EntityState.Detached )
  349. {
  350. dbSet.Attach( entityToDelete );
  351. }
  352. dbSet.Remove( entityToDelete );
  353. }
  354. public T Update( T obj )
  355. {
  356. dbSet.Attach( obj );
  357. context.Entry( obj ).State = EntityState.Modified;
  358. Save();
  359. return obj;
  360. }
  361. public void Save()
  362. {
  363. try
  364. {
  365. context.SaveChanges();
  366. }
  367. catch ( DbEntityValidationException dbEx )
  368. {
  369. foreach ( var validationErrors in dbEx.EntityValidationErrors )
  370. {
  371. foreach ( var validationError in validationErrors.ValidationErrors )
  372. {
  373. System.Console.WriteLine( "Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage );
  374. }
  375. }
  376. }
  377. }
  378. protected virtual void Dispose( bool disposing )
  379. {
  380. if ( disposing )
  381. {
  382. if ( context != null )
  383. {
  384. context.Dispose();
  385. context = null;
  386. }
  387. }
  388. }
  389. }
  390. }