- 调整 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

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