AOP for C# dotnet core 2.0,在方法体运行之前访问方法参数值 [英] AOP for C# dotnet core 2.0, access method parameter values before method body runs

查看:23
本文介绍了AOP for C# dotnet core 2.0,在方法体运行之前访问方法参数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的方法,我正在尝试验证 componentToSave(或访问方法参数值)并在方法主体运行之前抛出异常.

This is my method, I am trying to validate componentToSave (or access method parameter values) and throw an exception before method body even runs.

public Component SaveComponent(Component componentToSave) {
    ...
}

  1. 我尝试使用 PostSharp,但它不是免费的,还有其他库依赖 AutoFac 作为 IoC,但在我当前的设置中,我使用的是 dotnet core 的内置依赖注入.

  1. I tried using PostSharp but it is not free and also there were other libraries that rely on AutoFac as IoC but in my current setup I am using dotnet core's built-in dependency injection.

我尝试了 NConcern 并且它依赖于 CNeptuneCNeptune 本身依赖于.a> 用于编译后绑定,我目前使用 Linux 进行开发和生产,因此我无法使用它,即使我尝试在 Windows 上使用它进行测试,但无法使其与 dotnet core 一起使用.

I tried NConcern and it relies on CNeptune and CNeptune itself relies on a .exe file for post-compile binding and I currently use Linux for both development and production so I cannot use it, even I tried testing with it on windows but could not get it to work with dotnet core.

我尝试了这种方法(即ActionFilterServiceFilter) 但我只有在 [ServiceFilter(typeof(LoggingActionFilter))] 超过控制器而不是任何其他方法(即 SaveComponent 方法).

I tried this approach (i.e. ActionFilter and ServiceFilter) but I only got it working if [ServiceFilter(typeof(LoggingActionFilter))] is over controller not any other method (i.e. SaveComponent method).

我尝试使用 RealProxy 但显然 dotnet 核心不支持它.

I tried using RealProxy but apparently, it is not supported in dotnet core.

我迷路了,也许我把问题复杂化了,但应该有办法.任何帮助将不胜感激.

I am just lost, maybe I am over complicating the problem but there should be a way. Any help would be greatly appreciated.

推荐答案

在方法体运行之前验证方法参数可以通过创建动态代理来实现,该代理拦截方法调用并执行验证逻辑.

Validating method arguments before the method body even runs can be achieved by creating a dynamic proxy, which intercepts method calls and executes your validation logic.

.NET Core 支持的一种动态代理实现由 Castle.Core 包提供.

One dynamic proxy implementation that is supported in .NET Core is provided by Castle.Core package.

但是,根据我个人的经验,实现这些动态代理需要一些时间来适应,并且通常包含相当多的样板代码.

However, in my personal experience, implementing these dynamic proxies takes some getting used to and often consists of quite some boiler plate code.

为了使动态装饰方法的过程更简单,我创建了一个包装包 Decor.NET 并在 MIT 许可下发布.以下是有关如何实现您所要求的行为的说明.

To make the process of dynamically decorating your methods simpler, I created a wrapper package Decor.NET and released it under MIT licence. Following are the instructions on how to achieve the behaviour you are asking.

  1. 安装软件包.

安装包装饰

Install-Package Decor.Extensions.Microsoft.DependencyInjection

  1. 创建一个装饰器,其中包含验证逻辑.

public class ComponentValidator : IDecorator
{    
    public async Task OnInvoke(Call call)
    {
        var componentToSave = (Component)call.Arguments[0];

        if (/* Your validation logic */)
            throw new Exception("Something's not right.");

        await call.Next();
    }
}

  1. [Decorate(typeof(ComponentValidator))] 属性添加到要验证的方法.
  1. Add [Decorate(typeof(ComponentValidator))] attribute to the method(s) you want to validate.

[Decorate(typeof(ComponentValidator))]
public virtual Component SaveComponent(Component componentToSave) {
    ...
}

  1. 使用Decor.Extensions.Microsoft.DependencyInjection提供的扩展方法注册Decor.NET、验证装饰器和装饰类.
  1. Register Decor.NET, validation decorator and decorated class using the extension methods provided by Decor.Extensions.Microsoft.DependencyInjection.

services.AddDecor()
    .AddTransient<ComponentValidator>()
    .AddScoped<SomeService>().Decorated();

注意装饰方法必须是可覆盖的(标记为virtual 从接口实现).这是动态代理覆盖方法实现所必需的.

Notice that the decorated method has to be overridable (marked as virtual or implemented from interface). This is needed for dynamic proxy to override method's implementation.

您可以在这个 .Net Fiddle 中玩耍.

You can play around in this .Net Fiddle.

这篇关于AOP for C# dotnet core 2.0,在方法体运行之前访问方法参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