Postsharp is designed based on the. NET platform, which emphasizes the easy-to-learn and easy-to-use AOP framework and the concept of AOP. For the advantages, see:
Http://www.cnblogs.com/wayfarer/category/35983.html
Here is a brief introduction to several scenarios using postsharp for your reference.
I. Logs
Generally, there are two ways to write business logs,
1. Simply record the occurrence time, trigger user, and service method name of the business method.
We record the traditional solution in this way.
Public Class Employee
{
Public Void Add ( String Employeename, String Employeepwd)
{
Console. writeline ( " Add employee name: {0}, password: {1} " , Employeename, employeepwd );
Logmanager. logwrite ( " Add " );
}
}
Class Logmanager
{
Public Static Void Logwrite ( String Methodname)
{
Genericprincipal GP = (Genericprincipal) thread. getdata (
Thread. getnameddataslot ( " Principal " ));
Console. writeline ( " Username: {0}, log time: {1}, method to trigger this log: {2} " ,
GP. Identity. Name, datetime. Now. tostring (), methodname );
}
}
// MasterProgram:
Static Void Main ( String [] ARGs)
{
Genericidentity Gi = New Genericidentity ( " Linear Filter " );
Genericprincipal GP = New Genericprincipal (GI, New String [] { " Admin " });
Localdatastoreslot localslot = Thread. allocatenameddataslot ( " Principal " );
Thread. setdata (localslot, GP );
Employee em = New Employee ();
Em. Add ( " Linear Filter " , " Linear Filter " );
}
(Because the console application is used here, we use a thread slot to store users for ease of testing. If you do not understand this part, you can temporarily ignore user information .) The log record problem of the traditional solution is that every method to record logs depends on the logmanager class, that is, all business logic depends on the system-level logmanager class, can we let them leave the system completely? Next we will use the postsharp method to solve this problem.
To use postsharp, you must first download (http://www.postsharp.org/) postsharp, and at least introduce postsharp. Laos, postsharp. Public Assembly after installation. Postsharp uses the attribute to link the business class with the system class. Therefore, we need to first define the samplelogattrigatclass as follows:
Use postsharp to record logs
[Serializable]
Class Samplelogattrict: onmethodboundaryaspect
{
Public Override Void Onsuccess (methodexecutioneventargs eventargs)
{
Logmanager. logwrite (eventargs );
}
}
Log Record type:
Class Logmanager
{
Public Static Void Logwrite (methodexecutioneventargs eventargs)
{
Genericprincipal GP = (Genericprincipal) thread. getdata (
Thread. getnameddataslot ( " Principal " ));
Console. writeline ( " Username: {0}, log time: {1}, method to trigger this log: {2} " ,
GP. Identity. Name, datetime. Now. tostring (),
Eventargs. instance. tostring () + " . " + Eventargs. method. Name );
}
}
Then, identify samplelogattribute on the method for logging.
Method
[Samplelog]
Public Void Add ( String Employeeid, String PWD)
{
Console. writeline ( " Username: {0} password: {1} " , Employeeid, PWD );
}
Then we run the program and get the following results:
Here we need to know about the onmethodboundaryaspect class. We can rewrite onentry and onsuccess to intercept the information of the method. onentry is triggered before the method that identifies the corresponding feature, onsuccess is triggered after the method that identifies the corresponding feature.
2. We may need to record more detailed information about the method, such as the update operation. We need to record the information before the update.
Logging featuresCodeAs follows:
Complex Logging
[Serializable]
Class Updatelogattrict: onmethodboundaryaspect
{
Public Override Void Onentry (methodexecutioneventargs eventargs)
{
Logmanager. logwrite (eventargs );
Foreach (VAR item In Eventargs. instance. GetType (). getproperties ())
{
Console. writeline ( " Original property name: {0}, original value: {1} " , Item. Name,
Item. getvalue (eventargs. instance, Null ));
}
Console. writeline ();
}
}
// Business type:
Class Employee
{
Public Employee ( String Employeeid, String PWD)
{
This . Employeeid = Employeeid;
This . Pwd = PWD;
}
[Updatelog]
Public Void Update (employee em)
{
This . Employeeid = Em. employeeid;
This . Pwd = Em. pwd;
Console. writeline ( " Updated: employeeid: {0}, PWD: {1} " , This . Employeeid, This . Pwd );
}
Public String Employeeid { Get ; Set ;}
Public String PWD { Get ; Set ;}
}
The result of running the main program is as follows:
In some cases, log records may be more complex, but in general, we can obtain the desired information by intercepting method parameters and instance attributes. You may think this writing feature is quite troublesome. postsharp provides a way to broadcast cross-cutting points. We can use the relevant features on the class and set its attributetargetmembers value, for example:
[Samplelog (attributetargetmembers = "add *")]
Class employee
{
}
In this way, all methods starting with "add" in the employee class will record logs, and the entire assembly can be set. You can refer to the documentation provided by postsharp.
Source code download