AspectSharp example analysis

xiaoxiao2021-03-06  59

AspectSharp comes with an example of a simple Logger interceptor and a persistent mixer. Let's take a look at its working principle.

1. Class and Interface Description: IMessage: Message object interface (object to be achieved aspect of the interface must be declared?) MessageImpl: implement IMessage Interface; IMessagePersistence: Message object persistence interfaces; LoggerInterceptor: Log interceptor; MessagePersistenceMixin: Message durable mixer Implement an IMESSAGEPERSISISTENCE interface; 2. Main program: // Creating the proxyimage message = aspectsharpengine.wrap () as iMessage; message.Message = "test"; // Note: Obviously this message is not really realistic IMessage objects will not be able to intercept method .//Casting to the persistence serviceIMessagePersistence persistence = (IMessagePersistence) message; // Note II: message object does not implement IMessagePersistence Interface persistence.Save (); completely unable to see this code! Imagine how the AOP works, a bit of a bit of the feeling of turning the day; the key is on this proxy object, let's make one such assumption: message.Message = "Test"; this message is not a instance of MessageImpl, then this message is what? Maybe Aspect object (Aspect object contains all interceptors and mixers of the object). It wraps the MessageImpl object. It cannot be explained that aspect does not inherit from the iMessage interface? IMessagePersistence persistence = (iMessagePersistence) Message; this is easier to understand, though Message Didn't implement iMessagePersistence, but you can get what we want as long as you overload the AS operation.

3. AspectSharpengine

Look at the name, ask the AspectSharp core object, responsible for the encapsulation object. Let's take a look at the code of the package:

public static object Wrap (object target) {Type originType = target.GetType (); string typeName = originType.FullName; // Invocation of obtaining processed, default DefaultInvocationHandler IInvocationHandler handler = GetInvocationHandler (target); // process the acquired MixinInvocation Objects, defaults to MixinivationHandler iMixinInvocationHandler MixinHandler = getMixinIvocationHandler (); // Get all mixers on the object, declare in the configuration file. IMixin [] Mixins = getMixins (Typename);

/ / Check the interface type [] interfaces = getInterfacesFrom (ORIGINTYPE); // acquiring the object type of the agent, pay attention to the object's object type, note that the object is entered Interfaces and all mixers interface, / / ​​Will I establish an object type that implements all of these interfaces? Type ProxyType = proxygenerator.createProxytype (Interfaces, Mixins); _ typecache.add (TypeName, ProxyType);} // Return packaged package examples of target return ProxyGenerator.CreateInstance (_typeCache [typeName], mixins, handler, mixinHandler);.} 4 is responsible for creating a proxy object types and instances ProxyGenerator /// Generates a proxy transient type implementing all the specified interfaces and mixins /. // redirecting method invocations to the specifed handler. The above is a description of the CreateProxyType method. If I think, I do to build a proxy object that implements all interfaces. Year! I have a little admire yourself. (Throw it over, Oh) internal static Type CreateProxyType (Type [] interfaces, IMixin [] mixins) {// ... // Create a new assembly AssemblyName assemblyName = new AssemblyName (); assemblyName.Name = "DynamicAssemblyProxyGen";

// definition of a dynamic assembly AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly (assemblyName, AssemblyBuilderAccess.Run); ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule (assemblyName.Name, true);

Typebuilder typebuilder = modulebuilder.definetype ("Proxytype", TypeAttributes.public | typettributes.class, null, mergetypes (interface, mixins));

Fieldbuilder Handlerfield = Generatefield (Typebuilder, HandlerfieldName, Typeof (IinvocationHandler);

FieldBuilder mixinHandlerField = GenerateField (typeBuilder, mixinHandlerFieldName, typeof (IMixinInvocationHandler)); FieldBuilder mixinsField = GenerateField (typeBuilder, "mixins", typeof (IMixin []));

ConstructorBuilder constr = GenerateConstructor (typeBuilder, handlerField, mixinsField, mixinHandlerField); GenerateInterfaceImplementation (typeBuilder, interfaces, handlerField); GenerateMixinImplementation (typeBuilder, mixins, mixinHandlerField, mixinsField); return typeBuilder.CreateType ();} The above code look a little big head, Refer .net sdk, just know the type of object to establish a method to achieve all interfaces like. public static object CreateInstance (type type, IMixin [] mixins, IInvocationHandler handler, IMixinInvocationHandler mixinHandler) {return Activator.CreateInstance (type, new Object [] {handler, mixinhandler, mixins});} Create an instance directly, the type of object is to move in CreateProxyType. Not aspect. Judgment Error!. It seems that the head is also clear. The code is clear. However, there is a question, the dynamically established constructor is added to the code? 5 Profile profile content is relatively simple. // Define Interceptor // Define the mixer // Declaration to Aspect object and mixer // To intercept objects, Obviously, the key to the AOP is in that dynamically established implementation of all interfaces On the agent object, how does this agent object interact with the interceptor and mixer we define? Please pay attention to the follow-up article.

转载请注明原文地址:https://www.9cbs.com/read-119577.html

New Post(0)