- 调整 Repository 接口定义,合并为一个 IBaseRepository;

This commit is contained in:
28810 2020-03-25 13:36:13 +08:00
parent 5e9975891e
commit 58aa99a6e6
15 changed files with 134 additions and 216 deletions

View File

@ -39,8 +39,7 @@ namespace restful.Controllers
DefaultRepository<xxxx, int> repos21,
BaseRepository<Song> repos3, BaseRepository<Song, int> repos4,
IBasicRepository<Song> repos31, IBasicRepository<Song, int> repos41,
IReadOnlyRepository<Song> repos311, IReadOnlyRepository<Song, int> repos411,
IBaseRepository<Song> repos31, IBaseRepository<Song, int> repos41,
SongRepository reposSong
)

View File

@ -105,7 +105,7 @@ namespace FreeSql
repo.Update(buf);
}
T UpdateIsDelete<T>(bool value, Func<BaseRepository<TEntity>, List<TEntity>, T> func)
T UpdateIsDelete<T>(bool value, Func<IBaseRepository<TEntity>, List<TEntity>, T> func)
{
var childs = GetAllChilds();
childs.Add(this as TEntity);

View File

@ -110,13 +110,6 @@
清空状态数据
</summary>
</member>
<member name="M:FreeSql.DbSet`1.RemoveAsync(System.Linq.Expressions.Expression{System.Func{`0,System.Boolean}})">
<summary>
根据 lambda 条件删除数据
</summary>
<param name="predicate"></param>
<returns></returns>
</member>
<member name="M:FreeSql.DbSet`1.Add(`0)">
<summary>
添加
@ -218,29 +211,35 @@
<param name="entityType"></param>
<returns></returns>
</member>
<member name="M:FreeSql.IBaseRepository.AsTable(System.Func{System.String,System.String})">
<summary>
分表规则,参数:旧表名;返回:新表名 https://github.com/2881099/FreeSql/wiki/Repository
</summary>
<param name="rule"></param>
</member>
<member name="P:FreeSql.IBaseRepository.DbContextOptions">
<summary>
设置 DbContext 选项
</summary>
</member>
<member name="M:FreeSql.IBasicRepository`1.FlushState">
<member name="M:FreeSql.IBaseRepository`1.FlushState">
<summary>
清空状态数据
</summary>
</member>
<member name="M:FreeSql.IBasicRepository`1.Attach(`0)">
<member name="M:FreeSql.IBaseRepository`1.Attach(`0)">
<summary>
附加实体,可用于不查询就更新或删除
</summary>
<param name="entity"></param>
</member>
<member name="M:FreeSql.IBasicRepository`1.AttachOnlyPrimary(`0)">
<member name="M:FreeSql.IBaseRepository`1.AttachOnlyPrimary(`0)">
<summary>
附加实体并且只附加主键值可用于不更新属性值为null或默认值的字段
</summary>
<param name="data"></param>
</member>
<member name="M:FreeSql.IBasicRepository`1.SaveMany(`0,System.String)">
<member name="M:FreeSql.IBaseRepository`1.SaveMany(`0,System.String)">
<summary>
保存实体的指定 ManyToMany/OneToMany 导航属性(完整对比)<para></para>
场景:在关闭级联保存功能之后,手工使用本方法<para></para>

View File

@ -17,7 +17,6 @@ namespace FreeSql
_repo = repo;
}
static ConcurrentDictionary<Type, FieldInfo> _dicGetRepositoryDbField = new ConcurrentDictionary<Type, FieldInfo>();
static FieldInfo GetRepositoryDbField(Type type) => _dicGetRepositoryDbField.GetOrAdd(type, tp => typeof(BaseRepository<,>).MakeGenericType(tp, typeof(int)).GetField("_dbPriv", BindingFlags.Instance | BindingFlags.NonPublic));
public override IDbSet Set(Type entityType)

View File

