AOP’s faceted programming uses the Interceptor’s built-in container for property injection

  1. 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 { ///

/// custom interceptor ///


methods i n v o c a t i o n . M e t h o d . N a m e Before execution… ) ; b a s e . P e r f o r m P r o c e e d ( i n v o c a t i o n ) ; C o n s o l e . W r i t e L i n e ( “Method {Invocation.Method.Name} before execution…” ); base.PerformProceed(invocation); Console.WriteLine(

protected override void PreProceed(IInvocation invocation) { base.PreProceed(invocation); }

protected override void PostProceed(IInvocation invocation) { base.PostProceed(invocation); }}

///

/// aop programming interceptor extension method ///

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 ///

// the attribute tag is used to mark the attribute ///

///

/// this is an example of a method

# Region interceptor feature 111 /

/ marks method interceptions before /

/

/ flag method intercepting /

/

/ after the flag method intercepts /

Public Abstract Class AbstractAttribute: Attribute { public abstract Action Do(IInvocation invocation, Action action); }

///

// mark method before intercepting ///

///

/// mark method interception ///


L o g M o n i t o r A t t r i b u t e perform In the . i n v o c a t i o n . M e t h o d . N a m e ) ; / / Add operations that need to be handled a c t i o n . I n v o k e ( ) ; / / Add operations that need to be handled C o n s o l e . W r i t e L i n e ( “LogMonitorAttribute Execution — middle,{Invocation. Method.name}”); // Add action action.invoke () to be handled; // Add the operation to be handled console. WriteLine(

///

/// mark method after intercepting ///

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

/ Custom interceptor – Can intercept all interface methods, all the same, no more need for special customization, such as: method before interception, method after interception, method within interception, no way to do /


methods i n v o c a t i o n . M e t h o d . N a m e Before execution… ) ; / / b a s e . P e r f o r m P r o c e e d ( i n v o c a t i o n ) ; / / C o n s o l e . W r i t e L i n e ( “Method {Invocation.Method.Name} before execution…” ); // base.PerformProceed(invocation); // Console.WriteLine(

// 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

///

/// aop programming interceptor extension method ///