| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Data.Entity.Core.Objects;
- using System.Data.Entity.Infrastructure;
- using System.Data.Entity.Validation;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Text;
- using System.Threading.Tasks;
- using GSG.NET.Logging;
- using GSG.NET.Utils;
- namespace OHV.SqliteDAL.DAL
- {
- public class GenericDAL<T> where T : class
- {
- public delegate void DlgChangedProperty();
- public event DlgChangedProperty ChangedProperty;
- protected virtual void OnChangedProperty()
- {
- ChangedProperty?.BeginInvoke( null, null );
- }
- public List<T> All
- {
- get
- {
- List<T> ll = new List<T>();
- using ( var db = new OHVDbContext( "OHVDb" ) )
- {
- ll = db.Set<T>().ToList();
- }
- return ll;
- }
- }
- public int Count
- {
- get
- {
- using ( var db = new OHVDbContext( "OHVDb" ) )
- {
- return db.Set<T>().Count();
- }
- }
- }
- public T GetK( object key )
- {
- using ( var db = new OHVDbContext( "OHVDb" ) )
- {
- return db.Set<T>().Find( key );
- }
- }
- public List<T> ListN( Expression<Func<T, bool>> where )
- {
- using ( var db = new OHVDbContext( "OHVDb" ) )
- {
- return db.Set<T>().Where( where ).ToList();
- }
- }
- public virtual bool HasK( object key )
- {
- return GetK( key ) == null ? false : true;
- }
- public virtual bool HasN( Expression<Func<T, bool>> where )
- {
- List<T> ll = new List<T>();
- using ( var db = new OHVDbContext( "OHVDb" ) )
- {
- ll = db.Set<T>().Where( where ).ToList();
- }
- return ll.Count > 0;
- }
- public void Add( T entity )
- {
- using ( var db = new OHVDbContext( "OHVDb" ) )
- {
- db.Set<T>().Add( entity );
- db.SaveChanges();
- }
- OnChangedProperty();
- }
- public void Delete( T entity )
- {
- using ( var db = new OHVDbContext( "OHVDb" ) )
- {
- db.Entry( entity ).State = EntityState.Deleted;
- //db.Set<T>().Remove(entity);
- db.SaveChanges();
- }
- OnChangedProperty();
- }
- public virtual void Update( T entity )
- {
- using ( var db = new OHVDbContext( "OHVDb" ) )
- {
- db.Entry( entity ).State = EntityState.Modified;
- db.SaveChanges();
- }
- OnChangedProperty();
- }
- public void Clean()
- {
- using ( var db = new OHVDbContext( "OHVDb" ) )
- {
- db.Set<T>().RemoveRange( db.Set<T>() );
- db.SaveChanges();
- }
- OnChangedProperty();
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////
- public interface IGenericRepository<T> where T : class
- {
- event Action OnChangeTable;
- int Count { get; }
- IEnumerable<T> GetAll();
- IEnumerable<T> Get( Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = "" );
- T GetById( object id );
- void Insert( T obj );
- void Update( T obj );
- void Delete( object id );
- void Delete( Expression<Func<T, bool>> filter = null );
- void Clean();
- void Save();
- void Dispose();
- }
- class GenericRepository<T> : IGenericRepository<T> where T : class
- {
- protected Logger logger = Logger.GetLogger();
- private OHVDbContext _context = null;
- private DbSet<T> table = null;
- public event Action OnChangeTable;
- object lockObj = new object();
- public int Count { get => GetAll().Count(); }
- public GenericRepository()
- {
- this._context = new OHVDbContext( "OHVDb" );
- this.table = this._context.Set<T>();
- }
- public GenericRepository( OHVDbContext dbContext )
- {
- this._context = dbContext;
- this.table = _context.Set<T>();
- }
- public void Dispose()
- {
- this._context.Dispose();
- }
- public IEnumerable<T> GetAll()
- {
- //lock ( lockObj )
- return table.ToList();
- }
- public virtual IEnumerable<T> Get( Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = "" )
- {
- lock ( lockObj )
- {
- IQueryable<T> query = this.table;
- if ( filter != null )
- query = query.Where( filter );
- if ( includeProperties != null )
- {
- foreach ( var includeProperty in includeProperties.Split( new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries ) )
- query = query.Include( includeProperty );
- }
- if ( orderBy != null )
- return orderBy( query ).ToList();
- else
- return query.ToList();
- }
- }
- public T GetById( object id )
- {
- lock ( lockObj )
- return table.Find( id );
- }
- public void Insert( T obj )
- {
- lock ( lockObj )
- {
- table.Add( obj );
- this.Save();
- }
- }
- public void Update( T obj )
- {
- if ( obj == null )
- {
- logger.E( $"[DataBase] - Update obj is Null {this.table.GetType().Name}" );
- return;
- }
- lock ( lockObj )
- {
- table.Attach( obj );
- _context.Entry( obj ).State = EntityState.Modified;
- this.Save();
- }
- }
- public void Delete( object id )
- {
- T existing = table.Find( id );
- if ( existing == null )
- return;
- lock ( lockObj )
- {
- if ( _context.Entry( existing ).State == EntityState.Detached )
- {
- table.Attach( existing );
- }
- table.Remove( existing );
- this.Save();
- }
- }
- public void Delete( Expression<Func<T, bool>> filter )
- {
- lock ( lockObj )
- {
- var delList = this.table.Where( filter ).ToList();
- if ( delList.Count > 0 )
- {
- this.table.RemoveRange( delList );
- Save();
- }
- }
- }
- public void Clean()
- {
- lock ( lockObj )
- {
- var ll = this.GetAll();
- if (ll.Count() <= 0)
- return;
- table.RemoveRange( ll );
- this.Save();
- }
- }
- public void Save()
- {
- try
- {
- _context.SaveChanges();
- }
- catch ( DbEntityValidationException dbEx )
- {
- foreach ( var validationErrors in dbEx.EntityValidationErrors )
- {
- foreach ( var validationError in validationErrors.ValidationErrors )
- {
- logger.E( "Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage );
- }
- }
- return;
- }
- //catch ( DbUpdateConcurrencyException ex )
- //{
- // ex.Entries.Single().Reload();
- // logger.E( $"[DataBase] - DbUpdateConcurrencyException {this.table.GetType().Name}" );
- // return;
- //}
- this.OnChangeTable?.Invoke();
- //DelegateUtils.Invoke(OnChangeTable);
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////
- public interface IGenericRepositoryType1<T> where T : class
- {
- IQueryable<T> GetAll();
- IQueryable<T> FindBy( Expression<Func<T, bool>> predicate );
- void Add( T entity );
- void Delete( T entity );
- void Edit( T entity );
- void Save();
- }
- public abstract class GenericRepository<C, T> : IGenericRepositoryType1<T> where T : class where C : DbContext, new()
- {
- private C _entities = new C();
- public C Context
- {
- get { return _entities; }
- set { _entities = value; }
- }
- public virtual IQueryable<T> GetAll()
- {
- IQueryable<T> query = _entities.Set<T>();
- return query;
- }
- public IQueryable<T> FindBy( System.Linq.Expressions.Expression<Func<T, bool>> predicate )
- {
- IQueryable<T> query = _entities.Set<T>().Where( predicate );
- return query;
- }
- public virtual void Add( T entity )
- {
- _entities.Set<T>().Add( entity );
- }
- public virtual void Delete( T entity )
- {
- _entities.Set<T>().Remove( entity );
- }
- public virtual void Edit( T entity )
- {
- _entities.Entry( entity ).State = EntityState.Modified;
- }
- public virtual void Save()
- {
- _entities.SaveChanges();
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////
- public interface IRepository<T> where T : class
- {
- IEnumerable<T> GetAll();
- T GetById( object Id );
- T Insert( T obj );
- void Delete( object Id );
- T Update( T obj );
- void Save();
- }
- public class Repository<T> : IRepository<T> where T : class
- {
- private OHVDbContext context;
- private DbSet<T> dbSet;
- public Repository()
- {
- context = new OHVDbContext( "OHVDb" );
- dbSet = context.Set<T>();
- }
- public IEnumerable<T> GetAll()
- {
- return dbSet.ToList();
- }
- public T GetById( object id )
- {
- return dbSet.Find( id );
- }
- public T Insert( T obj )
- {
- dbSet.Add( obj );
- Save();
- return obj;
- }
- public void Delete( object id )
- {
- T entityToDelete = dbSet.Find( id );
- Delete( entityToDelete );
- }
- public void Delete( T entityToDelete )
- {
- if ( context.Entry( entityToDelete ).State == EntityState.Detached )
- {
- dbSet.Attach( entityToDelete );
- }
- dbSet.Remove( entityToDelete );
- }
- public T Update( T obj )
- {
- dbSet.Attach( obj );
- context.Entry( obj ).State = EntityState.Modified;
- Save();
- return obj;
- }
- public void Save()
- {
- try
- {
- context.SaveChanges();
- }
- catch ( DbEntityValidationException dbEx )
- {
- foreach ( var validationErrors in dbEx.EntityValidationErrors )
- {
- foreach ( var validationError in validationErrors.ValidationErrors )
- {
- System.Console.WriteLine( "Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage );
- }
- }
- }
- }
- protected virtual void Dispose( bool disposing )
- {
- if ( disposing )
- {
- if ( context != null )
- {
- context.Dispose();
- context = null;
- }
- }
- }
- }
- }
|