mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-12-16 07:15:47 +08:00
- FreeSql.Repository 增加 filter 参数,现实数据过滤 + 验证;
如:var postRepos = fsql.GetGuidRepository<Post>(a => a.TopicId == 1); postRepos CURD 方法都会以 lambad 条件作为查询或验证,Update/Insert验证错误时会抛出异常。 - ISelect 增加 First/FirstAsync;
This commit is contained in:
@@ -9,25 +9,35 @@ namespace FreeSql {
|
||||
where TEntity : class {
|
||||
|
||||
protected IFreeSql _fsql;
|
||||
protected Expression<Func<TEntity, bool>> _filter;
|
||||
protected Func<TEntity, bool> _filterCompile;
|
||||
|
||||
public BaseRepository(IFreeSql fsql) : base() {
|
||||
_fsql = fsql;
|
||||
if (_fsql == null) throw new NullReferenceException("fsql 参数不可为空");
|
||||
public BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter) : base() {
|
||||
_fsql = fsql ?? throw new NullReferenceException("fsql 参数不可为空");
|
||||
_filter = filter;
|
||||
_filterCompile = filter?.Compile();
|
||||
}
|
||||
|
||||
public ISelect<TEntity> Select => _fsql.Select<TEntity>();
|
||||
public ISelect<TEntity> Select => _fsql.Select<TEntity>().Where(_filter);
|
||||
|
||||
public IUpdate<TEntity> UpdateDiy => _fsql.Update<TEntity>();
|
||||
public IUpdate<TEntity> UpdateDiy => _fsql.Update<TEntity>().Where(_filter);
|
||||
|
||||
public int Delete(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(predicate).ExecuteAffrows();
|
||||
public int Delete(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(_filter).Where(predicate).ExecuteAffrows();
|
||||
|
||||
public int Delete(TEntity entity) => _fsql.Delete<TEntity>(entity).ExecuteAffrows();
|
||||
public int Delete(TEntity entity) {
|
||||
ValidatorEntityAndThrow(entity);
|
||||
return _fsql.Delete<TEntity>(entity).Where(_filter).ExecuteAffrows();
|
||||
}
|
||||
|
||||
public Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(predicate).ExecuteAffrowsAsync();
|
||||
public Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(_filter).Where(predicate).ExecuteAffrowsAsync();
|
||||
|
||||
public Task<int> DeleteAsync(TEntity entity) => _fsql.Delete<TEntity>(entity).ExecuteAffrowsAsync();
|
||||
public Task<int> DeleteAsync(TEntity entity) {
|
||||
ValidatorEntityAndThrow(entity);
|
||||
return _fsql.Delete<TEntity>(entity).Where(_filter).ExecuteAffrowsAsync();
|
||||
}
|
||||
|
||||
public virtual TEntity Insert(TEntity entity) {
|
||||
ValidatorEntityAndThrow(entity);
|
||||
switch (_fsql.Ado.DataType) {
|
||||
case DataType.SqlServer:
|
||||
case DataType.PostgreSQL:
|
||||
@@ -40,11 +50,12 @@ namespace FreeSql {
|
||||
}
|
||||
}
|
||||
|
||||
public virtual List<TEntity> Insert(IEnumerable<TEntity> entity) {
|
||||
public virtual List<TEntity> Insert(IEnumerable<TEntity> entitys) {
|
||||
ValidatorEntityAndThrow(entitys);
|
||||
switch (_fsql.Ado.DataType) {
|
||||
case DataType.SqlServer:
|
||||
case DataType.PostgreSQL:
|
||||
return _fsql.Insert<TEntity>().AppendData(entity).ExecuteInserted();
|
||||
return _fsql.Insert<TEntity>().AppendData(entitys).ExecuteInserted();
|
||||
case DataType.MySql:
|
||||
case DataType.Oracle:
|
||||
case DataType.Sqlite:
|
||||
@@ -54,6 +65,7 @@ namespace FreeSql {
|
||||
}
|
||||
|
||||
async public virtual Task<TEntity> InsertAsync(TEntity entity) {
|
||||
ValidatorEntityAndThrow(entity);
|
||||
switch (_fsql.Ado.DataType) {
|
||||
case DataType.SqlServer:
|
||||
case DataType.PostgreSQL:
|
||||
@@ -66,11 +78,12 @@ namespace FreeSql {
|
||||
}
|
||||
}
|
||||
|
||||
public virtual Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entity) {
|
||||
public virtual Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entitys) {
|
||||
ValidatorEntityAndThrow(entitys);
|
||||
switch (_fsql.Ado.DataType) {
|
||||
case DataType.SqlServer:
|
||||
case DataType.PostgreSQL:
|
||||
return _fsql.Insert<TEntity>().AppendData(entity).ExecuteInsertedAsync();
|
||||
return _fsql.Insert<TEntity>().AppendData(entitys).ExecuteInsertedAsync();
|
||||
case DataType.MySql:
|
||||
case DataType.Oracle:
|
||||
case DataType.Sqlite:
|
||||
@@ -79,24 +92,37 @@ namespace FreeSql {
|
||||
}
|
||||
}
|
||||
|
||||
public int Update(TEntity entity) => _fsql.Update<TEntity>().SetSource(entity).ExecuteAffrows();
|
||||
public int Update(TEntity entity) {
|
||||
ValidatorEntityAndThrow(entity);
|
||||
return _fsql.Update<TEntity>().SetSource(entity).Where(_filter).ExecuteAffrows();
|
||||
}
|
||||
|
||||
public Task<int> UpdateAsync(TEntity entity) => _fsql.Update<TEntity>().SetSource(entity).ExecuteAffrowsAsync();
|
||||
public Task<int> UpdateAsync(TEntity entity) {
|
||||
ValidatorEntityAndThrow(entity);
|
||||
return _fsql.Update<TEntity>().SetSource(entity).Where(_filter).ExecuteAffrowsAsync();
|
||||
}
|
||||
|
||||
protected void ValidatorEntityAndThrow(TEntity entity) => ValidatorEntityAndThrow(new[] { entity });
|
||||
protected virtual void ValidatorEntityAndThrow(IEnumerable<TEntity> entitys) {
|
||||
foreach (var entity in entitys) {
|
||||
if (_filterCompile?.Invoke(entity) == false) throw new Exception($"FreeSql.Repository Insert 失败,因为设置了 {_filter},插入的数据不符合");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class BaseRepository<TEntity, TKey> : BaseRepository<TEntity>, IRepository<TEntity, TKey>
|
||||
where TEntity : class {
|
||||
|
||||
public BaseRepository(IFreeSql fsql) : base(fsql) {
|
||||
public BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter) : base(fsql, filter) {
|
||||
}
|
||||
|
||||
public int Delete(TKey id) => _fsql.Delete<TEntity>(id).ExecuteAffrows();
|
||||
public int Delete(TKey id) => _fsql.Delete<TEntity>(id).Where(_filter).ExecuteAffrows();
|
||||
|
||||
public Task<int> DeleteAsync(TKey id) => _fsql.Delete<TEntity>(id).ExecuteAffrowsAsync();
|
||||
public Task<int> DeleteAsync(TKey id) => _fsql.Delete<TEntity>(id).Where(_filter).ExecuteAffrowsAsync();
|
||||
|
||||
public TEntity Find(TKey id) => _fsql.Select<TEntity>(id).ToOne();
|
||||
public TEntity Find(TKey id) => _fsql.Select<TEntity>(id).Where(_filter).ToOne();
|
||||
|
||||
public Task<TEntity> FindAsync(TKey id) => _fsql.Select<TEntity>(id).ToOneAsync();
|
||||
public Task<TEntity> FindAsync(TKey id) => _fsql.Select<TEntity>(id).Where(_filter).ToOneAsync();
|
||||
|
||||
public TEntity Get(TKey id) => Find(id);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user