Problems in public code based on ERP programs and solutions for over-encapsulation and inconvenient maintenance

Source: Internet
Author: User

When designing an ERP program, it is necessary to extract public code into a common type library. This can reduce code duplication and improve code utilization.

However, there must be a degree to do everything. Some public code causes excessive encapsulation, which is not conducive to the understanding of the Code.

Example:

Copy codeThe Code is as follows: public class ConfigHelper
{
/// <Summary> /// Gets whether the specified path is a valid absolute file path. // </summary>
/// <Param name = "path"> Any path. OK if null or empty. </param>
Static public bool IsValidPath (string path)
{
Regex r = new Regex (@ "^ ([a-zA-Z] :) | (\) (\ {1} | (\ {1 }) [^ \] ([^ /:*? <> "" |] *) +) $ ");
Return r. IsMatch (path );
}

Public static string GetString (string key)
{
Return System. Configuration. ConfigurationManager. deleetask[ key];
}
}

The second method is GetString. I thought its encapsulation is unnecessary. The code that calls the. NET Framework has only one or a few simple lines. Encapsulating the code will lead to obstacles to understanding.

Let's look at another method. The encapsulation of it depends on the specific use case.

Copy codeThe Code is as follows: public static decimal GetDecimal (string key)
{
Decimal value = default (decimal );
If (decimal. TryParse (GetString (key), out value )))
{
Return value;
}
Else
{
Return 0 m;
}
}

This method converts a string to a numeric type. If its value is not numeric, the default value 0 is returned.

Depending on the scenario, this encapsulation may be necessary to reduce a lot of repetitive code.

You are welcome to give your comments. I think this GetDecimal method is redundant and unnecessary encapsulation.

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.