## v0.1.13

- 修改 连接池内部 Ping Timeout 值暂定 5秒;
- 优化 初始化时若数据库超时,则放弃预热;
- FreeSql.Repository 下增加 ISelect.FromRepository 扩展方法,实现分表的多表查询;
- 增加 FreeSql.Repository Autofac 泛型注入,可利用实现全局过滤+分表分库;
- 补充 GuidRepository 插入数据时,根据 filter 参数设定进行数据验证;
This commit is contained in:
28810
2019-03-09 02:58:23 +08:00
parent 766fe901d7
commit 428220e754
21 changed files with 228 additions and 64 deletions

View File

@ -9,35 +9,54 @@ namespace FreeSql {
where TEntity : class {
protected IFreeSql _fsql;
protected Expression<Func<TEntity, bool>> _filter;
protected Func<TEntity, bool> _filterCompile;
protected Func<string, string> _asTable;
protected Func<Type, string, string> _asTableSelect => _asTable == null ? null : new Func<Type, string, string>((a, b) => a == _entityType ? _asTable(b) : null);
protected Type _entityType = typeof(TEntity);
Expression<Func<TEntity, bool>> _filterVal;
protected Expression<Func<TEntity, bool>> Filter {
get => _filterVal;
set {
_filterVal = value;
FilterCompile = value?.Compile();
}
}
internal Expression<Func<TEntity, bool>> FilterInternal => Filter;
protected Func<TEntity, bool> FilterCompile { get; private set; }
internal Func<TEntity, bool> FilterCompileInternal => FilterCompile;
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);
}
}
protected Func<Type, string, string> AsTableSelect { get; private set; }
internal Func<Type, string, string> AsTableSelectInternal => AsTableSelect;
protected Type EntityType { get; } = typeof(TEntity);
protected BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable = null) : base() {
_fsql = fsql ?? throw new NullReferenceException("fsql 参数不可为空");
_filter = filter;
_filterCompile = filter?.Compile();
_asTable = asTable;
Filter = filter;
AsTable = asTable;
}
public ISelect<TEntity> Select => _fsql.Select<TEntity>().Where(_filter).AsTable(_asTableSelect);
public ISelect<TEntity> Select => _fsql.Select<TEntity>().Where(Filter).AsTable(AsTableSelect);
public IUpdate<TEntity> UpdateDiy => _fsql.Update<TEntity>().Where(_filter).AsTable(_asTable);
public IUpdate<TEntity> UpdateDiy => _fsql.Update<TEntity>().Where(Filter).AsTable(AsTable);
public int Delete(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(_filter).Where(predicate).AsTable(_asTable).ExecuteAffrows();
public int Delete(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(Filter).Where(predicate).AsTable(AsTable).ExecuteAffrows();
public int Delete(TEntity entity) {
ValidatorEntityAndThrow(entity);
return _fsql.Delete<TEntity>(entity).Where(_filter).AsTable(_asTable).ExecuteAffrows();
return _fsql.Delete<TEntity>(entity).Where(Filter).AsTable(AsTable).ExecuteAffrows();
}
public Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(_filter).Where(predicate).AsTable(_asTable).ExecuteAffrowsAsync();
public Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate) => _fsql.Delete<TEntity>().Where(Filter).Where(predicate).AsTable(AsTable).ExecuteAffrowsAsync();
public Task<int> DeleteAsync(TEntity entity) {
ValidatorEntityAndThrow(entity);
return _fsql.Delete<TEntity>(entity).Where(_filter).AsTable(_asTable).ExecuteAffrowsAsync();
return _fsql.Delete<TEntity>(entity).Where(Filter).AsTable(AsTable).ExecuteAffrowsAsync();
}
public virtual TEntity Insert(TEntity entity) {
@ -45,7 +64,7 @@ namespace FreeSql {
switch (_fsql.Ado.DataType) {
case DataType.SqlServer:
case DataType.PostgreSQL:
return _fsql.Insert<TEntity>().AppendData(entity).AsTable(_asTable).ExecuteInserted().FirstOrDefault();
return _fsql.Insert<TEntity>().AppendData(entity).AsTable(AsTable).ExecuteInserted().FirstOrDefault();
case DataType.MySql:
case DataType.Oracle:
case DataType.Sqlite:
@ -59,7 +78,7 @@ namespace FreeSql {
switch (_fsql.Ado.DataType) {
case DataType.SqlServer:
case DataType.PostgreSQL:
return _fsql.Insert<TEntity>().AppendData(entitys).AsTable(_asTable).ExecuteInserted();
return _fsql.Insert<TEntity>().AppendData(entitys).AsTable(AsTable).ExecuteInserted();
case DataType.MySql:
case DataType.Oracle:
case DataType.Sqlite:
@ -73,7 +92,7 @@ namespace FreeSql {
switch (_fsql.Ado.DataType) {
case DataType.SqlServer:
case DataType.PostgreSQL:
return (await _fsql.Insert<TEntity>().AppendData(entity).AsTable(_asTable).ExecuteInsertedAsync()).FirstOrDefault();
return (await _fsql.Insert<TEntity>().AppendData(entity).AsTable(AsTable).ExecuteInsertedAsync()).FirstOrDefault();
case DataType.MySql:
case DataType.Oracle:
case DataType.Sqlite:
@ -87,7 +106,7 @@ namespace FreeSql {
switch (_fsql.Ado.DataType) {
case DataType.SqlServer:
case DataType.PostgreSQL:
return _fsql.Insert<TEntity>().AppendData(entitys).AsTable(_asTable).ExecuteInsertedAsync();
return _fsql.Insert<TEntity>().AppendData(entitys).AsTable(AsTable).ExecuteInsertedAsync();
case DataType.MySql:
case DataType.Oracle:
case DataType.Sqlite:
@ -98,18 +117,18 @@ namespace FreeSql {
public int Update(TEntity entity) {
ValidatorEntityAndThrow(entity);
return _fsql.Update<TEntity>().SetSource(entity).Where(_filter).AsTable(_asTable).ExecuteAffrows();
return _fsql.Update<TEntity>().SetSource(entity).Where(Filter).AsTable(AsTable).ExecuteAffrows();
}
public Task<int> UpdateAsync(TEntity entity) {
ValidatorEntityAndThrow(entity);
return _fsql.Update<TEntity>().SetSource(entity).Where(_filter).AsTable(_asTable).ExecuteAffrowsAsync();
return _fsql.Update<TEntity>().SetSource(entity).Where(Filter).AsTable(AsTable).ExecuteAffrowsAsync();
}
protected void ValidatorEntityAndThrow(TEntity entity) => ValidatorEntityAndThrow(new[] { entity });
protected virtual void ValidatorEntityAndThrow(IEnumerable<TEntity> entitys) {
foreach (var entity in entitys) {
if (_filterCompile?.Invoke(entity) == false) throw new Exception($"FreeSql.Repository Insert 失败,因为设置了 {_filter},插入的数据不符合");
if (FilterCompile?.Invoke(entity) == false) throw new Exception($"FreeSql.Repository Insert 失败,因为设置了 {Filter},插入的数据不符合");
}
}
}
@ -120,13 +139,13 @@ namespace FreeSql {
public BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable = null) : base(fsql, filter, asTable) {
}
public int Delete(TKey id) => _fsql.Delete<TEntity>(id).Where(_filter).AsTable(_asTable).ExecuteAffrows();
public int Delete(TKey id) => _fsql.Delete<TEntity>(id).Where(Filter).AsTable(AsTable).ExecuteAffrows();
public Task<int> DeleteAsync(TKey id) => _fsql.Delete<TEntity>(id).Where(_filter).AsTable(_asTable).ExecuteAffrowsAsync();
public Task<int> DeleteAsync(TKey id) => _fsql.Delete<TEntity>(id).Where(Filter).AsTable(AsTable).ExecuteAffrowsAsync();
public TEntity Find(TKey id) => _fsql.Select<TEntity>(id).Where(_filter).AsTable(_asTableSelect).ToOne();
public TEntity Find(TKey id) => _fsql.Select<TEntity>(id).Where(Filter).AsTable(AsTableSelect).ToOne();
public Task<TEntity> FindAsync(TKey id) => _fsql.Select<TEntity>(id).Where(_filter).AsTable(_asTableSelect).ToOneAsync();
public Task<TEntity> FindAsync(TKey id) => _fsql.Select<TEntity>(id).Where(Filter).AsTable(AsTableSelect).ToOneAsync();
public TEntity Get(TKey id) => Find(id);

View File

@ -9,6 +9,10 @@ namespace FreeSql {
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) {
}
}

View File

@ -0,0 +1,82 @@
using Autofac;
using FreeSql;
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq.Expressions;
using System.Reflection;
public static class FreeSqlRepositoryAutofacDependencyInjection {
public static void RegisterFreeGuidRepository<TEntity>(this ContainerBuilder builder, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable) =>
builder.RegisterRepository<TEntity>(filter, asTable, 1);
public static void RegisterFreeRepository<TEntity>(this ContainerBuilder builder, Expression<Func<TEntity, bool>> filter) =>
builder.RegisterRepository<TEntity>(filter, null, 2);
static void RegisterRepository<TEntity>(this ContainerBuilder builder, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable, int regType) {
Func<Type, LambdaExpression> reposFunc = type => {
var entityType = type.GenericTypeArguments[0];
var filterParameter1 = Expression.Parameter(entityType, filter.Parameters[0].Name);
var convertFilter = Expression.Lambda(
typeof(Func<,>).MakeGenericType(entityType, typeof(bool)),
new ReplaceVisitor<TEntity>().Modify(filter.Body, filterParameter1),
filterParameter1
);
var repos = Expression.Parameter(type);
var blocks = new List<Expression>();
if (filter != null) blocks.Add(Expression.Call(repos, type.GetMethod("set_Filter", BindingFlags.Instance | BindingFlags.NonPublic), Expression.Constant(convertFilter)));
if (asTable != null) blocks.Add(Expression.Call(repos, type.GetMethod("set_AsTable", BindingFlags.Instance | BindingFlags.NonPublic), Expression.Constant(asTable)));
return Expression.Lambda(
//Expression.New(
// typeof(GuidRepository<>).MakeGenericType(type.GenericTypeArguments).GetConstructors()[1],
// Expression.Constant(a.Context.Resolve<IFreeSql>()),
// Expression.Constant(convertFilter),
// Expression.Constant(asTable)
//)
Expression.Block(blocks),
repos
);
};
if (regType == 1)
builder.RegisterGeneric(typeof(GuidRepository<>)).As(
typeof(GuidRepository<>),
typeof(BaseRepository<>), typeof(BaseRepository<,>),
typeof(IBasicRepository<>), typeof(IBasicRepository<,>),
typeof(IReadOnlyRepository<>), typeof(IReadOnlyRepository<,>)
).OnActivating(a => {
if (filter != null)
_dicAddGuidRepositoryFunc.GetOrAdd(a.Instance.GetType(), t => reposFunc(t).Compile()).DynamicInvoke(a.Instance);
}).InstancePerDependency();
if (regType == 2)
builder.RegisterGeneric(typeof(DefaultRepository<,>)).As(
typeof(DefaultRepository<,>),
typeof(BaseRepository<,>),
typeof(IBasicRepository<,>),
typeof(IReadOnlyRepository<,>)
).OnActivating(a => {
if (filter != null)
_dicAddGuidRepositoryFunc.GetOrAdd(a.Instance.GetType(), t => reposFunc(t).Compile()).DynamicInvoke(a.Instance);
}).InstancePerDependency();
}
static ConcurrentDictionary<Type, Delegate> _dicAddGuidRepositoryFunc = new ConcurrentDictionary<Type, Delegate>();
class ReplaceVisitor<TEntity1> : ExpressionVisitor {
private ParameterExpression parameter;
public Expression Modify(Expression expression, ParameterExpression parameter) {
this.parameter = parameter;
return Visit(expression);
}
protected override Expression VisitMember(MemberExpression node) {
if (node.Expression?.NodeType == ExpressionType.Parameter)
return Expression.Property(parameter, node.Member.Name);
return base.VisitMember(node);
}
}
}

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.1.12</Version>
<Version>0.1.13</Version>
<Authors>YeXiangQin</Authors>
<Description>FreeSql 通用仓库层实现,支持 MySql/SqlServer/PostgreSQL/Oracle/Sqlite读写分离、分表分库。</Description>
<PackageProjectUrl>https://github.com/2881099/FreeSql</PackageProjectUrl>
@ -10,6 +10,10 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FreeSql\FreeSql.csproj" />
</ItemGroup>

View File

@ -10,26 +10,33 @@ namespace FreeSql {
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) {
}
public override List<TEntity> Insert(IEnumerable<TEntity> entity) {
_fsql.Insert<TEntity>().AppendData(entity).AsTable(_asTable).ExecuteAffrows();
base.ValidatorEntityAndThrow(entity);
_fsql.Insert<TEntity>().AppendData(entity).AsTable(AsTable).ExecuteAffrows();
return entity.ToList();
}
async public override Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entity) {
await _fsql.Insert<TEntity>().AppendData(entity).AsTable(_asTable).ExecuteAffrowsAsync();
base.ValidatorEntityAndThrow(entity);
await _fsql.Insert<TEntity>().AppendData(entity).AsTable(AsTable).ExecuteAffrowsAsync();
return entity.ToList();
}
public override TEntity Insert(TEntity entity) {
_fsql.Insert<TEntity>().AppendData(entity).AsTable(_asTable).ExecuteAffrows();
base.ValidatorEntityAndThrow(entity);
_fsql.Insert<TEntity>().AppendData(entity).AsTable(AsTable).ExecuteAffrows();
return entity;
}
async public override Task<TEntity> InsertAsync(TEntity entity) {
await _fsql.Insert<TEntity>().AppendData(entity).AsTable(_asTable).ExecuteAffrowsAsync();
base.ValidatorEntityAndThrow(entity);
await _fsql.Insert<TEntity>().AppendData(entity).AsTable(AsTable).ExecuteAffrowsAsync();
return entity;
}
}

View File

@ -42,4 +42,16 @@ public static class IFreeSqlExtenssions {
.GetOrAdd(typeof(TEntity), key1 => new GuidRepository<TEntity>(that, null, null)) as GuidRepository<TEntity>;
}
static ConcurrentDictionary<Type, IRepository> dicGetGuidRepository = new ConcurrentDictionary<Type, IRepository>();
/// <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 {
return that.AsTable(repos.AsTableSelectInternal).Where<T2>(repos.FilterInternal);
}
}