Skip to content

Commit

Permalink
refine
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzhongke committed Nov 1, 2021
1 parent dcb92f6 commit 7dc0f5c
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ public class ExpressionHelper
}
Type propType = prop.PropertyType;
//访问待比较的属性
var memAccessProp = MakeMemberAccess(
var leftExpr = MakeMemberAccess(
propAccessor.Body,//要取出来Body部分,不能带参数
prop
);
var constValue = Convert(Constant(otherValue), propType);
Expression rightExpr = Convert(Constant(otherValue), propType);
if (propType.IsPrimitive)//基本数据类型和复杂类型比较方法不一样
{
equalExpr = Equal(memAccessProp, constValue);
equalExpr = Equal(leftExpr, rightExpr);
}
else
{
equalExpr = MakeBinary(ExpressionType.Equal,
memAccessProp, constValue, false,
leftExpr, rightExpr, false,
prop.PropertyType.GetMethod("op_Equality")
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ namespace Users.WebAPI
{
public class UnitOfWorkFilter: IAsyncActionFilter
{
//private IServiceProvider serviceProvider;
/*
public UnitOfWorkFilter(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
}*/
private static UnitOfWorkAttribute? GetUoWAttr(ActionDescriptor actionDesc)
{
var caDesc = actionDesc as ControllerActionDescriptor;
Expand Down Expand Up @@ -50,6 +44,8 @@ public UnitOfWorkFilter(IServiceProvider serviceProvider)
List<DbContext> dbCtxs = new List<DbContext>();
foreach (var dbCtxType in uowAttr.DbContextTypes)
{
//用HttpContext的RequestServices
//确保获取的是和请求相关的Scope实例
var sp = context.HttpContext.RequestServices;
DbContext dbCtx = (DbContext)sp.GetRequiredService(dbCtxType);
dbCtxs.Add(dbCtx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,61 +10,57 @@
using System.Transactions;

namespace Zack.ASPNETCore;
public class UnitOfWorkFilter : IAsyncActionFilter
public class UnitOfWorkFilter: IAsyncActionFilter
{
private IServiceProvider serviceProvider;

public UnitOfWorkFilter(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
}

private static UnitOfWorkAttribute? GetUoWAttr(ActionDescriptor actionDesc)
{
var caDesc = actionDesc as ControllerActionDescriptor;
if (caDesc == null)
{
return null;
}
//try to get UnitOfWorkAttribute from controller,
//if there is no UnitOfWorkAttribute on controller,
//try to get UnitOfWorkAttribute from action
var uowAttr = caDesc.ControllerTypeInfo
.GetCustomAttribute<UnitOfWorkAttribute>();
if (uowAttr != null)
{
return uowAttr;
}
else
{
return caDesc.MethodInfo
.GetCustomAttribute<UnitOfWorkAttribute>();
}
}
public async Task OnActionExecutionAsync(ActionExecutingContext context,
ActionExecutionDelegate next)
{
var uowAttr = GetUoWAttr(context.ActionDescriptor);
if (uowAttr == null)
{
await next();
return;
}
using TransactionScope txScope = new(TransactionScopeAsyncFlowOption.Enabled);
List<DbContext> dbCtxs = new List<DbContext>();
foreach (var dbCtxType in uowAttr.DbContextTypes)
{
DbContext dbCtx = (DbContext)serviceProvider.GetRequiredService(dbCtxType);
dbCtxs.Add(dbCtx);
}
var result = await next();
if (result.Exception == null)
{
foreach (var dbCtx in dbCtxs)
{
await dbCtx.SaveChangesAsync();
}
txScope.Complete();
}
}
}
private static UnitOfWorkAttribute? GetUoWAttr(ActionDescriptor actionDesc)
{
var caDesc = actionDesc as ControllerActionDescriptor;
if(caDesc==null)
{
return null;
}
//try to get UnitOfWorkAttribute from controller,
//if there is no UnitOfWorkAttribute on controller,
//try to get UnitOfWorkAttribute from action
var uowAttr = caDesc.ControllerTypeInfo
.GetCustomAttribute<UnitOfWorkAttribute>();
if(uowAttr != null)
{
return uowAttr;
}
else
{
return caDesc.MethodInfo
.GetCustomAttribute<UnitOfWorkAttribute>();
}
}
public async Task OnActionExecutionAsync(ActionExecutingContext context,
ActionExecutionDelegate next)
{
var uowAttr = GetUoWAttr(context.ActionDescriptor);
if (uowAttr == null)
{
await next();
return;
}
using TransactionScope txScope = new(TransactionScopeAsyncFlowOption.Enabled);
List<DbContext> dbCtxs = new List<DbContext>();
foreach (var dbCtxType in uowAttr.DbContextTypes)
{
//用HttpContext的RequestServices
//确保获取的是和请求相关的Scope实例
var sp = context.HttpContext.RequestServices;
DbContext dbCtx = (DbContext)sp.GetRequiredService(dbCtxType);
dbCtxs.Add(dbCtx);
}
var result = await next();
if (result.Exception == null)
{
foreach (var dbCtx in dbCtxs)
{
await dbCtx.SaveChangesAsync();
}
txScope.Complete();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,48 @@ public class ExpressionHelper
where TItem:class
where TProp:class
{
var e1 = propAccessor.Parameters.Single();//提取出来参数
BinaryExpression? conditionalExpr = null;
foreach (var prop in typeof(TProp).GetProperties())
{
BinaryExpression equalExpr;
//other的prop属性的值
object? otherValue = null;
if(other!=null)
var e1 = propAccessor.Parameters.Single();//提取出来参数
BinaryExpression? conditionalExpr = null;
foreach (var prop in typeof(TProp).GetProperties())
{
otherValue=prop.GetValue(other);
}
Type propType = prop.PropertyType;
//访问待比较的属性
var memAccessProp = MakeMemberAccess(
propAccessor.Body,//要取出来Body部分,不能带参数
prop
);
var constValue = Convert(Constant(otherValue), propType);
if (propType.IsPrimitive)//基本数据类型和复杂类型比较方法不一样
{
equalExpr = Equal(memAccessProp, constValue);
}
else
{
equalExpr = MakeBinary(ExpressionType.Equal,
memAccessProp, constValue, false,
prop.PropertyType.GetMethod("op_Equality")
BinaryExpression equalExpr;
//other的prop属性的值
object? otherValue = null;
if (other != null)
{
otherValue = prop.GetValue(other);
}
Type propType = prop.PropertyType;
//访问待比较的属性
var leftExpr = MakeMemberAccess(
propAccessor.Body,//要取出来Body部分,不能带参数
prop
);
Expression rightExpr = Convert(Constant(otherValue), propType);
if (propType.IsPrimitive)//基本数据类型和复杂类型比较方法不一样
{
equalExpr = Equal(leftExpr, rightExpr);
}
else
{
equalExpr = MakeBinary(ExpressionType.Equal,
leftExpr, rightExpr, false,
prop.PropertyType.GetMethod("op_Equality")
);
}
if (conditionalExpr == null)
{
conditionalExpr = equalExpr;
}
else
{
conditionalExpr = AndAlso(conditionalExpr, equalExpr);
}
}
if (conditionalExpr == null)
{
conditionalExpr = equalExpr;
}
else
{
conditionalExpr = AndAlso(conditionalExpr, equalExpr);
throw new ArgumentException("There should be at least one property.");
}
}
if (conditionalExpr == null)
{
throw new ArgumentException("There should be at least one property.");
}
return Lambda<Func<TItem, bool>>(conditionalExpr, e1);
return Lambda<Func<TItem, bool>>(conditionalExpr, e1);
}
}

0 comments on commit 7dc0f5c

Please sign in to comment.