Common functions: how to structure?

How should you structure your frequently used non-important functions (transformations, etc.) in C #, since everything should be contained in an object? Usually I take all these functions and put them in the static Utility class. Is this a good practice? How do most developers do it?

+4
source share
4 answers

This is usually what most developers, including me, do.

The only mistake is that you start to have massive utilities that do not have to be all together (conversion, access to the database, registration, which you name ...).

Try to structure them as different assemblies, so projects that do not need a set of utilities will not have to import a large set of extraneous code.

+3
source

The elegant method for this in C # uses extension methods ( Link ). You can then import the appropriate namespace if you need your utility functions.

On the technical side, this is basically just a static utility class, as you described it, but in C # you get good syntactic sugar for using them.

+1
source

Depending on the specific need, I usually use a static structure or a registered service provider. For example, for logging, I usually use a service provider and a logging class with the type of interface or base logger, and then in dev mode I register a message box provider so that I can see all logged lines for a specific log level, while to get a specific predicate List.find for the list of objects I will create a utility class that has a static method that returns a predicate class that matches the type.

0
source

Secondly, the concept of using extension methods for this. I usually have classes in the .Extensions namespace, like StringExtensions , IEnumerableExtensions , etc. Basically, I am trying to create a separate class for each logical grouping of functionality. Usually this means that I group them by the type that they extend, the type that they create, or the general function that they support (conversion, formatting, etc.).

0
source

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


All Articles