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

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

问题描述

这是我的方法,我试图验证 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 ,它依赖于 CNeptune CNeptune 本身依赖于 .exe 文件用于后编译绑定,并且我目前使用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.

我尝试了此方法(即 ActionFilter ServiceFilter ),但只有在 [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. 安装软件包。

安装包装饰

安装包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();

注意修饰方法必须是可重写的(标记为虚拟 通过界面实现)。动态代理需要此方法来覆盖方法的实现。

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.

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

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