AOP’s faceted programming uses the Interceptor’s built-in container for property injection
- NuGet package management adds Castle Core, which is used for interceptor. Startup ConfigureServices adds the following
Public void ConfigureServices(IServiceCollection Services) {#region Controller Its built-in ioc is then used to replace the original creator of the controller, so that you can use the ioc to dependency injection and inversion of control services, create the corresponding controller AddControllersWithViews () AddControllersAsServices (); services.Replace(ServiceDescriptor.Transient<IControllerActivator, CustomServiceBaseControllerActivator>());
services.AddTransient<ITestA, TestA>(); services.AddTransient<ITestB, TestB>(); #endregion} 2. Custom interceptor and aop aspect programming interceptor extension method
using Castle.DynamicProxy; using System;
namespace WebAppTest.Extensions { ///
protected override void PreProceed(IInvocation invocation) { base.PreProceed(invocation); }
protected override void PostProceed(IInvocation invocation) { base.PostProceed(invocation); }}
///
using WebAppTest.CustomAttrributes;
Namespace webAppTest. Services {public Interface ITestA {// Interface service tag Interceptor needs to intercept the feature [LogBeforeAttribute] [LogAfterAttribute] void Show(); }}
Public IActionResult Index() {// _testa.show (); _testB.Show(); ((ITestA)_testA.AOP(typeof(ITestA))).Show(); return View(); } 4. Custom feature tag using castle.dynamicProxy; using System;
Namespace WebAppTest. CustomAttrributes {# region to create a custom controller object, in order to use ioc to create controllers, Controller Controller property injection Controller method injection IOC is a Dictionary ///
///
# Region interceptor feature 111 /
/
/
Public Abstract Class AbstractAttribute: Attribute { public abstract Action Do(IInvocation invocation, Action action); }
///
///
///
using Castle.DynamicProxy; using System; using System.Reflection; using WebAppTest.CustomAttrributes;
Namespace webAppTest. Extensions {#region custom interceptor – Intercepts all interface methods, all of which are the same
// protected override void PreProceed(IInvocation invocation) // { // base.PreProceed(invocation); // }
// protected override void PostProceed(IInvocation invocation) // { // base.PostProceed(invocation); // } //} #endregion
Public Class CustomInterceptor public Class CustomInterceptor StandardInterceptor { protected override void PerformProceed(IInvocation invocation) { Console.WriteLine( “Method Invocation.Method.Name Before invocation…” ); Actionaction=()=>base.PerformProceed(invocation); varcustomAtts=invocation.Method.GetCustomAttributes
(); foreach(varcustomAttincustomAtts)action=customAtt.Do(invocation,action); action.Invoke(); Console.WriteLine(” Method {Invocation.Method.Name} before execution…” ); Action action = () => base.PerformProceed(invocation); var customAtts = invocation.Method.GetCustomAttributes
(); foreach (var customAtt in customAtts) { action = customAtt.Do(invocation, action); } action.Invoke(); Console.WriteLine(” Method Invocation.Method.Name Before invocation…” ); Actionaction=()=>base.PerformProceed(invocation); varcustomAtts=invocation.Method.GetCustomAttributes
(); foreach(varcustomAttincustomAtts)action=customAtt.Do(invocation,action); action.Invoke(); Console.WriteLine(” Method {Invocation.Method.Name} after execution…” ); }
//protected override void PreProceed(IInvocation invocation) //{ // var customAtt = invocation.Method.GetCustomAttribute(); // customAtt.Do(); // base.PreProceed(invocation); / /}
//protected override void PostProceed(IInvocation invocation) //{ // var customAtt = invocation.Method.GetCustomAttribute(); // customAtt.Do(); // base.PostProceed(invocation); //} } #endregion
///