C # uses Autofac to implement inversion of control IoC and aspect oriented programming AOP
Install-Package Autofac. Aop
using System; using System. Collections. Generic; using System. Linq; using System. Text; using System. Threading. Tasks; // Search Autofac on Nuget first Aop installation using Castle. DynamicProxy; namespace AutofacDEMO { /// <summary> /// Interceptor needs to implement IInterceptor interface Intercept method /// </summary> public class LogInterceptor : IInterceptor { /// <summary> /// Intercept method prints the name and parameters of the intercepted method before execution and the returned result after execution /// </summary> /// <param name="invocation"> Include information about intercepted methods </param> public void Intercept(IInvocation invocation) { Console. WriteLine( " Before method execution: the parameter of intercepting method {1} under {0} class is {2} " , invocation. InvocationTarget. GetType(), invocation. Method. Name, string .Join( " , " , invocation. Arguments. Select(a => (a ?? "" ). ToString()). ToArray())); // Continue execution after the intercepted method is completed invocation. Proceed(); Console. WriteLine( " Method execution completed, return result: {0} " , invocation. ReturnValue); Console. WriteLine(); } } }
var builder = new ContainerBuilder();
// Naming injection builder. Register(c => new LogInterceptor()). Named<IInterceptor>( " log-calls " ); // Type injection builder. Register(c => new LogInterceptor());
//Or builder. RegisterType <LogInterceptor>();
// Enable class proxy interception // Method 1: Add attribute to the type builder. RegisterType<Student> (). EnableClassInterceptors(); // Method 2: Dynamically inject interceptors when registering types to containers (remove attribute attributes on types) builder. RegisterType<Teacher>(). InterceptedBy( typeof (LogInterceptor)). EnableClassInterceptors(); // Enable interface proxy interception // Method 1: Add attribute to the type builder. RegisterType<Man>(). As<IPerson> (). EnableInterfaceInterceptors(); // Method 2: Dynamically inject interceptors when registering types to containers (remove attribute attributes on types) builder. RegisterType<Man>(). As<IPerson>(). InterceptedBy( typeof (LogInterceptor)). EnableInterfaceInterceptors();
// Dynamic injection interceptor builder. RegisterType<Student>(). InterceptedBy(typeof(LogInterceptor)) .EnableClassInterceptors();
using System; using System. Collections. Generic; using System. Linq; using System. Text; using System. Threading. Tasks; using Autofac. Extras. DynamicProxy2; namespace AutofacDEMO { /// <summary> /// Inherit the interface, implement the method, and add attribute to the type /// </summary> [Intercept( typeof (LogInterceptor))] public class Student { public string Name; public Teacher Teacher; public Subject Subject; /// <summary> /// Must be a virtual method /// </summary> public virtual void Say() { Console. WriteLine( " You are calling Say method! Student Name: " + Name); } } [Intercept( typeof (LogInterceptor))] public class Teacher { /// <summary> /// Must be a virtual method /// </summary> public virtual void Show() { Console. WriteLine( " I am Teacher's class ! " ); } } public class Subject { /// <summary> /// Must be a virtual method /// </summary> public virtual void Show() { Console. WriteLine( " I am Subject's class ! " ); } } }
using System; using System. Collections. Generic; using System. Linq; using System. Text; using System. Threading. Tasks; using Autofac; using Autofac. Extras. DynamicProxy2; namespace AutofacDEMO { class Program { static void Main( string [] args) { // There are two main methods to enable interceptors: EnableInterfaceInterceptors () and EnableClassInterceptors () // The EnableInterfaceConnectors method will dynamically create an interface proxy // The EnableClassInterceptors method will create a subclass proxy class of the target class. Note that it will only intercept virtual methods and override methods // Note: Autofac Extras. DynamicProxy2 can only use the above two methods #region Enable class proxy interception // Create interception container var builder = new ContainerBuilder(); // Register interceptor to container builder. RegisterType<LogInterceptor> (); // Method 1: Add attribute to the type builder. RegisterType<Student> (). EnableClassInterceptors(); builder. RegisterType <Teacher> (). EnableClassInterceptors(); // Method 2: Dynamically inject interceptors when registering types to containers (remove attribute attributes on types) // builder. RegisterType<Teacher>(). InterceptedBy(typeof(LogInterceptor)). EnableClassInterceptors(); // builder. RegisterType<Student>(). InterceptedBy(typeof(LogInterceptor)). EnableClassInterceptors(); // Attribute injection builder. Register(c => new Student { Teacher = c.Resolve<Teacher>(), Subject = new Subject(), Name = " Zhang San " }); using ( var container = builder. Build()) { // Get object from container var Student = container. Resolve<Student> (); Student. Say(); Student. Subject. Show(); Student. Teacher. Show(); } Console. ReadLine(); #endregion } } }
using System; using System. Collections. Generic; using System. Linq; using System. Text; using System. Threading. Tasks; namespace AutofacDEMO { /// <summary> /// Define an interface /// </summary> public interface IPerson { void Say( string Name); } }
using System; using System. Collections. Generic; using System. Linq; using System. Text; using System. Threading. Tasks; using Autofac. Extras. DynamicProxy2; namespace AutofacDEMO { /// <summary> /// Inherit the interface, implement the method, and add attribute to the type /// </summary> [Intercept( typeof (LogInterceptor))] public class Man: IPerson { public string Age; public void Say( string Name) { Console. WriteLine( " Men call Say method! full name: " + Name + " , Age: " + Age); } } /// <summary> /// Inherit the interface, implement the method, and add attribute to the type /// </summary> [Intercept( typeof (LogInterceptor))] public class Woman : IPerson { public void Say( string Name) { Console. WriteLine( " Women call Say method! full name: " + Name); } } /// <summary> /// Management /// </summary> public class PersonManager { IPerson _Person; /// <summary> /// Create objects dynamically based on the passed in type /// </summary> /// <param name="ds"></param> public PersonManager(IPerson Person) { _Person = Person; } public void Say( string Name) { _Person. Say(Name); } } }
using System; using System. Collections. Generic; using System. Linq; using System. Text; using System. Threading. Tasks; using Autofac; using Autofac. Extras. DynamicProxy2; namespace AutofacDEMO { class Program { static void Main( string [] args) { // There are two main methods to enable interceptors: EnableInterfaceInterceptors () and EnableClassInterceptors () // The EnableInterfaceConnectors method will dynamically create an interface proxy // The EnableClassInterceptors method will create a subclass proxy class of the target class. Note that it will only intercept virtual methods and override methods // Note: Autofac Extras. DynamicProxy2 can only use the above two methods #region Enable interface proxy interception (recommended) // Create interception container var builder2 = new ContainerBuilder(); // Register interceptor to container builder2.RegisterType<LogInterceptor> (); // Constructor injection (as long as the caller passes in the object that implements the interface, the object creation is implemented, in the following two ways) builder2.RegisterType<PersonManager> (); // Method 1: Add attribute to the type // Attribute injection builder2.Register<Man>(c => new Man { Age = " twenty " }). As<IPerson> (). EnableInterfaceInterceptors(); // builder2.RegisterType<Man>(). As<IPerson>(). EnableInterfaceInterceptors(); builder2.RegisterType<Woman>(). Named<IPerson>( " Woman " ). EnableInterfaceInterceptors(); // Method 2: Dynamically inject interceptors when registering types to containers (remove attribute attributes on types) // builder2.RegisterType<Man>(). As<IPerson>(). InterceptedBy(typeof(LogInterceptor)). EnableInterfaceInterceptors(); // builder2.RegisterType<Woman>(). Named<IPerson>("Woman"). InterceptedBy(typeof(LogInterceptor)). EnableInterfaceInterceptors(); using ( var container = builder2.Build()) { // Get object from container var Manager = container. Resolve<PersonManager> (); Manager. Say( " administrators " ); var Person = container. Resolve<IPerson> (); Person. Say( " Zhang San " ); var Woman = container. ResolveNamed<IPerson>( " Woman " ); Woman. Say( " Wang Meng " ); } Console. ReadLine(); #endregion } } }
Autofac has three life cycles: InstancePerLifetimeScope, SingleInstance and InstancePerDependency
// Method 2: dynamically inject interceptors when registering types to containers (remove attribute attributes on types) builder. RegisterType<Man>(). As<IPerson>(). InterceptedBy( typeof (LogInterceptor)). InstancePerLifetimeScope (). EnableInterfaceInterceptors();
Assembly assembly = Assembly. Load(assemblyName); // Assembly assembly = this. GetType(). GetTypeInfo(). Assembly; builder. RegisterAssemblyTypes (assembly). Where(type => !type.IsInterface && !type.IsSealed && ! type. IsAbstract && type. Name. EndsWith( " BLL " , StringComparison. OrdinalIgnoreCase)) .AsImplementedInterfaces() .InstancePerLifetimeScope() .EnableInterfaceInterceptors() .InterceptedBy( typeof (LogInterceptor));