The implementation of nvelocity violates the LSP rule, causing an afternoon for troubleshooting a low-level error.

Source: Internet
Author: User

Recently, I am working on a CMS system. I need a template engine and chose nvelocity. nvelocity is the Java open-source template engine. velocity has been transplanted to the. NET platform. Because the template syntax of velocity is very simple, I chose it as the template engine of our CMS. Nvelocity seems to have been maintained by the castle project. I found an entry-level article on the castle project website, which seems simple and easy to use. Then I will code it according to his example:

VaR velocityengine = new velocityengine ();

VaR props = new extendedproperties ();
Props. Add (runtimeconstants. input_encoding, "UTF-8 ");
Props. Add (runtimeconstants. output_encoding, "UTF-8 ");
Props. Add (runtimeconstants. file_resource_loader_path, targetviewfolder );
Velocityengine. INIT (props );

Props. the three "add" statements do not exist in the example. I am smart enough to add them, because nvelocity will be configured by default if no configuration is input, but it is wrong to write this statement, let us know why it is wrong.

This code has been compiled, and there is no problem after it is run, but I am in velocityengine. A problem occurred while gettemplate. An exception was reported, indicating that the resource could not be found. The exception was found during breakpoint debugging.

Why does my configuration fail to take effect, but it is still the default setting? Then I found some examples on the Internet. I didn't know where the error was due to the "old eyes and shadows", and finally I couldn't find a way to trace the nvelocity source code, I downloaded the complete source code on the website of the castle project, including the nvelocity code, but the project lacks direve ve. properties and nvelocity. properties connects to a file. I first removed this file from the project and found that although the file can be compiled smoothly, the default configuration is unavailable when nvelocity is used, I have to compile nvelocity. DLL has obtained the two files (previously I do not know how to obtain the resource files in the DLL, and studied for N long), finally compiled successfully, one-step Adjustment, followed by Init (props) the method goes in and looks at it. It is really strange:

 

How can p. Keys be absent from one element? In fact, there is something in the props variable for monitoring, but there is nothing in keys. Then I found the cause through the analysis of extendedproperties:
1) extendedproperties inherits the hashtable
2) extendedproperties maintains the keys

Public new ienumerable keys
{
Get {return keysaslisted ;}
}

Therefore, the add method is implemented by hashtable itself. extendedproperties when adding elements using the add method. keys has not been added. We should use the extended addproperty method to maintain keysaslisted only when addproperty is executed. After I modify the initialization code, I find everything is normal. Later, I went back to the online examples and used addproperty !!! Finally, paste the correct initialization code

VaR velocityengine = new velocityengine ();

VaR props = new extendedproperties ();
Props. addproperty (runtimeconstants. input_encoding, "UTF-8 ");
Props. addproperty (runtimeconstants. output_encoding, "UTF-8 ");
Props. addproperty (runtimeconstants. file_resource_loader_path, targetviewfolder );
Velocityengine. INIT (props );

 

However, this time I was confused and understood the LSP rule. In object-oriented practice, there are some rules. I often only know what it is, but I don't know why. Finally, I will talk about what the LSP rule is. :

LSP: The liskov substitution principle
The subclass must be able to replace the base class. Subtypes must be substitutable for their base types.

Extendedproperties inherits hashtable, but does not replace the base class. Instead, it will lead to abnormal behavior, which leads me astray, a low-level error, one afternoon, but it will always be solved before work.

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.