4 AOP
ansonzhang edited this page 2019-06-11 15:24:30 +08:00
This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

1、系列文章

https://www.cnblogs.com/laozhang-is-phi/category/1290896.html

2、哪些AOP

A、内存缓存AOP

B、Redis缓存AOP

C、日志AOP

这三个AOP对应的文件地址

├─Blog.Core
├──AOP
│   ├─BlogCacheAOP.cs
│   ├─BlogLogAOP.cs
│   ├─BlogRedisCacheAOP.cs


D、SqlSugar 生产Sql语句AOP在仓储层 DbContext.cs 上下文中 _db.Aop.OnLogExecuting

3、配置AOP是否启用

在 appsettings.json 文件中配置:

"RedisCaching": {
  "Enabled": false,
  "ConnectionString": "127.0.0.1:6319"
},
"MemoryCachingAOP": {
  "Enabled": true
},
"LogAOP": {
  "Enabled": true
},

4、Startup.cs 中注入服务

 builder.RegisterType<BlogCacheAOP>();//可以直接替换其他拦截器
 builder.RegisterType<BlogRedisCacheAOP>();//可以直接替换其他拦截器
 builder.RegisterType<BlogLogAOP>();//这样可以注入第二个


 // AOP 开关,如果想要打开指定的功能,只需要在 appsettigns.json 对应对应 true 就行。
 var cacheType = new List<Type>();
 if (Appsettings.app(new string[] { "AppSettings", "RedisCaching", "Enabled" }).ObjToBool())
 {
     cacheType.Add(typeof(BlogRedisCacheAOP));
 }
 if (Appsettings.app(new string[] { "AppSettings", "MemoryCachingAOP", "Enabled" }).ObjToBool())
 {
     cacheType.Add(typeof(BlogCacheAOP));
 }
 if (Appsettings.app(new string[] { "AppSettings", "LogAOP", "Enabled" }).ObjToBool())
 {
     cacheType.Add(typeof(BlogLogAOP));
 }

 builder.RegisterAssemblyTypes(assemblysServices)
           .AsImplementedInterfaces()
           .InstancePerLifetimeScope()
           .EnableInterfaceInterceptors()//引用Autofac.Extras.DynamicProxy;
                                         // 如果你想注入两个,就这么写  InterceptedBy(typeof(BlogCacheAOP), typeof(BlogLogAOP));
                                         // 如果想使用Redis缓存请必须开启 redis 服务端口号我的是6319如果不一样还是无效否则请使用memory缓存 BlogCacheAOP
           .InterceptedBy(cacheType.ToArray());//允许将拦截器服务的列表分配给注册。