C # examples of simple Aop literacy and ORM Object Property Interception

Source: Internet
Author: User

First, let's talk about the scenario. Why do I use Aop in C #, and where do I use Aop?

I just want to intercept the Set Method of the object class, and then call other methods before the Set to assign the value to another object.
 
What I do is to process in the base class of the object class:
For example:
Public class OrmBase
 
Let all the entity classes that inherit this base class have the Orm operation function, and add a small special requirement for processing, when the attribute Set, you need to assign a value to another object.
 
If we say this, we can provide methods in OrmBase so that all subclass attributes can be operated like this:
 
 
Public class Users: OrmBase
{
Public int _ ID;
Public int ID
{
Get;
Set
{
Base. SetXX (value );
}
}
 
 
However, every entity is written in this way. Although it is okay, it can be simplified.
 
In a world that can pursue conciseness, of course, you prefer concise writing methods such:
Public int ID {get; set ;}
Therefore, you can directly intercept the set Method of the subclass in the base class and call SetXX directly. How can this problem be achieved? It took another day to study and study the data.
 
To do this, you have to use Aop:
Traditional Aop uses RealProxy, which is very simple to use, but it is very complicated to be fooled. below:
 
1: add an attribute identifier to the class to be intercepted and inherit from ContextBoundObject:
 
[AopAttribute]
Public class OrmBase: ContextBoundObject
 
OK. Add one to the base class so that all the sub-classes are appended and an identifier can be blocked. What is the AopAttribute attribute? See the following:
 
2: AopAttribute inherits the proxy attribute identifier class, which is used to hold the class to be intercepted:
 
 
Class AopAttribute: ProxyAttribute
{
Public override implements albyrefobject CreateInstance (Type serverType)
{
AopProxy realProxy = new AopProxy (serverType );
Return realProxy. GetTransparentProxy () as your albyrefobject;
}
}
 
 
Look, there are two lines in it. It is very simple. The AopProxy class inherited from RealProxy is called in the middle. What is AopProxy? How did it come out? See the following:
 
3: AopProxy class: it refers to the intercepted message processing. First, a simple version is provided, which is not understandable to everyone:
 
 
Class AopProxy: RealProxy
{
Public AopProxy (Type serverType)
: Base (serverType)
{
}
Public override IMessage Invoke (IMessage msg)
{
// After the message is intercepted, the method is executed here.
}
}
 
OK, that's simple. There are two classes to intercept, but the point is that the Code after intercept is a little complicated. Generally, just copy it. The interception code is as follows:
 
If (msg is IConstructionCallMessage) // if it is a constructor, return it in the original way.
{
IConstructionCallMessage constructCallMsg = msg as IConstructionCallMessage;
IConstructionReturnMessage constructionReturnMessage = this. InitializeServerObject (IConstructionCallMessage) msg );
RealProxy. SetStubData (this, constructionReturnMessage. ReturnValue );
Return constructionReturnMessage;
}
Else if (msg is IMethodCallMessage) // if it is a method call (attribute is also a method call)
{
IMethodCallMessage callMsg = msg as IMethodCallMessage;
Object [] args = callMsg. Args;
IMessage message;
Try
{
If (callMsg. MethodName. StartsWith ("set _") & args. Length = 1)
{
// The set method is detected here. How can I call other methods of the object?
}
Object o = callMsg. MethodBase. Invoke (GetUnwrappedServer (), args );
Message = new ReturnMessage (o, args, args. Length, callMsg. LogicalCallContext, callMsg );
}
Catch (Exception e)
{
Message = new ReturnMessage (e, callMsg );
}
Return message;
}
Return msg;
 
In order to call other methods of the original object, I spent nearly one day checking the information. Unfortunately, there is no relevant information on the network, and most people use it, is directed to another method (a method that does not need to call the original object)
Currently, there is too little information about Aop on the network and less information about C #. I am struggling to find a related article about how to obtain the original object and then call the original object.
As a result, I tried my best to get the original object and call it again in the traditional way. After strokes, I still failed.
(In the beginning, I thought: Using Reflection to create another entity from the type. This is an unreliable attempt: This creates an endless loop. Every time a new interception occurs, there is a new one in the interception)
Saving a lot of experience and attempts .......
As long as I thought, the method was always there and I finally found it:
1: Obtain the method to be called:
In the constructor, obtain the SetXX method MethodInfo Based on the passed serverType:
Method = serverType. GetMethod ("SetXX", BindingFlags. NonPublic | BindingFlags. Instance );
2: Call in the interception method:
 
If (callMsg. MethodName. StartsWith ("set _") & args. Length = 1)
{
Method. Invoke (GetUnwrappedServer (), new object [] {callMsg. MethodName. Substring (4), args [0]}); // call the attribute
}
 
The process is very complicated. I have tried N hundred methods and the results are very simple. Sharing is very important!
To solve this problem, ORM intercepts the attributes of sub-classes and calls other methods of instances when assigning values to properties.

 

 

From the fall

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.