|
|
@@ -1,13 +1,11 @@
|
|
|
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 System.Threading;
|
|
|
using GSG.NET.Logging;
|
|
|
|
|
|
namespace OHV.SqliteDAL.DAL
|
|
|
@@ -19,7 +17,7 @@ namespace OHV.SqliteDAL.DAL
|
|
|
|
|
|
protected virtual void OnChangedProperty()
|
|
|
{
|
|
|
- ChangedProperty?.BeginInvoke( null, null );
|
|
|
+ ChangedProperty?.BeginInvoke(null, null);
|
|
|
}
|
|
|
|
|
|
public List<T> All
|
|
|
@@ -27,7 +25,7 @@ namespace OHV.SqliteDAL.DAL
|
|
|
get
|
|
|
{
|
|
|
List<T> ll = new List<T>();
|
|
|
- using ( var db = new OHVDbContext( "OHVDb" ) )
|
|
|
+ using (var db = new OHVDbContext("OHVDb"))
|
|
|
{
|
|
|
ll = db.Set<T>().ToList();
|
|
|
}
|
|
|
@@ -39,7 +37,7 @@ namespace OHV.SqliteDAL.DAL
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
- using ( var db = new OHVDbContext( "OHVDb" ) )
|
|
|
+ using (var db = new OHVDbContext("OHVDb"))
|
|
|
{
|
|
|
return db.Set<T>().Count();
|
|
|
}
|
|
|
@@ -47,63 +45,63 @@ namespace OHV.SqliteDAL.DAL
|
|
|
|
|
|
}
|
|
|
|
|
|
- public T GetK( object key )
|
|
|
+ public T GetK(object key)
|
|
|
{
|
|
|
- using ( var db = new OHVDbContext( "OHVDb" ) )
|
|
|
+ using (var db = new OHVDbContext("OHVDb"))
|
|
|
{
|
|
|
- return db.Set<T>().Find( key );
|
|
|
+ return db.Set<T>().Find(key);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public List<T> ListN( Expression<Func<T, bool>> where )
|
|
|
+ public List<T> ListN(Expression<Func<T, bool>> where)
|
|
|
{
|
|
|
- using ( var db = new OHVDbContext( "OHVDb" ) )
|
|
|
+ using (var db = new OHVDbContext("OHVDb"))
|
|
|
{
|
|
|
- return db.Set<T>().Where( where ).ToList();
|
|
|
+ return db.Set<T>().Where(where).ToList();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public virtual bool HasK( object key )
|
|
|
+ public virtual bool HasK(object key)
|
|
|
{
|
|
|
- return GetK( key ) == null ? false : true;
|
|
|
+ return GetK(key) == null ? false : true;
|
|
|
}
|
|
|
|
|
|
- public virtual bool HasN( Expression<Func<T, bool>> where )
|
|
|
+ public virtual bool HasN(Expression<Func<T, bool>> where)
|
|
|
{
|
|
|
List<T> ll = new List<T>();
|
|
|
- using ( var db = new OHVDbContext( "OHVDb" ) )
|
|
|
+ using (var db = new OHVDbContext("OHVDb"))
|
|
|
{
|
|
|
- ll = db.Set<T>().Where( where ).ToList();
|
|
|
+ ll = db.Set<T>().Where(where).ToList();
|
|
|
}
|
|
|
return ll.Count > 0;
|
|
|
}
|
|
|
|
|
|
- public void Add( T entity )
|
|
|
+ public void Add(T entity)
|
|
|
{
|
|
|
- using ( var db = new OHVDbContext( "OHVDb" ) )
|
|
|
+ using (var db = new OHVDbContext("OHVDb"))
|
|
|
{
|
|
|
- db.Set<T>().Add( entity );
|
|
|
+ db.Set<T>().Add(entity);
|
|
|
db.SaveChanges();
|
|
|
}
|
|
|
OnChangedProperty();
|
|
|
}
|
|
|
|
|
|
- public void Delete( T entity )
|
|
|
+ public void Delete(T entity)
|
|
|
{
|
|
|
- using ( var db = new OHVDbContext( "OHVDb" ) )
|
|
|
+ using (var db = new OHVDbContext("OHVDb"))
|
|
|
{
|
|
|
- db.Entry( entity ).State = EntityState.Deleted;
|
|
|
+ db.Entry(entity).State = EntityState.Deleted;
|
|
|
//db.Set<T>().Remove(entity);
|
|
|
db.SaveChanges();
|
|
|
}
|
|
|
OnChangedProperty();
|
|
|
}
|
|
|
|
|
|
- public virtual void Update( T entity )
|
|
|
+ public virtual void Update(T entity)
|
|
|
{
|
|
|
- using ( var db = new OHVDbContext( "OHVDb" ) )
|
|
|
+ using (var db = new OHVDbContext("OHVDb"))
|
|
|
{
|
|
|
- db.Entry( entity ).State = EntityState.Modified;
|
|
|
+ db.Entry(entity).State = EntityState.Modified;
|
|
|
db.SaveChanges();
|
|
|
}
|
|
|
OnChangedProperty();
|
|
|
@@ -111,9 +109,9 @@ namespace OHV.SqliteDAL.DAL
|
|
|
|
|
|
public void Clean()
|
|
|
{
|
|
|
- using ( var db = new OHVDbContext( "OHVDb" ) )
|
|
|
+ using (var db = new OHVDbContext("OHVDb"))
|
|
|
{
|
|
|
- db.Set<T>().RemoveRange( db.Set<T>() );
|
|
|
+ db.Set<T>().RemoveRange(db.Set<T>());
|
|
|
db.SaveChanges();
|
|
|
}
|
|
|
OnChangedProperty();
|
|
|
@@ -126,12 +124,12 @@ namespace OHV.SqliteDAL.DAL
|
|
|
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 );
|
|
|
+ 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();
|
|
|
@@ -152,11 +150,11 @@ namespace OHV.SqliteDAL.DAL
|
|
|
|
|
|
public GenericRepository()
|
|
|
{
|
|
|
- this._context = new OHVDbContext( "OHVDb" );
|
|
|
+ this._context = new OHVDbContext("OHVDb");
|
|
|
this.table = this._context.Set<T>();
|
|
|
}
|
|
|
|
|
|
- public GenericRepository( OHVDbContext dbContext )
|
|
|
+ public GenericRepository(OHVDbContext dbContext)
|
|
|
{
|
|
|
this._context = dbContext;
|
|
|
this.table = _context.Set<T>();
|
|
|
@@ -164,94 +162,93 @@ namespace OHV.SqliteDAL.DAL
|
|
|
|
|
|
public IEnumerable<T> GetAll()
|
|
|
{
|
|
|
- lock ( lockObj )
|
|
|
+ lock (this.lockObj)
|
|
|
return table.ToList();
|
|
|
}
|
|
|
|
|
|
- public virtual IEnumerable<T> Get( Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = "" )
|
|
|
+ public virtual IEnumerable<T> Get(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = "")
|
|
|
{
|
|
|
IQueryable<T> query = this.table;
|
|
|
|
|
|
- lock ( lockObj )
|
|
|
+ lock (this.lockObj)
|
|
|
{
|
|
|
- if ( filter != null )
|
|
|
- query = query.Where( filter );
|
|
|
+ query = query.Where(filter);
|
|
|
|
|
|
- if ( includeProperties != null )
|
|
|
+ if (includeProperties != null)
|
|
|
{
|
|
|
- foreach ( var includeProperty in includeProperties.Split( new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries ) )
|
|
|
- query = query.Include( includeProperty );
|
|
|
+ foreach (var includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
|
|
|
+ query = query.Include(includeProperty);
|
|
|
}
|
|
|
|
|
|
- if ( orderBy != null )
|
|
|
- return orderBy( query ).ToList();
|
|
|
+ if (orderBy != null)
|
|
|
+ return orderBy(query).ToList();
|
|
|
else
|
|
|
return query.ToList();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public T GetById( object id )
|
|
|
+ public T GetById(object id)
|
|
|
{
|
|
|
- lock ( lockObj )
|
|
|
- return table.Find( id );
|
|
|
+ lock (this.lockObj)
|
|
|
+ return table.Find(id);
|
|
|
}
|
|
|
|
|
|
- public void Insert( T obj )
|
|
|
+ public void Insert(T obj)
|
|
|
{
|
|
|
- lock ( lockObj )
|
|
|
+ lock (this.lockObj)
|
|
|
{
|
|
|
- table.Add( obj );
|
|
|
+ table.Add(obj);
|
|
|
this.Save();
|
|
|
}
|
|
|
|
|
|
this.OnChangeTable?.Invoke();
|
|
|
}
|
|
|
|
|
|
- public void Update( T obj )
|
|
|
+ public void Update(T obj)
|
|
|
{
|
|
|
- if ( obj == null )
|
|
|
+ if (obj == null)
|
|
|
{
|
|
|
- logger.E( $"[DataBase] - Update obj is Null {this.table.GetType().Name}" );
|
|
|
+ logger.E($"[DataBase] - Update obj is Null {this.table.GetType().Name}");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- lock ( lockObj )
|
|
|
+ lock (this.lockObj)
|
|
|
{
|
|
|
- table.Attach( obj );
|
|
|
- _context.Entry( obj ).State = EntityState.Modified;
|
|
|
+ table.Attach(obj);
|
|
|
+ _context.Entry(obj).State = EntityState.Modified;
|
|
|
this.Save();
|
|
|
}
|
|
|
|
|
|
this.OnChangeTable?.Invoke();
|
|
|
}
|
|
|
|
|
|
- public void Delete( object id )
|
|
|
+ public void Delete(object id)
|
|
|
{
|
|
|
- T existing = table.Find( id );
|
|
|
- if ( existing == null )
|
|
|
- return;
|
|
|
-
|
|
|
- lock ( lockObj )
|
|
|
+ lock (this.lockObj)
|
|
|
{
|
|
|
- if ( _context.Entry( existing ).State == EntityState.Detached )
|
|
|
+ T existing = table.Find(id);
|
|
|
+ if (existing == null)
|
|
|
+ return;
|
|
|
+
|
|
|
+ if (_context.Entry(existing).State == EntityState.Detached)
|
|
|
{
|
|
|
- table.Attach( existing );
|
|
|
+ table.Attach(existing);
|
|
|
}
|
|
|
|
|
|
- table.Remove( existing );
|
|
|
+ table.Remove(existing);
|
|
|
this.Save();
|
|
|
}
|
|
|
this.OnChangeTable?.Invoke();
|
|
|
}
|
|
|
|
|
|
- public void Delete( Expression<Func<T, bool>> filter )
|
|
|
+ public void Delete(Expression<Func<T, bool>> filter)
|
|
|
{
|
|
|
- lock ( lockObj )
|
|
|
+ lock (this.lockObj)
|
|
|
{
|
|
|
- var delList = this.table.Where( filter ).ToList();
|
|
|
- if ( delList.Any() && delList != null )
|
|
|
+ var delList = this.table.Where(filter).ToList();
|
|
|
+ if (delList.Any() && delList != null)
|
|
|
{
|
|
|
- this.table.RemoveRange( delList );
|
|
|
+ this.table.RemoveRange(delList);
|
|
|
Save();
|
|
|
}
|
|
|
}
|
|
|
@@ -260,13 +257,13 @@ namespace OHV.SqliteDAL.DAL
|
|
|
|
|
|
public void Clean()
|
|
|
{
|
|
|
- lock ( lockObj )
|
|
|
+ lock (this.lockObj)
|
|
|
{
|
|
|
var delList = table.ToList();
|
|
|
|
|
|
- if ( delList != null && delList.Any() )
|
|
|
+ if (delList != null && delList.Any())
|
|
|
{
|
|
|
- table.RemoveRange( delList );
|
|
|
+ table.RemoveRange(delList);
|
|
|
this.Save();
|
|
|
}
|
|
|
}
|
|
|
@@ -279,24 +276,23 @@ namespace OHV.SqliteDAL.DAL
|
|
|
try
|
|
|
{
|
|
|
_context.SaveChanges();
|
|
|
+ //_context.SaveChangesAsync();
|
|
|
+
|
|
|
}
|
|
|
- catch ( DbEntityValidationException dbEx )
|
|
|
+ catch (DbEntityValidationException dbEx)
|
|
|
{
|
|
|
- foreach ( var validationErrors in dbEx.EntityValidationErrors )
|
|
|
+ foreach (var validationErrors in dbEx.EntityValidationErrors)
|
|
|
{
|
|
|
- foreach ( var validationError in validationErrors.ValidationErrors )
|
|
|
+ foreach (var validationError in validationErrors.ValidationErrors)
|
|
|
{
|
|
|
- logger.E( "Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage );
|
|
|
+ logger.E("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- return;
|
|
|
}
|
|
|
- catch ( DbUpdateConcurrencyException ex )
|
|
|
+ catch (DbUpdateConcurrencyException ex)
|
|
|
{
|
|
|
ex.Entries.Single().Reload();
|
|
|
- logger.E( $"[DataBase] - DbUpdateConcurrencyException {this.table.GetType().Name}" );
|
|
|
- return;
|
|
|
+ logger.E($"[DataBase] - DbUpdateConcurrencyException {this.table.GetType().Name}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -311,10 +307,10 @@ namespace OHV.SqliteDAL.DAL
|
|
|
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 );
|
|
|
+ IQueryable<T> FindBy(Expression<Func<T, bool>> predicate);
|
|
|
+ void Add(T entity);
|
|
|
+ void Delete(T entity);
|
|
|
+ void Edit(T entity);
|
|
|
void Save();
|
|
|
}
|
|
|
|
|
|
@@ -333,25 +329,25 @@ namespace OHV.SqliteDAL.DAL
|
|
|
return query;
|
|
|
}
|
|
|
|
|
|
- public IQueryable<T> FindBy( System.Linq.Expressions.Expression<Func<T, bool>> predicate )
|
|
|
+ public IQueryable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
|
|
|
{
|
|
|
- IQueryable<T> query = _entities.Set<T>().Where( predicate );
|
|
|
+ IQueryable<T> query = _entities.Set<T>().Where(predicate);
|
|
|
return query;
|
|
|
}
|
|
|
|
|
|
- public virtual void Add( T entity )
|
|
|
+ public virtual void Add(T entity)
|
|
|
{
|
|
|
- _entities.Set<T>().Add( entity );
|
|
|
+ _entities.Set<T>().Add(entity);
|
|
|
}
|
|
|
|
|
|
- public virtual void Delete( T entity )
|
|
|
+ public virtual void Delete(T entity)
|
|
|
{
|
|
|
- _entities.Set<T>().Remove( entity );
|
|
|
+ _entities.Set<T>().Remove(entity);
|
|
|
}
|
|
|
|
|
|
- public virtual void Edit( T entity )
|
|
|
+ public virtual void Edit(T entity)
|
|
|
{
|
|
|
- _entities.Entry( entity ).State = EntityState.Modified;
|
|
|
+ _entities.Entry(entity).State = EntityState.Modified;
|
|
|
}
|
|
|
|
|
|
public virtual void Save()
|
|
|
@@ -364,10 +360,10 @@ namespace OHV.SqliteDAL.DAL
|
|
|
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 );
|
|
|
+ T GetById(object Id);
|
|
|
+ T Insert(T obj);
|
|
|
+ void Delete(object Id);
|
|
|
+ T Update(T obj);
|
|
|
void Save();
|
|
|
}
|
|
|
|
|
|
@@ -377,44 +373,44 @@ namespace OHV.SqliteDAL.DAL
|
|
|
private DbSet<T> dbSet;
|
|
|
public Repository()
|
|
|
{
|
|
|
- context = new OHVDbContext( "OHVDb" );
|
|
|
+ context = new OHVDbContext("OHVDb");
|
|
|
dbSet = context.Set<T>();
|
|
|
}
|
|
|
public IEnumerable<T> GetAll()
|
|
|
{
|
|
|
return dbSet.ToList();
|
|
|
}
|
|
|
- public T GetById( object id )
|
|
|
+ public T GetById(object id)
|
|
|
{
|
|
|
- return dbSet.Find( id );
|
|
|
+ return dbSet.Find(id);
|
|
|
}
|
|
|
- public T Insert( T obj )
|
|
|
+ public T Insert(T obj)
|
|
|
{
|
|
|
- dbSet.Add( obj );
|
|
|
+ dbSet.Add(obj);
|
|
|
Save();
|
|
|
return obj;
|
|
|
}
|
|
|
|
|
|
- public void Delete( object id )
|
|
|
+ public void Delete(object id)
|
|
|
{
|
|
|
- T entityToDelete = dbSet.Find( id );
|
|
|
- Delete( entityToDelete );
|
|
|
+ T entityToDelete = dbSet.Find(id);
|
|
|
+ Delete(entityToDelete);
|
|
|
}
|
|
|
|
|
|
- public void Delete( T entityToDelete )
|
|
|
+ public void Delete(T entityToDelete)
|
|
|
{
|
|
|
- if ( context.Entry( entityToDelete ).State == EntityState.Detached )
|
|
|
+ if (context.Entry(entityToDelete).State == EntityState.Detached)
|
|
|
{
|
|
|
- dbSet.Attach( entityToDelete );
|
|
|
+ dbSet.Attach(entityToDelete);
|
|
|
}
|
|
|
|
|
|
- dbSet.Remove( entityToDelete );
|
|
|
+ dbSet.Remove(entityToDelete);
|
|
|
}
|
|
|
|
|
|
- public T Update( T obj )
|
|
|
+ public T Update(T obj)
|
|
|
{
|
|
|
- dbSet.Attach( obj );
|
|
|
- context.Entry( obj ).State = EntityState.Modified;
|
|
|
+ dbSet.Attach(obj);
|
|
|
+ context.Entry(obj).State = EntityState.Modified;
|
|
|
Save();
|
|
|
return obj;
|
|
|
}
|
|
|
@@ -425,23 +421,23 @@ namespace OHV.SqliteDAL.DAL
|
|
|
{
|
|
|
context.SaveChanges();
|
|
|
}
|
|
|
- catch ( DbEntityValidationException dbEx )
|
|
|
+ catch (DbEntityValidationException dbEx)
|
|
|
{
|
|
|
- foreach ( var validationErrors in dbEx.EntityValidationErrors )
|
|
|
+ foreach (var validationErrors in dbEx.EntityValidationErrors)
|
|
|
{
|
|
|
- foreach ( var validationError in validationErrors.ValidationErrors )
|
|
|
+ foreach (var validationError in validationErrors.ValidationErrors)
|
|
|
{
|
|
|
- System.Console.WriteLine( "Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage );
|
|
|
+ System.Console.WriteLine("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- protected virtual void Dispose( bool disposing )
|
|
|
+ protected virtual void Dispose(bool disposing)
|
|
|
{
|
|
|
- if ( disposing )
|
|
|
+ if (disposing)
|
|
|
{
|
|
|
- if ( context != null )
|
|
|
+ if (context != null)
|
|
|
{
|
|
|
context.Dispose();
|
|
|
context = null;
|