@ -22,7 +22,7 @@ namespace FreeSql
var filters = (_repo.DataFilter as DataFilter<TEntity>)._filters.Where(a => a.Value.IsEnabled == true);
foreach (var filter in filters) select.Where(filter.Value.Expression);
return select.AsTable(_repo.AsTableSelectInternal);
return select.AsTable(_repo.AsTableSelectValueInternal);
}
internal ISelect<TEntity> OrmSelectInternal(object dywhere) => OrmSelect(dywhere);
protected override IUpdate<TEntity> OrmUpdate(IEnumerable<TEntity> entitys)
@ -37,7 +37,7 @@ namespace FreeSql
throw new Exception($"FreeSql.Repository Update 失败,因为设置了过滤器 {filter.Key}: {filter.Value.Expression},更新的数据不符合 {_db.Orm.GetEntityString(_entityType, entity)}");
update.Where(filter.Value.Expression);
}
return update.AsTable(_repo.AsTableInternal);
return update.AsTable(_repo.AsTableValueInternal);
}
internal IUpdate<TEntity> OrmUpdateInternal(IEnumerable<TEntity> entitys) => OrmUpdate(entitys);
protected override IDelete<TEntity> OrmDelete(object dywhere)
@ -45,7 +45,7 @@ namespace FreeSql
var delete = base.OrmDelete(dywhere);
var filters = (_repo.DataFilter as DataFilter<TEntity>)._filters.Where(a => a.Value.IsEnabled == true);
foreach (var filter in filters) delete.Where(filter.Value.Expression);
return delete.AsTable(_repo.AsTableInternal);
return delete.AsTable(_repo.AsTableValueInternal);
}
internal IDelete<TEntity> OrmDeleteInternal(object dywhere) => OrmDelete(dywhere);
protected override IInsert<TEntity> OrmInsert(TEntity entity) => OrmInsert(new[] { entity });
@ -60,7 +60,7 @@ namespace FreeSql
if (filter.Value.ExpressionDelegate?.Invoke(entity) == false)
throw new Exception($"FreeSql.Repository Insert 失败,因为设置了过滤器 {filter.Key}: {filter.Value.Expression},插入的数据不符合 {_db.Orm.GetEntityString(_entityType, entity)}");
}
return insert.AsTable(_repo.AsTableInternal);
return insert.AsTable(_repo.AsTableValueInternal);
}
internal IInsert<TEntity> OrmInsertInternal(TEntity entity) => OrmInsert(entity);
internal IInsert<TEntity> OrmInsertInternal(IEnumerable<TEntity> entitys) => OrmInsert(entitys);

View File

