- 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:
28810
2019-03-02 20:46:26 +08:00
parent a2f4a8bcd8
commit 30385d2e91
10 changed files with 84 additions and 34 deletions

View File

@ -3,6 +3,7 @@ using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Text;
using System.Linq.Expressions;
public static class IFreeSqlExtenssions {
@ -12,23 +13,33 @@ public static class IFreeSqlExtenssions {
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <param name="that"></param>
/// <param name="filter">数据过滤 + 验证</param>
/// <returns></returns>
public static DefaultRepository<TEntity, TKey> GetRepository<TEntity, TKey>(this IFreeSql that) where TEntity : class {
public static DefaultRepository<TEntity, TKey> GetRepository<TEntity, TKey>(this IFreeSql that, Expression<Func<TEntity, bool>> filter = null) where TEntity : class {
return dicGetRepository.GetOrAdd(typeof(TEntity), type1 => new ConcurrentDictionary<Type, IRepository>())
.GetOrAdd(typeof(TKey), type2 => new DefaultRepository<TEntity, TKey>(that)) as DefaultRepository<TEntity, TKey>;
return dicGetRepository
.GetOrAdd(typeof(TEntity), key1 => new ConcurrentDictionary<Type, ConcurrentDictionary<string, IRepository>>())
.GetOrAdd(typeof(TKey), key2 => new ConcurrentDictionary<string, IRepository>())
.GetOrAdd(string.Concat(filter), key3 => new DefaultRepository<TEntity, TKey>(that, filter)) as DefaultRepository<TEntity, TKey>;
}
static ConcurrentDictionary<Type, ConcurrentDictionary<Type, IRepository>> dicGetRepository = new ConcurrentDictionary<Type, ConcurrentDictionary<Type, IRepository>>();
static ConcurrentDictionary<Type,
ConcurrentDictionary<Type,
ConcurrentDictionary<string, IRepository>>
> dicGetRepository = new ConcurrentDictionary<Type, ConcurrentDictionary<Type, ConcurrentDictionary<string, IRepository>>>();
/// <summary>
/// 返回仓库类,适用 Insert 方法无须返回插入的数据
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="that"></param>
/// <param name="filter">数据过滤 + 验证</param>
/// <returns></returns>
public static GuidRepository<TEntity> GetGuidRepository<TEntity>(this IFreeSql that) where TEntity : class {
public static GuidRepository<TEntity> GetGuidRepository<TEntity>(this IFreeSql that, Expression<Func<TEntity, bool>> filter = null) where TEntity : class {
return dicGetGuidRepository.GetOrAdd(typeof(TEntity), type1 => new GuidRepository<TEntity>(that)) as GuidRepository<TEntity>;
return dicGetGuidRepository
.GetOrAdd(typeof(TEntity), key1 => new ConcurrentDictionary<string, IRepository>())
.GetOrAdd(string.Concat(filter), key2 => new GuidRepository<TEntity>(that, filter)) as GuidRepository<TEntity>;
}
static ConcurrentDictionary<Type, IRepository> dicGetGuidRepository = new ConcurrentDictionary<Type, IRepository>();
static ConcurrentDictionary<Type,
ConcurrentDictionary<string, IRepository>> dicGetGuidRepository = new ConcurrentDictionary<Type, ConcurrentDictionary<string, IRepository>>();
}