Aspect Oriented Programming(AOP)是较为热门的一个话题。AOP,国内我们都习惯称之为:面向切面编程
下面直接code 干货展示:(一般人我还不告诉,嘻嘻)
1:导入相关的包:AutoFac 默认最新稳定版即可
Autofac.Extensions.DependencyInjection Version=\”7.1.0\”
Autofac.Extras.DynamicProxy Version=\”6.0.0\”
2:AutoFac注入的扩展方法
1 using System;2 using System.Linq;3 namespace ZRF.CRM.Commoms4 {5 using Autofac;6 using Autofac.Extras.DynamicProxy;7 using Microsoft.AspNetCore.Mvc;8 using System.Reflection;9 using ZRF.CRM.Commoms.Interceptors;10 using ZRF.CRM.MyAttrobutrs;1112 public static class ContainerBuilderExtenTion13 {14 public static void DependInjecterIoc(this ContainerBuilder builder)15 {16 builder.RegisterType<TransactionScopeAsyncInterCeptor>();17 builder.RegisterAssemblyTypes(Assembly.Load(\"ZRF.CRM.Service\"))18 .AsImplementedInterfaces().PropertiesAutowired().InterceptedBy(typeof(TransactionScopeAsyncInterCeptor)).EnableInterfaceInterceptors().OnRegistered(t =>19 {20 Console.WriteLine(\"ZRF.CRM.Service===Register_ok\");21 });22 }23 }24 }
3:新增ConfigureContainer(ContainerBuilder builder)方法,并在Startup方法中注册
1 using System;2 using System.Collections.Generic;3 using System.Linq;4 using System.Reflection;5 using System.Threading.Tasks;7 using Autofac;8 using Microsoft.AspNetCore.Builder;9 using Microsoft.AspNetCore.Hosting;10 using Microsoft.AspNetCore.Mvc;11 using Microsoft.AspNetCore.Mvc.Controllers;12 using Microsoft.Extensions.Configuration;13 using Microsoft.Extensions.DependencyInjection;14 using Microsoft.Extensions.DependencyInjection.Extensions;15 using Microsoft.Extensions.Hosting;16 using WebApplication1.IOCS;17 using ZRF.CRM.Commoms;18 namespace WebApplication119 {20 public class Startup21 {22 public Startup(IConfiguration configuration)23 {24 Configuration = configuration;25 }26 public void ConfigureContainer(ContainerBuilder builder)27 {2856cbuilder.DependInjecterIoc();2930 //控制器属性注入31 builder.RegisterModule<ControllerModule>();32 }33 public IConfiguration Configuration { get; }3435 // This method gets called by the runtime. Use this method to add services to the container.36 public void ConfigureServices(IServiceCollection services)37 {38 services.AddControllersWithViews();3940 //core 3.0级以上41 services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());42 }4344 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.45 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)46 {47 if (env.IsDevelopment())48 {49 app.UseDeveloperExceptionPage();50 }51 else52 {53 app.UseExceptionHandler(\"/Home/Error\");54 }55 app.UseStaticFiles();5657 app.UseRouting();5859 app.UseAuthorization();6061 app.UseEndpoints(endpoints =>62 {63 endpoints.MapControllerRoute(64 name: \"default\",65 pattern: \"{controller=Home}/{action=Index}/{id?}\");66 });67 }68 }69 }
4:Controller
1 using Microsoft.AspNetCore.Mvc;2 using Microsoft.Extensions.Logging;3 using System;4 using System.Collections.Generic;5 using System.Diagnostics;6 using System.Linq;7 using System.Threading.Tasks;8 using WebApplication1.Models;9 using ZRF.CRM.MyAttrobutrs;10 using ZRF.CRM.InterFaces;11 namespace WebApplication1.Controllers12 {13 [ControllerCanInjectPropertity]14 public class HomeController : Controller15 {16 private readonly ILogger<HomeController> _logger;1718 IDoLog _dolog;19 ITest002Service _service;20 public IDoLog _dolog2 { get; set; }2122 public HomeController(ILogger<HomeController> logger, IDoLog dolog, ITest002Service service)23ad8{24 _logger = logger;25 _dolog = dolog;26 this._service = service;27 }2829 public IActionResult Index()30 {31 var boolflag = _dolog2.LogWork(\"aaaa\");32 bool flag = _dolog.LogWork(\"qqai\");33 var getMsg = _service.Dowork(\"有aop的方法\");34 Console.WriteLine(\"Aop获取到方法:\" + getMsg);35 //_service.DoWork02(\"没有aop的普通方法\");36 return View();37 }3839 public IActionResult Privacy()40 {41 return View();42 }4344 [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]45 public IActionResult Error()46 {47 return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });48 }49 }50 }
5:InterFace
1 using System;2 using System.Collections.Generic;3 using System.Text;45 namespace ZRF.CRM.InterFaces6 {78 public interface ITest002Service9 {10 string Dowork(string msg);11 string DoWork02(string msg);12 }13 }
6:Service
1 using System;2 using System.Collections.Generic;3 using System.Text;4 namespace ZRF.CRM.Service5 {6 using ZRF.CRM.InterFaces;7 using ZRF.CRM.MyAttrobutrs;89 public class Test002Service : ITest002Service10 {11 [TransactionScopeAsync]12 puad8blic string Dowork(string msg)13 {1415 return $\"Test002Service:Dowork {DateTime.Now}-{msg}\";16 }1718 public string DoWork02(string msg)19 {20 return $\"Test002Service:DoWork02:{DateTime.Now}-{msg}\";21 }22 }23 }
7:自定义特性,将来判断哪些方法可以aop
1 using System;2 using System.Collections.Generic;3 using System.Text;45 namespace ZRF.CRM.MyAttrobutrs6 {7 [AttributeUsage(AttributeTargets.Method)]8 public class TransactionScopeAsyncAttribute : Attribute9 {10 }1112 }
8:自定义类来实现Aop的IInterceptor方法
1 using System;2 using System.Collections.Generic;3 using System.Text;45 namespace ZRF.CRM.Commoms.Interceptors6 {7 using Autofac;8 using Castle.DynamicProxy;9 using System.Reflection;10 using System.Transactions;11 using ZRF.CRM.MyAttrobutrs;1213 publiad8c class TransactionScopeAsyncInterCeptor : IInterceptor14 {15 public void Intercept(IInvocation invocation)16 {1718 if (HaveAsyncTrascAttribute(invocation))19 {20 TransactionScope scope = null;21 try22 {23 scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);24 Console.WriteLine(\"TransactionScopeAsyncFlowOption之前=======\");25 invocation.Proceed();26 scope.Complete();27 Console.WriteLine(\"invocation.Proceed()用户自己的逻辑处理ok=======\");28 Console.WriteLine(\"TransactionScopeAsyncFlowOption56c之后=======\");29 }30 catch (Exception)31 {32 Console.WriteLine(\"TransactionScopeAsyncInterCeptor:发生异常\");33 if (scope != null)34 {35 scope.Dispose();36 }37 }38 }39 else {40 Console.WriteLine(\"没有异步事务发生!\");41 invocation.Proceed();42 return;43 }44 }4546 private bool HaveAsyncTrascAttribute(IInvocation invocation)47 {48 var methodInfo = invocation.MethodInvocationTarget ?? invocation.Method;49 if (methodInfo.GetCustomAttribute<TransactionScopeAsyncAttribute>() != null)50 {51 return true;52 }53 else103d{ return false; }54 }55 }56 }
9:来一张测试的截图
10:最后欢迎留言指教