Skip to content

Commit

Permalink
Update 1.使用指南.md (#213)
Browse files Browse the repository at this point in the history
* Update docs/1.使用指南.md
1. 主要依據原文件描述調整為 2.0.0 版本 AspectCore 實作 Aop 機制實際使用情形

* Update 1.使用指南.md

調整排版
  • Loading branch information
cdcd72 authored Apr 3, 2020
1 parent 4b10b34 commit 30ee910
Showing 1 changed file with 36 additions and 29 deletions.
65 changes: 36 additions & 29 deletions docs/1.使用指南.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# 1. 使用指南

(简单的代码示例:https://github.com/fs7744/AspectCoreDemo )
2.0.0 版本 :arrow_up: (简单的代码示例:https://github.com/cdcd72/NetCore.AspectCore.AOP.Demo)
2.0.0 版本 :arrow_down: (简单的代码示例:https://github.com/fs7744/AspectCoreDemo)

* 开始使用AspectCore
## 开始使用AspectCore

* 启动 Visual Studio。从 File 菜单, 选择 New > Project。选择 ASP.NET Core Web Application 项目模版,创建新的 ASP.NET Core Web Application 项目。

Expand Down Expand Up @@ -68,19 +69,29 @@
```
* 注册`ICustomService`,接着,在`ConfigureServices`中配置创建代理类型的容器:
``` csharp
public IServiceProvider ConfigureServices(IServiceCollection services)
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<ICustomService, CustomService>();
services.AddMvc();
services.AddDynamicProxy();
return services.BuildAspectCoreServiceProvider();
services.ConfigureDynamicProxy();
}
```
* 拦截器配置。

全局拦截器。使用`AddDynamicProxy(Action<IAspectConfiguration>)`的重载方法,其中`IAspectConfiguration`提供`Interceptors`注册全局拦截器:
* 最后于 `Program` 的 `CreateHostBuilder` 處加上 `UseServiceProviderFactory(new DynamicProxyServiceProviderFactory())`:
``` csharp
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
//
.UseServiceProviderFactory(new DynamicProxyServiceProviderFactory());
```
---
## 拦截器配置
* 全局拦截器。使用`ConfigureDynamicProxy(Action<IAspectConfiguration>)`的重载方法,其中`IAspectConfiguration`提供`Interceptors`注册全局拦截器:
``` csharp
services.AddDynamicProxy(config =>
services.ConfigureDynamicProxy(config =>
{
config.Interceptors.AddTyped<CustomInterceptorAttribute>();
});
Expand Down Expand Up @@ -115,32 +126,33 @@
```
修改全局拦截器注册:
``` csharp
services.AddDynamicProxy(config =>
services.ConfigureDynamicProxy(config =>
{
config.Interceptors.AddTyped<CustomInterceptorAttribute>(args: new object[] { "custom" });
});
```
作为服务的全局拦截器。在`ConfigureServices`中添加:
``` csharp
services.AddTransient<CustomInterceptorAttribute>(provider => new CustomInterceptorAttribute("service"));
services.AddTransient<CustomInterceptorAttribute>(provider => new CustomInterceptorAttribute("custom"));
```
修改全局拦截器注册:
``` csharp
services.AddDynamicProxy(config =>
services.ConfigureDynamicProxy(config =>
{
// 加入已注册服务
config.Interceptors.AddServiced<CustomInterceptorAttribute>();
});
```
作用于特定`Service`或`Method`的全局拦截器,下面的代码演示了作用于带有`Service`后缀的类的全局拦截器:
``` csharp
services.AddDynamicProxy(config =>
services.ConfigureDynamicProxy(config =>
{
config.Interceptors.AddTyped<CustomInterceptorAttribute>(method => method.DeclaringType.Name.EndsWith("Service"));
config.Interceptors.AddTyped<CustomInterceptorAttribute>(method => method.Name.EndsWith("MethodName"));
});
```
使用通配符的特定全局拦截器:
``` csharp
services.AddDynamicProxy(config =>
services.ConfigureDynamicProxy(config =>
{
config.Interceptors.AddTyped<CustomInterceptorAttribute>(Predicates.ForService("*Service"));
});
Expand All @@ -155,7 +167,7 @@
```
同时支持全局忽略配置,亦支持通配符:
``` csharp
services.AddDynamicProxy(config =>
services.ConfigureDynamicProxy(config =>
{
//App1命名空间下的Service不会被代理
config.NonAspectPredicates.AddNamespace("App1");
Expand All @@ -178,13 +190,13 @@
```
* 拦截器中的依赖注入。在拦截器中支持属性注入,构造器注入和服务定位器模式。

属性注入,在拦截器中拥有`public get and set`权限的属性标记`[AspectCore.Injector.FromContainerAttribute]`特性,即可自动注入该属性,如:
属性注入,在拦截器中拥有`public get and set`权限的属性标记`[AspectCore.DependencyInjection.FromServiceContextAttribute]`特性,即可自动注入该属性,如:
``` csharp
public class CustomInterceptorAttribute : AbstractInterceptorAttribute
{
//ps : 只有使用 config.Interceptors.AddTyped<CustomInterceptor>(); 时,属性注入才生效,
// 不能使用以下这种方式 services.AddSingleton<CustomInterceptor>(); + [ServiceInterceptor(typeof(CustomInterceptor))]
[FromContainer]
//ps : 只有使用 config.Interceptors.AddTyped<CustomInterceptorAttribute>(); 时,属性注入才生效,
// 不能使用以下这种方式 services.AddSingleton<CustomInterceptorAttribute>(); + services.ConfigureDynamicProxy(config => { config.Interceptors.AddServiced<CustomInterceptorAttribute>(); });
[FromServiceContext]
public ILogger<CustomInterceptorAttribute> Logger { get; set; }


Expand All @@ -202,19 +214,13 @@
{
private readonly ILogger<CustomInterceptor> ctorlogger;

// ps : 当全局配置 config.Interceptors.AddTyped<CustomInterceptor>(); 时,构造器注入无法自动注入,需要手动处理
// 只有使用 services.AddSingleton<CustomInterceptor>(); + [ServiceInterceptor(typeof(CustomInterceptor))] 才会自动注入
// ps : 当全局配置 config.Interceptors.AddTyped<CustomInterceptorAttribute>(); 时,构造器注入无法自动注入,需要手动处理
// 只有使用 services.AddSingleton<CustomInterceptorAttribute>(); + services.ConfigureDynamicProxy(config => { config.Interceptors.AddServiced<CustomInterceptorAttribute>(); }); 才会自动注入
public CustomInterceptor(ILogger<CustomInterceptor> ctorlogger)
{
this.ctorlogger = ctorlogger;
}
}

public interface ICustomService
{
[ServiceInterceptor(typeof(CustomInterceptorAttribute))]
void Call();
}
```

服务定位器模式。拦截器上下文`AspectContext`可以获取当前Scoped的`ServiceProvider`:
Expand All @@ -230,4 +236,5 @@
}
```

ps : 原文(http://www.cnblogs.com/liuhaoyang/p/aspectcore-introduction-1.html)
2.0.0 版本 :arrow_up: ps : 原文(https://www.thinkinmd.com/post/2020/03/20/use-aspectcore-to-implement-aop-mechanism/)
2.0.0 版本 :arrow_down: ps : 原文(http://www.cnblogs.com/liuhaoyang/p/aspectcore-introduction-1.html)

0 comments on commit 30ee910

Please sign in to comment.