我将PostSharp 6.2.8用于面向方面的编程(AOP)。我的解决方案是.Net Core 2.2解决方案。
我创建了一个方面。但是在PostSharp Explorer中,我看到以下消息:
我在我使用方面或引用的每个项目中安装了PostSharp NuGet软件包(6.2.8)。
我将PostSharp 6.2.8扩展安装到Visual Studio。我成功构建了解决方案。
此外,涉及方面的测试无法运行,并且错过了调试断点。
方面代码:
[Serializable]
public class FluentValidationAspect : OnMethodBoundaryAspect
{
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(t => t.GetType() == entityType);
foreach (var entity in entities)
{
ValidatorTool.FluentValidate(validator, entity);
}
}
}
....
public WordValidator()
{
RuleFor(w => w.WordText).NotEmpty();
RuleFor(w => w.WordText).Length(1, 250);
}
致电:
public class WordManager : IWordService
{
...
[FluentValidationAspect(typeof(WordValidator))]
public Word Add(Word word)
{
//ValidatorTool.FluentValidate(new WordValidator(), word);
return _wordDal.Add(word);
}
...
}
方面测试:
[ExpectedException(typeof(ValidationException))]
[TestMethod]
public void Word_validation_check()
{
Mock<IWordDal> mock = new Mock<IWordDal>();
WordManager wordManager = new WordManager(mock.Object);
Word word = new Word() { WordText = "" };
wordManager.Add(word);
}