mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 20:08:15 +08:00
- 调整 Repository 接口定义,合并为一个 IBaseRepository;
This commit is contained in:
@ -18,19 +18,8 @@ namespace FreeSql
|
||||
internal RepositoryDbSet<TEntity> _dbset => _dbsetPriv ?? (_dbsetPriv = _db.Set<TEntity>() as RepositoryDbSet<TEntity>);
|
||||
|
||||
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;
|
||||
internal Func<string, string> AsTableValueInternal { get; private set; }
|
||||
internal Func<Type, string, string> AsTableSelectValueInternal { get; private set; }
|
||||
|
||||
protected void ApplyDataFilter(string name, Expression<Func<TEntity, bool>> exp) => DataFilter.Apply(name, exp);
|
||||
|
||||
@ -39,7 +28,7 @@ namespace FreeSql
|
||||
Orm = fsql;
|
||||
DataFilterUtil.SetRepositoryDataFilter(this, null);
|
||||
DataFilter.Apply("", filter);
|
||||
AsTable = asTable;
|
||||
AsTable(asTable);
|
||||
}
|
||||
|
||||
~BaseRepository() => this.Dispose();
|
||||
@ -60,6 +49,11 @@ namespace FreeSql
|
||||
}
|
||||
public Type EntityType => _dbsetPriv?.EntityType ?? typeof(TEntity);
|
||||
public void AsType(Type entityType) => _dbset.AsType(entityType);
|
||||
public void AsTable(Func<string, string> rule)
|
||||
{
|
||||
AsTableValueInternal = rule;
|
||||
AsTableSelectValueInternal = rule == null ? null : new Func<Type, string, string>((a, b) => a == EntityType ? rule(b) : null);
|
||||
}
|
||||
public DbContextOptions DbContextOptions { get => _db.Options; set => _db.Options = value; }
|
||||
|
||||
public IFreeSql Orm { get; private set; }
|
||||
@ -125,7 +119,7 @@ namespace FreeSql
|
||||
|
||||
public void Attach(TEntity data) => _db.Attach(data);
|
||||
public void Attach(IEnumerable<TEntity> data) => _db.AttachRange(data);
|
||||
public IBasicRepository<TEntity> AttachOnlyPrimary(TEntity data)
|
||||
public IBaseRepository<TEntity> AttachOnlyPrimary(TEntity data)
|
||||
{
|
||||
_db.AttachOnlyPrimary(data);
|
||||
return this;
|
||||
@ -149,10 +143,7 @@ namespace FreeSql
|
||||
public abstract partial class BaseRepository<TEntity, TKey> : BaseRepository<TEntity>, IBaseRepository<TEntity, TKey>
|
||||
where TEntity : class
|
||||
{
|
||||
|
||||
public BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable = null) : base(fsql, filter, asTable)
|
||||
{
|
||||
}
|
||||
public BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable = null) : base(fsql, filter, asTable) { }
|
||||
|
||||
TEntity CheckTKeyAndReturnIdEntity(TKey id)
|
||||
{
|
||||
@ -167,9 +158,7 @@ namespace FreeSql
|
||||
}
|
||||
|
||||
public int Delete(TKey id) => Delete(CheckTKeyAndReturnIdEntity(id));
|
||||
|
||||
public TEntity Find(TKey id) => _dbset.OrmSelectInternal(CheckTKeyAndReturnIdEntity(id)).ToOne();
|
||||
|
||||
public TEntity Get(TKey id) => Find(id);
|
||||
}
|
||||
}
|
||||
|
@ -73,11 +73,8 @@ namespace FreeSql
|
||||
|
||||
partial class BaseRepository<TEntity, TKey>
|
||||
{
|
||||
|
||||
public Task<int> DeleteAsync(TKey id) => DeleteAsync(CheckTKeyAndReturnIdEntity(id));
|
||||
|
||||
public Task<TEntity> FindAsync(TKey id) => _dbset.OrmSelectInternal(CheckTKeyAndReturnIdEntity(id)).ToOneAsync();
|
||||
|
||||
public Task<TEntity> GetAsync(TKey id) => FindAsync(id);
|
||||
}
|
||||
}
|
||||
|
@ -3,18 +3,15 @@ using System.Linq.Expressions;
|
||||
|
||||
namespace FreeSql
|
||||
{
|
||||
public class DefaultRepository<TEntity, TKey> :
|
||||
BaseRepository<TEntity, TKey>
|
||||
where TEntity : class
|
||||
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) { }
|
||||
}
|
||||
|
||||
public DefaultRepository(IFreeSql fsql) : base(fsql, null, null)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public DefaultRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter) : base(fsql, filter, null)
|
||||
{
|
||||
}
|
||||
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,19 +0,0 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
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,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@ -17,6 +18,11 @@ namespace FreeSql
|
||||
/// <param name="entityType"></param>
|
||||
/// <returns></returns>
|
||||
void AsType(Type entityType);
|
||||
/// <summary>
|
||||
/// 分表规则,参数:旧表名;返回:新表名 https://github.com/2881099/FreeSql/wiki/Repository
|
||||
/// </summary>
|
||||
/// <param name="rule"></param>
|
||||
void AsTable(Func<string, string> rule);
|
||||
|
||||
/// <summary>
|
||||
/// 设置 DbContext 选项
|
||||
@ -24,19 +30,83 @@ namespace FreeSql
|
||||
DbContextOptions DbContextOptions { get; set; }
|
||||
}
|
||||
|
||||
public interface IBaseRepository<TEntity> : IReadOnlyRepository<TEntity>, IBasicRepository<TEntity>
|
||||
public interface IBaseRepository<TEntity> : IBaseRepository
|
||||
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);
|
||||
|
||||
TEntity Insert(TEntity entity);
|
||||
List<TEntity> Insert(IEnumerable<TEntity> entitys);
|
||||
|
||||
/// <summary>
|
||||
/// 清空状态数据
|
||||
/// </summary>
|
||||
void FlushState();
|
||||
/// <summary>
|
||||
/// 附加实体,可用于不查询就更新或删除
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
void Attach(TEntity entity);
|
||||
void Attach(IEnumerable<TEntity> entity);
|
||||
/// <summary>
|
||||
/// 附加实体,并且只附加主键值,可用于不更新属性值为null或默认值的字段
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
IBaseRepository<TEntity> AttachOnlyPrimary(TEntity data);
|
||||
|
||||
int Update(TEntity entity);
|
||||
int Update(IEnumerable<TEntity> entitys);
|
||||
|
||||
TEntity InsertOrUpdate(TEntity entity);
|
||||
/// <summary>
|
||||
/// 保存实体的指定 ManyToMany/OneToMany 导航属性(完整对比)<para></para>
|
||||
/// 场景:在关闭级联保存功能之后,手工使用本方法<para></para>
|
||||
/// 例子:保存商品的 OneToMany 集合属性,SaveMany(goods, "Skus")<para></para>
|
||||
/// 当 goods.Skus 为空(非null)时,会删除表中已存在的所有数据<para></para>
|
||||
/// 当 goods.Skus 不为空(非null)时,添加/更新后,删除表中不存在 Skus 集合属性的所有记录
|
||||
/// </summary>
|
||||
/// <param name="entity">实体对象</param>
|
||||
/// <param name="propertyName">属性名</param>
|
||||
void SaveMany(TEntity entity, string propertyName);
|
||||
|
||||
IUpdate<TEntity> UpdateDiy { get; }
|
||||
|
||||
int Delete(TEntity entity);
|
||||
int Delete(IEnumerable<TEntity> entitys);
|
||||
int Delete(Expression<Func<TEntity, bool>> predicate);
|
||||
|
||||
#if net40
|
||||
#else
|
||||
Task<TEntity> InsertAsync(TEntity entity);
|
||||
Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entitys);
|
||||
|
||||
Task<int> UpdateAsync(TEntity entity);
|
||||
Task<int> UpdateAsync(IEnumerable<TEntity> entitys);
|
||||
Task<TEntity> InsertOrUpdateAsync(TEntity entity);
|
||||
Task SaveManyAsync(TEntity entity, string propertyName);
|
||||
|
||||
Task<int> DeleteAsync(TEntity entity);
|
||||
Task<int> DeleteAsync(IEnumerable<TEntity> entitys);
|
||||
Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate);
|
||||
#endif
|
||||
}
|
||||
|
||||
public interface IBaseRepository<TEntity, TKey> : IBaseRepository<TEntity>, IReadOnlyRepository<TEntity, TKey>, IBasicRepository<TEntity, TKey>
|
||||
public interface IBaseRepository<TEntity, TKey> : IBaseRepository<TEntity>
|
||||
where TEntity : class
|
||||
{
|
||||
TEntity Get(TKey id);
|
||||
TEntity Find(TKey id);
|
||||
int Delete(TKey id);
|
||||
|
||||
#if net40
|
||||
#else
|
||||
Task<TEntity> GetAsync(TKey id);
|
||||
Task<TEntity> FindAsync(TKey id);
|
||||
Task<int> DeleteAsync(TKey id);
|
||||
#endif
|
||||
}
|
||||
}
|
@ -1,74 +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);
|
||||
|
||||
/// <summary>
|
||||
/// 清空状态数据
|
||||
/// </summary>
|
||||
void FlushState();
|
||||
/// <summary>
|
||||
/// 附加实体,可用于不查询就更新或删除
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
void Attach(TEntity entity);
|
||||
void Attach(IEnumerable<TEntity> entity);
|
||||
/// <summary>
|
||||
/// 附加实体,并且只附加主键值,可用于不更新属性值为null或默认值的字段
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
IBasicRepository<TEntity> AttachOnlyPrimary(TEntity data);
|
||||
|
||||
int Update(TEntity entity);
|
||||
int Update(IEnumerable<TEntity> entitys);
|
||||
|
||||
TEntity InsertOrUpdate(TEntity entity);
|
||||
/// <summary>
|
||||
/// 保存实体的指定 ManyToMany/OneToMany 导航属性(完整对比)<para></para>
|
||||
/// 场景:在关闭级联保存功能之后,手工使用本方法<para></para>
|
||||
/// 例子:保存商品的 OneToMany 集合属性,SaveMany(goods, "Skus")<para></para>
|
||||
/// 当 goods.Skus 为空(非null)时,会删除表中已存在的所有数据<para></para>
|
||||
/// 当 goods.Skus 不为空(非null)时,添加/更新后,删除表中不存在 Skus 集合属性的所有记录
|
||||
/// </summary>
|
||||
/// <param name="entity">实体对象</param>
|
||||
/// <param name="propertyName">属性名</param>
|
||||
void SaveMany(TEntity entity, string propertyName);
|
||||
|
||||
IUpdate<TEntity> UpdateDiy { get; }
|
||||
|
||||
int Delete(TEntity entity);
|
||||
int Delete(IEnumerable<TEntity> entitys);
|
||||
|
||||
#if net40
|
||||
#else
|
||||
Task<TEntity> InsertAsync(TEntity entity);
|
||||
Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entitys);
|
||||
|
||||
Task<int> UpdateAsync(TEntity entity);
|
||||
Task<int> UpdateAsync(IEnumerable<TEntity> entitys);
|
||||
Task<TEntity> InsertOrUpdateAsync(TEntity entity);
|
||||
Task SaveManyAsync(TEntity entity, string propertyName);
|
||||
|
||||
Task<int> DeleteAsync(TEntity entity);
|
||||
Task<int> DeleteAsync(IEnumerable<TEntity> entitys);
|
||||
#endif
|
||||
}
|
||||
|
||||
public interface IBasicRepository<TEntity, TKey> : IBasicRepository<TEntity>, IReadOnlyRepository<TEntity, TKey>
|
||||
where TEntity : class
|
||||
{
|
||||
int Delete(TKey id);
|
||||
|
||||
#if net40
|
||||
#else
|
||||
Task<int> DeleteAsync(TKey id);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
@ -1,31 +0,0 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FreeSql
|
||||
{
|
||||
public interface IReadOnlyRepository<TEntity> : IBaseRepository
|
||||
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);
|
||||
TEntity Find(TKey id);
|
||||
|
||||
#if net40
|
||||
#else
|
||||
Task<TEntity> GetAsync(TKey id);
|
||||
Task<TEntity> FindAsync(TKey id);
|
||||
#endif
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user