@ -13,7 +13,7 @@ namespace FreeSql
/// <typeparam name="TKey"></typeparam>
/// <param name="filter">数据过滤 + 验证</param>
/// <returns></returns>
DefaultRepository<TEntity, TKey> GetRepository<TEntity, TKey>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class;
IBaseRepository<TEntity, TKey> GetRepository<TEntity, TKey>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class;
/// <summary>
/// 在工作单元内创建联合主键的仓储类,工作单元下的仓储操作具有事务特点
@ -21,7 +21,7 @@ namespace FreeSql
/// <typeparam name="TEntity"></typeparam>
/// <param name="filter">数据过滤 + 验证</param>
/// <returns></returns>
BaseRepository<TEntity> GetRepository<TEntity>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class;
IBaseRepository<TEntity> GetRepository<TEntity>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class;
/// <summary>
/// 在工作单元内创建仓库类,工作单元下的仓储操作具有事务特点
@ -30,7 +30,7 @@ namespace FreeSql
/// <param name="filter">数据过滤 + 验证</param>
/// <param name="asTable">分表规则,参数:旧表名;返回:新表名 https://github.com/2881099/FreeSql/wiki/Repository</param>
/// <returns></returns>
GuidRepository<TEntity> GetGuidRepository<TEntity>(Expression<Func<TEntity, bool>> filter = null, Func<string, string> asTable = null) where TEntity : class;
IBaseRepository<TEntity, Guid> GetGuidRepository<TEntity>(Expression<Func<TEntity, bool>> filter = null, Func<string, string> asTable = null) where TEntity : class;
}
class RepositoryUnitOfWork : UnitOfWork, IRepositoryUnitOfWork
@ -40,21 +40,21 @@ namespace FreeSql
{
}
public GuidRepository<TEntity> GetGuidRepository<TEntity>(Expression<Func<TEntity, bool>> filter = null, Func<string, string> asTable = null) where TEntity : class
public IBaseRepository<TEntity, Guid> GetGuidRepository<TEntity>(Expression<Func<TEntity, bool>> filter = null, Func<string, string> asTable = null) where TEntity : class
{
var repo = new GuidRepository<TEntity>(_fsql, filter, asTable);
repo.UnitOfWork = this;
return repo;
}
public DefaultRepository<TEntity, TKey> GetRepository<TEntity, TKey>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class
public IBaseRepository<TEntity, TKey> GetRepository<TEntity, TKey>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class
{
var repo = new DefaultRepository<TEntity, TKey>(_fsql, filter);
repo.UnitOfWork = this;
return repo;
}
public BaseRepository<TEntity> GetRepository<TEntity>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class
public IBaseRepository<TEntity> GetRepository<TEntity>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class
{
var repo = new DefaultRepository<TEntity, int>(_fsql, filter);
repo.UnitOfWork = this;

View File

@ -1,29 +1,36 @@
#if netcoreapp
using System;
using System.Reflection;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using System.Reflection;
namespace FreeSql
{
public static class FreeSqlRepositoryDependencyInjection
{
/// <summary>
/// 批量注入 Repository可以参考代码自行调整
/// </summary>
/// <param name="services"></param>
/// <param name="globalDataFilter"></param>
/// <param name="assemblies"></param>
/// <returns></returns>
public static IServiceCollection AddFreeRepository(this IServiceCollection services, Action<FluentDataFilter> globalDataFilter = null, params Assembly[] assemblies)
{
if (globalDataFilter != null)
{
DataFilterUtil._globalDataFilter = globalDataFilter;
//如果看到了这里的代码,想自己调整,但因为 _globalDataFilter 是内部属性,无法修改?
//请考虑改用 fsql.GlobalFilter.Apply
}
DataFilterUtil._globalDataFilter = globalDataFilter;
services.AddScoped(typeof(IReadOnlyRepository<>), typeof(GuidRepository<>));
services.AddScoped(typeof(IBasicRepository<>), typeof(GuidRepository<>));
services.AddScoped(typeof(IBaseRepository<>), typeof(GuidRepository<>));
services.AddScoped(typeof(BaseRepository<>), typeof(GuidRepository<>));
services.AddScoped(typeof(GuidRepository<>));
services.AddScoped(typeof(IReadOnlyRepository<,>), typeof(DefaultRepository<,>));
services.AddScoped(typeof(IBasicRepository<,>), typeof(DefaultRepository<,>));
services.AddScoped(typeof(IBaseRepository<,>), typeof(DefaultRepository<,>));
services.AddScoped(typeof(BaseRepository<,>), typeof(DefaultRepository<,>));
services.AddScoped(typeof(DefaultRepository<,>));
if (assemblies?.Any() == true)
foreach (var asse in assemblies)

View File

@ -1,7 +1,7 @@
using FreeSql;
using System;
using System.Linq.Expressions;
using System.Linq;
using System.Linq.Expressions;
public static class FreeSqlRepositoryExtensions
{
@ -14,7 +14,7 @@ public static class FreeSqlRepositoryExtensions
/// <param name="that"></param>
/// <param name="filter">数据过滤 + 验证</param>
/// <returns></returns>
public static DefaultRepository<TEntity, TKey> GetRepository<TEntity, TKey>(this IFreeSql that, Expression<Func<TEntity, bool>> filter = null) where TEntity : class
public static IBaseRepository<TEntity, TKey> GetRepository<TEntity, TKey>(this IFreeSql that, Expression<Func<TEntity, bool>> filter = null) where TEntity : class
{
return new DefaultRepository<TEntity, TKey>(that, filter);
}
@ -26,7 +26,7 @@ public static class FreeSqlRepositoryExtensions
/// <param name="that"></param>
/// <param name="filter">数据过滤 + 验证</param>
/// <returns></returns>
public static BaseRepository<TEntity> GetRepository<TEntity>(this IFreeSql that, Expression<Func<TEntity, bool>> filter = null) where TEntity : class
public static IBaseRepository<TEntity> GetRepository<TEntity>(this IFreeSql that, Expression<Func<TEntity, bool>> filter = null) where TEntity : class
{
return new DefaultRepository<TEntity, int>(that, filter);
}
@ -39,26 +39,11 @@ public static class FreeSqlRepositoryExtensions
/// <param name="filter">数据过滤 + 验证</param>
/// <param name="asTable">分表规则,参数:旧表名;返回:新表名 https://github.com/2881099/FreeSql/wiki/Repository</param>
/// <returns></returns>
public static GuidRepository<TEntity> GetGuidRepository<TEntity>(this IFreeSql that, Expression<Func<TEntity, bool>> filter = null, Func<string, string> asTable = null) where TEntity : class
public static IBaseRepository<TEntity, Guid> GetGuidRepository<TEntity>(this IFreeSql that, Expression<Func<TEntity, bool>> filter = null, Func<string, string> asTable = null) where TEntity : class
{
return new GuidRepository<TEntity>(that, filter, asTable);
}
///// <summary>
///// 合并两个仓储的设置(过滤+分表),以便查询
///// </summary>
///// <typeparam name="TEntity"></typeparam>
///// <typeparam name="T2"></typeparam>
///// <param name="that"></param>
///// <param name="repos"></param>
///// <returns></returns>
//public static ISelect<TEntity> FromRepository<TEntity, T2>(this ISelect<TEntity> that, BaseRepository<T2> repos) where TEntity : class where T2 : class
//{
// var filters = (repos.DataFilter as DataFilter<T2>)._filters.Where(a => a.Value.IsEnabled == true);
// foreach (var filter in filters) that.Where<T2>(filter.Value.Expression);
// return that.AsTable(repos.AsTableSelectInternal);
//}
/// <summary>
/// 创建基于仓储功能的工作单元,务必使用 using 包含使用
/// </summary>

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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) { }
}
}

View File

@ -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)
{
}
}
}

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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
}
}