|
|
@@ -1,6 +1,7 @@
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Data.Entity;
|
|
|
+using System.Data.Entity.Core.Objects;
|
|
|
using System.Linq;
|
|
|
using System.Linq.Expressions;
|
|
|
using System.Text;
|
|
|
@@ -11,10 +12,10 @@ namespace OHV.SqliteDAL.DAL
|
|
|
{
|
|
|
public interface IRepository<TEntity> where TEntity : class
|
|
|
{
|
|
|
- void Create(TEntity entity);
|
|
|
- void Delete(TEntity entity);
|
|
|
- void Delete(Guid id);
|
|
|
- void Edit(TEntity entity);
|
|
|
+ void Create( TEntity entity );
|
|
|
+ void Delete( TEntity entity );
|
|
|
+ void Delete( Guid id );
|
|
|
+ void Edit( TEntity entity );
|
|
|
}
|
|
|
|
|
|
public class GenericDAL<T> where T : class
|
|
|
@@ -24,7 +25,7 @@ namespace OHV.SqliteDAL.DAL
|
|
|
|
|
|
protected virtual void OnChangedProperty()
|
|
|
{
|
|
|
- ChangedProperty?.BeginInvoke(null, null);
|
|
|
+ ChangedProperty?.BeginInvoke( null, null );
|
|
|
}
|
|
|
|
|
|
public List<T> All
|
|
|
@@ -32,7 +33,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();
|
|
|
}
|
|
|
@@ -44,7 +45,7 @@ namespace OHV.SqliteDAL.DAL
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
- using (var db = new OHVDbContext("OHVDb"))
|
|
|
+ using ( var db = new OHVDbContext( "OHVDb" ) )
|
|
|
{
|
|
|
return db.Set<T>().Count();
|
|
|
}
|
|
|
@@ -52,63 +53,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();
|
|
|
@@ -116,9 +117,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();
|
|
|
@@ -134,6 +135,8 @@ namespace OHV.SqliteDAL.DAL
|
|
|
|
|
|
public event Action OnChangeTable;
|
|
|
|
|
|
+ object lockObj = new object();
|
|
|
+
|
|
|
public int Count { get => GetAll().Count(); }
|
|
|
|
|
|
public GenericRepository()
|
|
|
@@ -142,7 +145,7 @@ namespace OHV.SqliteDAL.DAL
|
|
|
this.table = this._context.Set<T>();
|
|
|
}
|
|
|
|
|
|
- public GenericRepository(OHVDbContext dbContext )
|
|
|
+ public GenericRepository( OHVDbContext dbContext )
|
|
|
{
|
|
|
this._context = dbContext;
|
|
|
this.table = _context.Set<T>();
|
|
|
@@ -151,6 +154,7 @@ namespace OHV.SqliteDAL.DAL
|
|
|
public IEnumerable<T> GetAll()
|
|
|
{
|
|
|
return table.ToList();
|
|
|
+
|
|
|
}
|
|
|
|
|
|
public virtual IEnumerable<T> Get( Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = "" )
|
|
|
@@ -172,25 +176,25 @@ namespace OHV.SqliteDAL.DAL
|
|
|
return query.ToList();
|
|
|
}
|
|
|
|
|
|
- public T GetById(object id)
|
|
|
+ public T GetById( object id )
|
|
|
{
|
|
|
- return table.Find(id);
|
|
|
+ return table.Find( id );
|
|
|
}
|
|
|
- public void Insert(T obj)
|
|
|
+ public void Insert( T obj )
|
|
|
{
|
|
|
- table.Add(obj);
|
|
|
+ table.Add( obj );
|
|
|
this.Save();
|
|
|
}
|
|
|
- public void Update(T obj)
|
|
|
+ public void Update( T obj )
|
|
|
{
|
|
|
- table.Attach(obj);
|
|
|
- _context.Entry(obj).State = EntityState.Modified;
|
|
|
+ table.Attach( obj );
|
|
|
+ _context.Entry( obj ).State = EntityState.Modified;
|
|
|
this.Save();
|
|
|
}
|
|
|
- public void Delete(object id)
|
|
|
+ public void Delete( object id )
|
|
|
{
|
|
|
- T existing = table.Find(id);
|
|
|
- table.Remove(existing);
|
|
|
+ T existing = table.Find( id );
|
|
|
+ table.Remove( existing );
|
|
|
this.Save();
|
|
|
}
|
|
|
public void Delete( Expression<Func<T, bool>> filter )
|
|
|
@@ -205,13 +209,16 @@ namespace OHV.SqliteDAL.DAL
|
|
|
|
|
|
public void Clean()
|
|
|
{
|
|
|
- table.RemoveRange( table);
|
|
|
+ table.RemoveRange( table );
|
|
|
this.Save();
|
|
|
}
|
|
|
public void Save()
|
|
|
{
|
|
|
- _context.SaveChanges();
|
|
|
- this.OnChangeTable?.BeginInvoke(null, null);
|
|
|
+ lock ( this.lockObj )
|
|
|
+ {
|
|
|
+ _context.SaveChanges();
|
|
|
+ this.OnChangeTable?.BeginInvoke( null, null );
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -221,10 +228,10 @@ namespace OHV.SqliteDAL.DAL
|
|
|
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);
|
|
|
+ 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();
|