Before doing agentbooking, it is difficult to encounter two problems, one is abnormal delivery and capture: how to reasonably pass through the layer code calls uniformly and uniformly catch exceptions. Because if there is a way to deal with the exception, you can make the code to reduce a lot of try Cath logic, but also different time to pay attention to how to throw the exception, so the code is more happy to write. The second one is the unified record of log. As with the first question, if you can find a way to keep a log in mind, you don't have to focus on how to remember log at any time in your code, which is certainly a better improvement. last two weeks to search for AOP (aspect-oriented programming), the idea is to a great extent helpful. AOP is probably meant to automatically insert additional functional logic into the program during the compile or run time of the code. The idea is to separate the main business logic from the auxiliary functions. There is a classic application in Moe: Every time Moe calls the Web service, it goes home to the RQ serial number and saves it to a txt log file, so this method of calling WebService has both the main business logic (RQ Generation) code, It also has the auxiliary function of RQ into log. If AOP is applied, then this method only needs to write the logic of the main business, and the logical code of RQ into log can be handed to the AOP framework to automatically "insert" it into the original code at compile time (or static). There are probably two ways to implement AOP, one is to parse the Il of the code, and then insert (the official call "weave in") the extra functionality of the code. The second is to intercept the method call and execute additional code logic. . NET to intercept method calls is mainly through the context object. Reference: AOP research, context and interception Here is the main introduction to context and method interception. (Because IL involves compilers, too esoteric, unknown, and methods intercept other than AOP, feeling can also be used elsewhere) in. NET, each app domain (application domains, In. NET a process can have n threads, and a thread can have n app Domain) at least one context, and when an object is new, it exists in the context. (I think the context is more of a programming idea, is to put resources in one place.) Typically with HttpContext, the focus is on some of the information that each HttpRequest accesses). The first cont of a. NET programExt is a default context, and each context has a contextid. You can access the current context through Thread.currentcontext. There are two types of objects in the . NET from the context perspective: Context-aglie Object (Context flexible object) and Context-bound object (context bound object).
Context-aglie object is always present in the context of the caller of the object (I checked the information, presumably, but I haven't verified it yet)。 A call to a Context-aglie object is an intuitive reference, not a call through a proxy. This is important, because direct calls cannot be intercepted, only by "proxy" calls can be intercepted, see below. The other is the Context-bound object (the context-bound object). If an object inherits ContextBoundObject, it becomes a Context-bound object. The Context-bound object is forced to bind to a specified context. When a caller and the object being called are in a different context, the call is not a direct reference, but it needs to go through the proxy. It is because of this "agent" that it is possible to tamper with the calling process, for example, by making some extra logic (such as log, or time statistics for method execution, etc.) before invoking the object's method or after calling the method, which sounds familiar? This is also the practice in HttpModule. There are several "agents" here: Remote proxy, virtual proxy, Smart Reference Proxy. In. NET remoting, agents are divided into transparent proxies ( Transparent proxy) and real proxiesthe interception of a method call requires a transparent proxy and a real proxy. When an object invokes an object in another context (for example, a method that invokes the object), the call is converted to a message when it reaches the transparent proxy, and the message is passed to the real proxy. Real Proxy passes the message to this object, and the message goes through a pipeline of n messagesink (message slots). Each messagesink can process the message and toss it again to the next messagesink, passing the call to that object all the time. Every messagesink is where we intercept method calls. Provides a context intercept-based Logger log component Demo:aop Logger
C # AOP Programming: