c# - OnEntry 方法不起作用,当使用 postsharp 进行 AOP 时
问题描述
嗨,我正在尝试使用 postsharp 方面进行验证。FluentValidationAspect
不适用于 OnEntry 方法。(postsharp v6.9.6)
FluentValidationAspect
[Serializable]
public class FluentValidationAspect : OnMethodBoundaryAspect
{
private Type _validatorType;
public FluentValidationAspect(Type validatorType)
{
_validatorType = validatorType;
}
public override void OnEntry(MethodExecutionArgs args)
{
var validator = (IValidator)Activator.CreateInstance(_validatorType);
var entityType = _validatorType.BaseType.GetGenericArguments()[0];
var entities = args.Arguments.Where(o => o.GetType() == entityType);
foreach (var entity in entities)
{
ValidatorTool.FluentValidate(validator, entity);
}
}
}
它不会以任何方式进入 OnEntry 方法。
产品经理
[FluentValidationAspect(typeof(ProductValidator))]
public Product Add(Product product)
{
return _productDal.Add(product);
}
测试
[TestClass]
public class ProductManagerTests
{
[ExpectedException(typeof(ValidationException))]
[TestMethod]
public void Product_Validation_Check()
{
Mock<IProductDal> mock = new Mock<IProductDal>();
ProductManager productManager = new ProductManager(mock.Object);
productManager.Add(new Product());
}
}
解决方案
每个应用 PostSharp 方面的项目都需要PostSharp
安装 NuGet 包。
推荐阅读
- android - 日历 UI 未在 Android 上使用 react-native-add-calendar-event 打开
- javascript - 时间递归函数的表现
- python - pandas shift方法创建滞后值有什么问题吗?
- c# - 派生类有导航属性时的外键约束
- kubernetes - Kubernetes 负载均衡器服务如何与 AWS/GKE 负载均衡器交互?
- javascript - JavaScript 错误的时间戳格式
- java - Java中的递归函数是如何求值的?
- asp.net - 不允许 Plesk IIS POST
- delphi - Delphi 从 TList 中删除第一个 Y 元素
- amazon-web-services - 基于 AWS SageMaker 构建的经过训练的 DeepAR 模型的本地托管