Simple and clean C # method design case: mfcui. checkboxlink (2)

Source: Internet
Author: User
Solution 2 Step 1: write a new function name under the original code Step 2: copy the original code to the followingIn two steps,
@ This. mergeparameter (@ mfcui. imagelink ("highlight the status of the storyboard", this. mergeparameter ("highlight", "onboard"), displayaxtextusecheckicon: True, displayastext :! String. isnullorempty (this. parameterof ("highlight") if (! String. isnullorempty (this. parameterof ("highlight") {@ mfcui. Link ("[cancel]", this. mergeparameter ("highlight", null ))}

Step 3: remove information that you do not want to expose from the end of the function brackets. What can be "active" is the real parameter.Deletion result:

@ This. mergeparameter ("highlight the status of the story board", "[cancel]", "highlight", "onboard ")

The others are either always a value or repeated.

Question: What if a parameter is a value most of the time while another parameter is another? Use the default parameter. For example, our imagelink only has a URL instead of an imageurl. How can we know what image to display? Because if the URL is http: // localhost: 53979/agile/storyboard/teamstoryboard? Teamid = 103 & Highlight = onboard, and imageurl is not specified (it is a default parameter), we will try to use http: // localhost: 53979/ Resources/images/Agile/storyboard/teamstoryboard16.png, so that if you can put all the icons in a directory, each connection will automatically search for its own icons unless it is specified deliberately (we only need to specify 10% cases ). In this case, we can achieve Information Hiding.
Step 4: create this function and copy the three lines of code to it.
Public static mvchtmlstring mergeparameter ("highlight the status of the storyboard", "[cancel]", "highlight", "onboard") {@ mfcui. imagelink ("highlight the status on the story board", this. mergeparameter ("highlight", "onboard"), displayaxtextusecheckicon: True, displayastext :! String. isnullorempty (this. parameterof ("highlight") if (! String. isnullorempty (this. parameterof ("highlight") {@ mfcui. Link ("[cancel]", this. mergeparameter ("highlight", null ))}}

Step 5: Hands-on Transformation

        public static MvcHtmlString MergeParameter(this WebViewPage page, string title, string titleToCancel, string parameter, string value)        {            var span = new TagBuilder("span");            span.InnerHtml +=                MFCUI.ImageLink(title, page.MergeParameter(parameter, value),                                displayAxTextUseCheckIcon: true,                                displayAsText: !String.IsNullOrEmpty(page.ParameterOf(parameter))).ToString();            if (!String.IsNullOrEmpty(page.ParameterOf(parameter)))            {                span.InnerHtml += MFCUI.Link(titleToCancel, page.MergeParameter(parameter, null));            }            return new MvcHtmlString(span.ToString());        }

Step 6: TestNote that the original code is retained during the test and a new line is added:

@ Mfcui. imagelink ("highlight the status of the story board", this. mergeparameter ("highlight", "onboard"), displayaxtextusecheckicon: True, displayastext :! String. isnullorempty (this. parameterof ("highlight") if (! String. isnullorempty (this. parameterof ("highlight") {@ mfcui. link ("[cancel]", this. mergeparameter ("highlight", null)} <br/> @ This. mergeparameter ("highlight the status of the storyboard", "[cancel]", "highlight", "onboard ")

The advantage is that you can check the problem with the code at any time.

Entire 1 ~ The 6-step process took 10 minutes and the test was successful, ...... We found that when we saw this interface, we didn't want to click the cancel button at all. Instead, we wanted to click the check icon. So can we change it to "cancel" and click the check icon directly? Now, the demand is changed. In addition: @ This. mergeparameter (...) is also a nondescribedomainname function. Who guessed that this (webviewpage) function is required to generate a link? So you should change the location. After that, the design and reconstruction are all done. Unfortunately, this is the norm of development, that is, the demand is always changing, and the design is always restructuring! At this time, do we want to face a line of code or several lines of code in bulk? (If there is no imagelink, there will be more code.) Besides, there are many duplicates in it, which may need to be changed or not.
This is the purpose of information hiding: to securely and efficiently modify the least-places and texts when calling again, changing requirements, and restructuring designs.
For more information about how to modify it, see the next 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.