A multi-page ASP.NET + C # solution. Where should I use my global utility features?

As Title says, I have a multi-project solution.

I have a β€œcore” project that is loaded by most other projects.

So my question is that I have some useful features, such as FormatPhoneNumber (..), with which I would like to access as follows from anywhere.

(in Project_B, which depends on Core)

string str = FormatPhoneNumber(inputString);

In the worst case, I suppose I could live with a specific classifier:

string str = util.FormatPhoneNumber(inputString);
+3
source share
9 answers

- dll ( , - "CommonCode"?), DLL .

- "" ( ) -, using , .

using util;
+4

( , , / ), Core (, Core ) . , , ILMerge.

. Utils, , , a Formatting . , s_ruchit , (, ) .

( , %% & MarkDown [at] , -? Sigh.)

+1

.
.

, [companyName].Util. [subdomain] , , [CompanyName].Utils.StringHelpers

StringHelper FormatPhoneNumber. , . , , .

+1

, .

public static class Util {
    public static string FormatPhoneNumber(this string input) {
        :
    }
}

. , . , , .

string formattedString =  inputString.FormatPhoneNumber();
+1

# 3.0, , .

0

.NET , . , :

public class Utility
{
  public static string FormatPhoneNumber(string input)
  {
    ...
  }
}

// usage:
string output = Utility.FormatPhoneNumber(input);

, () .

0

. ( # ):

<%= Formatters.PhoneNumber(rawData) %>

: , ( ).

: , ( ASP.NET). .

0

, Core (, , , "Utils" ) DLL . , . , , .

0

If you want all the code to access these methods, go to extension methods, otherwise I would go with the Util class in the kernel assembly.

FWIW, if you follow a more formalized namespace like boris suggestuests (avoiding conflicts is recommended), you can shorten the using keyword:

using Util = [CompanyName].Utils.StringHelpers;

I try to follow the DRY principle and create an alias as soon as I need it more than once.

0
source

Source: https://habr.com/ru/post/1711560/


All Articles