Browse Source

Add filter docs.

master
Savorboard 3 years ago
parent
commit
d562d4e8bf
3 changed files with 92 additions and 0 deletions
  1. +46
    -0
      docs/content/user-guide/en/cap/filter.md
  2. +44
    -0
      docs/content/user-guide/zh/cap/filter.md
  3. +2
    -0
      docs/mkdocs.yml

+ 46
- 0
docs/content/user-guide/en/cap/filter.md View File

@@ -0,0 +1,46 @@
# Filter

Subscriber filters are similar to ASP.NET MVC filters and are mainly used to process additional work before and after the subscriber method is executed. Such as transaction management or logging, etc.

## Create subscribe filter

1. Create filter

Create a new filter class and inherit the `SubscribeFilter` abstract class.

```C#
public class MyCapFilter: SubscribeFilter
{
public override void OnSubscribeExecuting(ExecutingContext context)
{
// before subscribe method exectuing
}

public override void OnSubscribeExecuted(ExecutedContext context)
{
// after subscribe method executed
}

public override void OnSubscribeException(ExceptionContext context)
{
// subscribe method execution exception
}
}
```

In some scenarios, if you want to terminate the subscriber method execution, you can throw an exception in `OnSubscribeExecuting`, and choose to ignore the exception in `OnSubscribeException`.

To ignore exceptions, you can setting `context.ExceptionHandled = true` in `ExceptionContext`

2. Configuration

Use `AddSubscribeFilter<>` to add a filter.

```C#
services.AddCap(opt =>
{
// ***
}.AddSubscribeFilter<MyCapFilter>();
```

Currently, we do not support adding multiple filters.

+ 44
- 0
docs/content/user-guide/zh/cap/filter.md View File

@@ -0,0 +1,44 @@
# 过滤器

在 5.1.0 版本中,我们支持了在订阅者中添加过滤器。在过去,我们通过对第三方 AOP 组件提供支持来做到这一点,例如我们写了一篇[博客](https://www.cnblogs.com/savorboard/p/cap-castle.html) 来描述如何在 CAP 5.0 版本中使用 Castle 来对订阅方法进行拦截,但了这种方式存在一些缺点,例如无法方便的在代理类中进行构造函数注入以及方法需要设定为 virtual 另外还有拦截器生命周期控制等问题。

所以我们引入了对订阅者过滤器的支持,以使在某些场景(如事务处理,日志记录等)中变得容易。

## 自定义过滤器

1、创建一个过滤器类,并继承 `SubscribeFilter` 抽象类。

```C#
public class MyCapFilter: SubscribeFilter
{
public override void OnSubscribeExecuting(ExecutingContext context)
{
// 订阅方法执行前
}

public override void OnSubscribeExecuted(ExecutedContext context)
{
// 订阅方法执行后
}

public override void OnSubscribeException(ExceptionContext context)
{
// 订阅方法执行异常
}
}
```

在一些场景中,如果想终止订阅者方法执行,可以在 `OnSubscribeExecuting` 中抛出异常,并且在 `OnSubscribeException` 中选择忽略该异常。

通过在 `ExceptionContext` 中设置 `context.ExceptionHandled = true` 来忽略异常。

2、集成

```C#
services.AddCap(opt =>
{
// ***
}.AddSubscribeFilter<MyCapFilter>();
```

目前, 我们还不支持同时添加多个过滤器。

+ 2
- 0
docs/mkdocs.yml View File

@@ -89,6 +89,7 @@ nav:
- CAP:
- Configuration: user-guide/en/cap/configuration.md
- Messaging: user-guide/en/cap/messaging.md
- Filter: user-guide/en/cap/filter.md
- Serialization: user-guide/en/cap/serialization.md
- Transactions: user-guide/en/cap/transactions.md
- Idempotence: user-guide/en/cap/idempotence.md
@@ -123,6 +124,7 @@ nav:
- CAP:
- 配置: user-guide/zh/cap/configuration.md
- 消息: user-guide/zh/cap/messaging.md
- 过滤器: user-guide/zh/cap/filter.md
- 序列化: user-guide/zh/cap/serialization.md
- 事务: user-guide/zh/cap/transactions.md
- 幂等性: user-guide/zh/cap/idempotence.md


Loading…
Cancel
Save