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.