Title: Did not end without tail - Project development notes: abnormal processing and logging code automatic generation (how many code can be generated !? Continued)
Keywords: distributed development C # project division of labor Delphi and C # mixed development AOP code generation tool exception handling logging
December 7: Separation and processing of abnormalities and logging in the project from the idea of AOP
The last code generation tool is a code generation tool from the system's aspect. The relatively thick written, just definitions the code generation tools to correspond to the purpose of our project, and implement several means. It may be abstract. The application of the code generation tool is explained in this project to explain the application of the code generation tool in this item.
l Exception handling mechanism for logging
The unified processing of exception processing and logging here is partially handled. In the entire system, the processing of exception handling and log can only be done anywhere. This approach is only generated by 80% of logs and abnormal processing using code generation tools. For special exception handling, or every programmer to get J.
Abnormal processing and logging of logs should be a completely different two events. But basically I think this two events are independent of business processes, and in our system, exceptions must be recorded to log processing. So I consider uniform processing.
When I started, I went to read several reference information about C #. According to the experience of the Java application server, C # corresponding to the class can throw exception to the outermost layer. When I follow the C # code, I also found that when C # is often a very simple call, it is also possible to call a lot of code. (Microsoft's habits, I found L. when I was written before writing, so I would like to be indispensable.
Honestly, I didn't think about how the log and exception handle should be done when I started. Just have a very clear idea to take the process of these two processes together. So now, this issue is unified at the beginning of the project. Have the code to generate tools, will not be troublesome.
In the 11th programmer magazine introduced the method of AOP, I think it will be beneficial. I think the core of AOP is undoubtedly the four words for aspect. As for specific implementation, there is no version for C # present. Then we can only apply its ideas. I think that his idea is more clearly divided into the development of interest in the development program, thereby dividing the development. But I didn't see the organization of the development process staff.
(Every time I think we write a library without an abnormal handling mechanism, the interception method of using AOP from the outside may have no way to take out the wrong message and record. It may not be able to achieve only by intercepting the message. Uniformly handle an abnormal mechanism? I don't know what my friends have any opinion.)
In our system, after the level division, the server-side call can be divided into two large parts. The web service is defined according to the request of the client. And others, such as facade, business rules, do, etc. are all existing in the form of a library. (You can find a pile of DLLs on the directory of the Web Service's bin. In this case, the principle of project group definitions is that it is best not to capture exceptions in the library (unless an exception indicates that the code can be processed), and you can process them from the Web Service call code. (Refer to C # Advanced Programming Book.)
The result of the final project team discussion is to use C # to put exceptions can always throw out the proximity of the outermost layer. Only the process of writing uniform exceptions in the outermost web service layer. (Special exception handling everyone knows yourself.) And because the structure defined by the FACADE layer and the Web Service layer is processed for user design. (You can refer to the previous article: How many code can be generated!?) That is to say that the structure is exactly the same. E.g:
If the ProductFacade.cs of the Facade layer has the following functions: ///
/// Update All Change Product Data Item.
/// summary>
Public int ApplyAllChangeItem (String XmlstringofproductData)
{
........................
}
Then there will be propOductService.asmx.cs in the web service layer. There will be the following methods:
[WebMethod (EnableSession = true, description = "Update All Change Product Data Item.")]]
Public int ApplyAllChangeItem (String XmlstringofproductData)
{
Return New ProductFacade (). ApplyallChangeItem (XmlstringoftOfProductData);
}
(Code section)
The above Web Service method does not include exception and log processing, if you add processing to logs and exceptions, this method is written.
[WebMethod (EnableSession = true, description = "Update All Change Product Data Item.")]]
Public int ApplyAllChangeItem (String XmlstringofproductData)
{
Try
{
Return New ProductFacade (). ApplyallChangeItem (XmlstringoftOfProductData);
}
Catch (Exception E)
{
Try
{
LasTerror = E.Message;
Throw New ExceptionEx (e);
}
Catch
{
HTTPCONTEXT.CURRENT.RESPONSE.REDIRECT ("Common.asmx / geterrMessage?", False);
Return NULL;
}
Finally
{
}
}
Catch
{
Try
{
Lasterror = "unknowError! @@";
Throw new ExceptionEx (lasterror);
}
Catch
{
HTTPCONTEXT.CURRENT.RESPONSE.REDIRECT ("Common.asmx / geterrMessage?", False);
Return NULL;
}
Finally
{
}
Return NULL;
}
Finally
{
}
}
(Code section 2)
(Note:
u ExceptionEx is an exception class we defined by our own, and developers can throw this exception when the program appears in the development process. It is also possible to deal with the exception. I will throw it out of the wrong DOTNET.
u ExceptionEx is responsible for the unified log processing. As long as New ExceptionEx will log.
)
l Code generation tool application
The above analysis of the abnormal problem is actually at the beginning I didn't want to be clear. That is, when the system starts, we did not consider how to deal with uniform processing. (I don't know if I have not friends, it is also true that I started only the exception of the exception of the database connection with the colleagues responsible for the database part. Unified that process. (I have already pointed out that we are reckless programming, depressed. Or my experience is not enough.) And I have generated some of the web service code before this exception is not coming out above. So after this processing is defined, we have to do two things: 1. Modify the code that has been completed, join a unified process
2. Automatically generate web service code according to the FACADE layer
The following is directed to the code generation tool for both cases;
2 Modify the completed code, join a unified process
Or use the previous statement, making a code generation tool must figure out what three problems, input, generation methods, and outputs are you?
For the completed code, it must be modified the Web Service file already available in the Web Service directory. The modified part must be processed for the method of Web Service in each Web Service class. Then add the portion of the exception handling such as try. The following is a description of the specific process:
1. Get the directory of the Web Service by inputting;
2. Traversing the directory, find all the last eight characters, ".asmx.cs" file; (Web Service class file)
3. Read the contents of the file into a file string;
4. Cycling looks "[WebMethod" is the string position starting;
5. Find the first "{" position under the string position of the above steps, and then find the position of "}" corresponding to the first "{" according to the correspondence of the curly bracket;
6. Extract the Web Service's function by 4 and 5 and store a function string.
7. Insert "Try {" after the first "{" position; (TRY characters are read from the template file)
8. Insert "catch ................" before the last "{" position; "(Catch characters are read from the template file)
9. Replace the newly entered function string to replace the original function string in the file string
10. Repeated 3-9 steps until the file character is tail;
11. Write the file string into the file and replace the original file.
With this process, all the code similar to code segment one can generate code segment two code. Developers can write TRY and CATCH code.
If a new anomaly class is added during the development process (such as generating exceptionEx1, ExceptionEx2), what we have to do is to modify the template file, and update all the files of the web service.
2 Automatically generate web service code according to the FACADE layer
This process is different from the modification process is that the input is not to read the existing web service file, but to read the code of the FACADE layer, generate the web service file. The following is a specific process description:
1. Get the directory of the FACADE layer and the directory of the Web Service;
2. Read the Web Service generating template. Read it in a large string.
3. Traverse the FACADE directory to find all the last nine characters that are "facade.cs" files; (we define the FACADE file); set to xxxfacade.cs4. Read the contents of the file into a file string;
5. Cycle look at a string position starting with "/// summary>"; (add the last one of the comments when generating a Facade method.)
6. Add a string of a new method in the Web Service string;
7. Find the first "{" location under the string position of the above steps, and then find the position of "}" corresponding to the first "{" according to the correspondence of the currency;
8. Extract the Web Service's function by 4 and 5 and store it in a function string.
9. Method of reading the FACADE in the function string. (If you are a function attribute, it is skipped.) Reads the parameters of the FACADE method in the function string.
10. Modify the replacement characters in the web service string, write the method of the FACADE, and sequestration.
11. Repeated 3-10 steps until the FACADE file string;
12. Write the newly generated Web Service string to xxxService.asmx.cs and xxxservice.asmx files. (XXXService.asmx file is very small, not related to the function.)
The text of the web service generated in this way will correspond to the class and functions of all FACADE layers. Further, the exception processing of abnormal exceptions has been uniform. Reduced the workload of developers and opportunities for errors. Enable debugging to find an error log, facilitate the debugging process.
I haven't seen any big problems in such a generated code, I hope that my friends have given me a good suggestion.
Also, I have some ideas for the efficiency of code generated by the code generation tool, and I will talk to my friends.
If the priority of the project is unclear, you can refer to the previous document:
http://www.9cbs.net/develop/author/netauthor/viktoryu/