mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 20:08:15 +08:00
## v0.3.11
- 增加 ISelect、IInsert、IUpdate、IDelete WithTransaction 方法,将事务对象暴露给外部; - 增加 IAdo ExecuteXxx 系列方法重载,支持事务对象的传入;
This commit is contained in:
143
FreeSql.Repository/Extenssions/AutofacExtenssions.cs
Normal file
143
FreeSql.Repository/Extenssions/AutofacExtenssions.cs
Normal file
@ -0,0 +1,143 @@
|
||||
using Autofac;
|
||||
using FreeSql;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
|
||||
public static class FreeSqlRepositoryAutofacExtenssions {
|
||||
|
||||
public static void RegisterFreeRepository(this ContainerBuilder builder) => RegisterFreeRepositoryPrivate<bool>(builder, null, null);
|
||||
public static void RegisterFreeRepositoryAndFilter<TEntity>(this ContainerBuilder builder, Func<Expression<Func<TEntity, bool>>> filterHandler) => RegisterFreeRepositoryPrivate<TEntity>(builder, filterHandler, null);
|
||||
|
||||
static ConcurrentDictionary<Type, Delegate> _dicRegisterFreeRepositorySetFilterFunc = new ConcurrentDictionary<Type, Delegate>();
|
||||
static ConcurrentDictionary<Type, Delegate> _dicRegisterFreeRepositorySetAsTableFunc = new ConcurrentDictionary<Type, Delegate>();
|
||||
static void RegisterFreeRepositoryPrivate<TEntity>(ContainerBuilder builder, Func<Expression<Func<TEntity, bool>>> filterHandler, Func<string, string> asTableHandler) {
|
||||
|
||||
Func<Type, Delegate> setFilterFunc = reposType => _dicRegisterFreeRepositorySetFilterFunc.GetOrAdd(reposType, type => {
|
||||
var reposParameter = Expression.Parameter(type);
|
||||
var fitlerParameter = Expression.Parameter(typeof(Expression<>).MakeGenericType(typeof(Func<,>).MakeGenericType(type.GenericTypeArguments[0], typeof(bool))));
|
||||
return Expression.Lambda(
|
||||
Expression.Block(
|
||||
Expression.Call(reposParameter, type.GetMethod("set_Filter", BindingFlags.Instance | BindingFlags.NonPublic), fitlerParameter)
|
||||
),
|
||||
new[] {
|
||||
reposParameter, fitlerParameter
|
||||
}
|
||||
).Compile();
|
||||
});
|
||||
Func<Type, LambdaExpression> convertFilter = type => {
|
||||
var filter = filterHandler?.Invoke();
|
||||
if (filter == null) return null;
|
||||
var entityType = type.GenericTypeArguments[0];
|
||||
var filterParameter1 = Expression.Parameter(entityType, filter.Parameters[0].Name);
|
||||
try {
|
||||
return Expression.Lambda(
|
||||
typeof(Func<,>).MakeGenericType(entityType, typeof(bool)),
|
||||
new ReplaceVisitor<TEntity>().Modify(filter.Body, filterParameter1),
|
||||
filterParameter1
|
||||
);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
builder.RegisterGeneric(typeof(GuidRepository<>)).As(
|
||||
typeof(GuidRepository<>),
|
||||
typeof(BaseRepository<>),
|
||||
typeof(IBasicRepository<>),
|
||||
typeof(IReadOnlyRepository<>)
|
||||
).OnActivating(a => {
|
||||
if (filterHandler != null) {
|
||||
var type = a.Instance.GetType();
|
||||
setFilterFunc(type)?.DynamicInvoke(a.Instance, convertFilter(type));
|
||||
}
|
||||
}).InstancePerDependency();
|
||||
|
||||
builder.RegisterGeneric(typeof(DefaultRepository<,>)).As(
|
||||
typeof(DefaultRepository<,>),
|
||||
typeof(BaseRepository<,>),
|
||||
typeof(IBasicRepository<,>),
|
||||
typeof(IReadOnlyRepository<,>)
|
||||
).OnActivating(a => {
|
||||
if (filterHandler != null) {
|
||||
var type = a.Instance.GetType();
|
||||
setFilterFunc(type)?.DynamicInvoke(a.Instance, convertFilter(type));
|
||||
}
|
||||
}).InstancePerDependency();
|
||||
}
|
||||
|
||||
//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 => {
|
||||
// try { reposFunc(t).Compile(); } catch { }
|
||||
// return null;
|
||||
// })?.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 => {
|
||||
// try { reposFunc(t).Compile(); } catch { }
|
||||
// return null;
|
||||
// })?.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);
|
||||
}
|
||||
}
|
||||
}
|
57
FreeSql.Repository/Extenssions/IFreeSqlExtenssions.cs
Normal file
57
FreeSql.Repository/Extenssions/IFreeSqlExtenssions.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using FreeSql;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Text;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
public static class FreeSqlRepositoryIFreeSqlExtenssions {
|
||||
|
||||
/// <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 {
|
||||
|
||||
if (filter != null) return new DefaultRepository<TEntity, TKey>(that, filter);
|
||||
return dicGetRepository
|
||||
.GetOrAdd(typeof(TEntity), key1 => new ConcurrentDictionary<Type, IRepository>())
|
||||
.GetOrAdd(typeof(TKey), key2 => new DefaultRepository<TEntity, TKey>(that, null)) as DefaultRepository<TEntity, TKey>;
|
||||
}
|
||||
static ConcurrentDictionary<Type,
|
||||
ConcurrentDictionary<Type,
|
||||
IRepository>
|
||||
> dicGetRepository = new ConcurrentDictionary<Type, ConcurrentDictionary<Type, IRepository>>();
|
||||
|
||||
/// <summary>
|
||||
/// 返回仓库类,适用 Insert 方法无须返回插入的数据
|
||||
/// </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 GuidRepository<TEntity> GetGuidRepository<TEntity>(this IFreeSql that, Expression<Func<TEntity, bool>> filter = null, Func<string, string> asTable = null) where TEntity : class {
|
||||
|
||||
if (filter != null || asTable != null) return new GuidRepository<TEntity>(that, filter, asTable);
|
||||
return dicGetGuidRepository
|
||||
.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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user