FreeSql/FreeSql.Repository/IFreeSqlExtenssions.cs
28810 30385d2e91 - FreeSql.Repository 增加 filter 参数,现实数据过滤 + 验证;
如:var postRepos = fsql.GetGuidRepository<Post>(a => a.TopicId == 1); postRepos CURD 方法都会以 lambad 条件作为查询或验证,Update/Insert验证错误时会抛出异常。
- ISelect 增加 First/FirstAsync;
2019-03-02 20:46:26 +08:00

45 lines
2.0 KiB
C#

using FreeSql;
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Text;
using System.Linq.Expressions;
public static class IFreeSqlExtenssions {
/// <summary>
/// 返回默认仓库类
/// </summary>
/// <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, Expression<Func<TEntity, bool>> filter = null) where TEntity : class {
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,
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, Expression<Func<TEntity, bool>> filter = null) where TEntity : class {
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,
ConcurrentDictionary<string, IRepository>> dicGetGuidRepository = new ConcurrentDictionary<Type, ConcurrentDictionary<string, IRepository>>();
}