mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 20:08:15 +08:00
移除 FreeSql.DbContext,成为独立项目
This commit is contained in:
@ -1,143 +0,0 @@
|
||||
using FreeSql.Extensions.EntityUtil;
|
||||
using FreeSql.Internal.Model;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FreeSql {
|
||||
public abstract class BaseRepository<TEntity> : IRepository<TEntity>
|
||||
where TEntity : class {
|
||||
|
||||
internal IFreeSql _fsql;
|
||||
internal RepositoryDbContext<TEntity> _db;
|
||||
public IDataFilter<TEntity> DataFilter { get; } = new DataFilter<TEntity>();
|
||||
Func<string, string> _asTableVal;
|
||||
protected Func<string, string> AsTable {
|
||||
get => _asTableVal;
|
||||
set {
|
||||
_asTableVal = value;
|
||||
AsTableSelect = value == null ? null : new Func<Type, string, string>((a, b) => a == EntityType ? value(b) : null);
|
||||
}
|
||||
}
|
||||
internal Func<string, string> AsTableInternal => AsTable;
|
||||
protected Func<Type, string, string> AsTableSelect { get; private set; }
|
||||
internal Func<Type, string, string> AsTableSelectInternal => AsTableSelect;
|
||||
|
||||
protected BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable = null) {
|
||||
_fsql = fsql;
|
||||
DataFilterUtil.SetRepositoryDataFilter(this, null);
|
||||
DataFilter.Apply("", filter);
|
||||
AsTable = asTable;
|
||||
_db = new RepositoryDbContext<TEntity>(_fsql, this);
|
||||
}
|
||||
|
||||
~BaseRepository() {
|
||||
this.Dispose();
|
||||
}
|
||||
bool _isdisposed = false;
|
||||
public void Dispose() {
|
||||
if (_isdisposed) return;
|
||||
try {
|
||||
_db.Dispose();
|
||||
this.DataFilter.Dispose();
|
||||
} finally {
|
||||
_isdisposed = true;
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
|
||||
public IUnitOfWork UnitOfWork { get; set; }
|
||||
public Type EntityType => _db.DbSet._entityTypeInternal;
|
||||
public IUpdate<TEntity> UpdateDiy => _db.DbSet.OrmUpdateInternal(null);
|
||||
|
||||
public ISelect<TEntity> Select => _db.DbSet.OrmSelectInternal(null);
|
||||
public ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => _db.DbSet.OrmSelectInternal(null).Where(exp);
|
||||
public ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp) => _db.DbSet.OrmSelectInternal(null).WhereIf(condition, exp);
|
||||
|
||||
public int Delete(Expression<Func<TEntity, bool>> predicate) => _db.DbSet.OrmDeleteInternal(null).Where(predicate).ExecuteAffrows();
|
||||
public Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate) => _db.DbSet.OrmDeleteInternal(null).Where(predicate).ExecuteAffrowsAsync();
|
||||
|
||||
public int Delete(TEntity entity) {
|
||||
_db.DbSet.Remove(entity);
|
||||
return _db.SaveChanges();
|
||||
}
|
||||
public Task<int> DeleteAsync(TEntity entity) {
|
||||
_db.DbSet.Remove(entity);
|
||||
return _db.SaveChangesAsync();
|
||||
}
|
||||
public int Delete(IEnumerable<TEntity> entitys) {
|
||||
_db.DbSet.RemoveRange(entitys);
|
||||
return _db.SaveChanges();
|
||||
}
|
||||
public Task<int> DeleteAsync(IEnumerable<TEntity> entitys) {
|
||||
_db.DbSet.RemoveRange(entitys);
|
||||
return _db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public virtual TEntity Insert(TEntity entity) {
|
||||
_db.DbSet.Add(entity);
|
||||
_db.SaveChanges();
|
||||
return entity;
|
||||
}
|
||||
async public virtual Task<TEntity> InsertAsync(TEntity entity) {
|
||||
await _db.DbSet.AddAsync(entity);
|
||||
_db.SaveChanges();
|
||||
return entity;
|
||||
}
|
||||
public virtual List<TEntity> Insert(IEnumerable<TEntity> entitys) {
|
||||
_db.DbSet.AddRange(entitys);
|
||||
_db.SaveChanges();
|
||||
return entitys.ToList();
|
||||
}
|
||||
async public virtual Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entitys) {
|
||||
await _db.DbSet.AddRangeAsync(entitys);
|
||||
await _db.SaveChangesAsync();
|
||||
return entitys.ToList();
|
||||
}
|
||||
|
||||
public int Update(TEntity entity) {
|
||||
_db.DbSet.Update(entity);
|
||||
return _db.SaveChanges();
|
||||
}
|
||||
public Task<int> UpdateAsync(TEntity entity) {
|
||||
_db.DbSet.Update(entity);
|
||||
return _db.SaveChangesAsync();
|
||||
}
|
||||
public int Update(IEnumerable<TEntity> entitys) {
|
||||
_db.DbSet.UpdateRange(entitys);
|
||||
return _db.SaveChanges();
|
||||
}
|
||||
public Task<int> UpdateAsync(IEnumerable<TEntity> entitys) {
|
||||
_db.DbSet.UpdateRange(entitys);
|
||||
return _db.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class BaseRepository<TEntity, TKey> : BaseRepository<TEntity>, IRepository<TEntity, TKey>
|
||||
where TEntity : class {
|
||||
|
||||
public BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable = null) : base(fsql, filter, asTable) {
|
||||
}
|
||||
|
||||
public int Delete(TKey id) {
|
||||
var stateKey = string.Concat(id);
|
||||
if (_db.DbSet._statesInternal.ContainsKey(stateKey))
|
||||
_db.DbSet._statesInternal.Remove(stateKey);
|
||||
return _db.DbSet.OrmDeleteInternal(id).ExecuteAffrows();
|
||||
}
|
||||
public Task<int> DeleteAsync(TKey id) {
|
||||
var stateKey = string.Concat(id);
|
||||
if (_db.DbSet._statesInternal.ContainsKey(stateKey))
|
||||
_db.DbSet._statesInternal.Remove(stateKey);
|
||||
return _db.DbSet.OrmDeleteInternal(id).ExecuteAffrowsAsync();
|
||||
}
|
||||
|
||||
public TEntity Find(TKey id) => _db.DbSet.OrmSelectInternal(id).ToOne();
|
||||
public Task<TEntity> FindAsync(TKey id) => _db.DbSet.OrmSelectInternal(id).ToOneAsync();
|
||||
|
||||
public TEntity Get(TKey id) => Find(id);
|
||||
public Task<TEntity> GetAsync(TKey id) => FindAsync(id);
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FreeSql {
|
||||
public class DefaultRepository<TEntity, TKey> :
|
||||
BaseRepository<TEntity, TKey>
|
||||
where TEntity : class {
|
||||
|
||||
public DefaultRepository(IFreeSql fsql) : base(fsql, null, null) {
|
||||
|
||||
}
|
||||
|
||||
public DefaultRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter) : base(fsql, filter, null) {
|
||||
}
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FreeSql {
|
||||
public class GuidRepository<TEntity> :
|
||||
BaseRepository<TEntity, Guid>
|
||||
where TEntity : class {
|
||||
|
||||
public GuidRepository(IFreeSql fsql) : this(fsql, null, null) {
|
||||
|
||||
}
|
||||
public GuidRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable) : base(fsql, filter, asTable) {
|
||||
}
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FreeSql {
|
||||
public interface IBasicRepository<TEntity> : IReadOnlyRepository<TEntity>
|
||||
where TEntity : class {
|
||||
TEntity Insert(TEntity entity);
|
||||
List<TEntity> Insert(IEnumerable<TEntity> entitys);
|
||||
Task<TEntity> InsertAsync(TEntity entity);
|
||||
Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entitys);
|
||||
|
||||
int Update(TEntity entity);
|
||||
int Update(IEnumerable<TEntity> entitys);
|
||||
Task<int> UpdateAsync(TEntity entity);
|
||||
Task<int> UpdateAsync(IEnumerable<TEntity> entitys);
|
||||
|
||||
IUpdate<TEntity> UpdateDiy { get; }
|
||||
|
||||
int Delete(TEntity entity);
|
||||
int Delete(IEnumerable<TEntity> entitys);
|
||||
Task<int> DeleteAsync(TEntity entity);
|
||||
Task<int> DeleteAsync(IEnumerable<TEntity> entitys);
|
||||
}
|
||||
|
||||
public interface IBasicRepository<TEntity, TKey> : IBasicRepository<TEntity>, IReadOnlyRepository<TEntity, TKey>
|
||||
where TEntity : class {
|
||||
int Delete(TKey id);
|
||||
|
||||
Task<int> DeleteAsync(TKey id);
|
||||
}
|
||||
}
|
||||
|
@ -1,27 +0,0 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FreeSql {
|
||||
public interface IReadOnlyRepository<TEntity> : IRepository
|
||||
where TEntity : class {
|
||||
|
||||
IDataFilter<TEntity> DataFilter { get; }
|
||||
|
||||
ISelect<TEntity> Select { get; }
|
||||
|
||||
ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp);
|
||||
ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp);
|
||||
}
|
||||
|
||||
public interface IReadOnlyRepository<TEntity, TKey> : IReadOnlyRepository<TEntity>
|
||||
where TEntity : class {
|
||||
TEntity Get(TKey id);
|
||||
|
||||
Task<TEntity> GetAsync(TKey id);
|
||||
|
||||
TEntity Find(TKey id);
|
||||
|
||||
Task<TEntity> FindAsync(TKey id);
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FreeSql {
|
||||
|
||||
public interface IRepository : IDisposable {
|
||||
Type EntityType { get; }
|
||||
IUnitOfWork UnitOfWork { get; set; }
|
||||
}
|
||||
|
||||
public interface IRepository<TEntity> : IReadOnlyRepository<TEntity>, IBasicRepository<TEntity>
|
||||
where TEntity : class {
|
||||
int Delete(Expression<Func<TEntity, bool>> predicate);
|
||||
|
||||
Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate);
|
||||
}
|
||||
|
||||
public interface IRepository<TEntity, TKey> : IRepository<TEntity>, IReadOnlyRepository<TEntity, TKey>, IBasicRepository<TEntity, TKey>
|
||||
where TEntity : class {
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user