mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-08-02 17:57:31 +08:00
initial commit
This commit is contained in:
@ -0,0 +1,43 @@
|
||||
#if netcoreapp
|
||||
using FreeSql;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyInjection
|
||||
{
|
||||
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
|
||||
}
|
||||
|
||||
services.AddScoped(typeof(IBaseRepository<>), typeof(GuidRepository<>));
|
||||
services.AddScoped(typeof(BaseRepository<>), typeof(GuidRepository<>));
|
||||
|
||||
services.AddScoped(typeof(IBaseRepository<,>), typeof(DefaultRepository<,>));
|
||||
services.AddScoped(typeof(BaseRepository<,>), typeof(DefaultRepository<,>));
|
||||
|
||||
if (assemblies?.Any() == true)
|
||||
foreach (var asse in assemblies)
|
||||
foreach (var repo in asse.GetTypes().Where(a => a.IsAbstract == false && typeof(IBaseRepository).IsAssignableFrom(a)))
|
||||
services.AddScoped(repo);
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
@ -0,0 +1,56 @@
|
||||
using FreeSql;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
partial class FreeSqlDbContextExtensions
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 返回默认仓库类
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <typeparam name="TKey"></typeparam>
|
||||
/// <param name="that"></param>
|
||||
/// <param name="filter">数据过滤 + 验证</param>
|
||||
/// <returns></returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回默认仓库类,适用联合主键的仓储类
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="that"></param>
|
||||
/// <param name="filter">数据过滤 + 验证</param>
|
||||
/// <returns></returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回仓库类
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="that"></param>
|
||||
/// <param name="filter">数据过滤 + 验证</param>
|
||||
/// <param name="asTable">分表规则,参数:旧表名;返回:新表名 https://github.com/2881099/FreeSql/wiki/Repository</param>
|
||||
/// <returns></returns>
|
||||
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>
|
||||
/// 创建基于仓储功能的工作单元,务必使用 using 包含使用
|
||||
/// </summary>
|
||||
/// <param name="that"></param>
|
||||
/// <returns></returns>
|
||||
public static IRepositoryUnitOfWork CreateUnitOfWork(this IFreeSql that)
|
||||
{
|
||||
return new RepositoryUnitOfWork(that);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user