0

I have a solution with a "Common" project. This "Common" project is used by other projects in the solution.

Within this "Common" project, I have a "Utilities" folder with several different utility classes, for example, "CsvUtilities.cs" and "JsonUtilities.cs". Assume that I could have many classes like this, and that all methods in these classes are pure functions. Based on this, it would make sense for these classes and methods to be static. Then from other projects I can import the common project and do things like:

CsvUtilities.StaticCsvMethod();
JsonUtilities.StaticJsonMethod();

This works and I think this is relatively normal.

Now the complication is that I want to create a hierarchy structure to access the static utility methods. I want to be able to type "Utilities." in the IDE and have intellisense show all existing utility classes, followed by all methods within that utility class.

So the code that I want would look like this:

Utilities.Csv.StaticCsvMethod();
Utilities.Json.StaticJsonMethod();

I implemented this idea by doing the following:

public static class Utilities
{
    public static CsvUtilities Csv { get; } = new CsvUtilities();
    public static JsonUtilities Json { get; } = new JsonUtilities();
}

However, there is a big problem with this solution. For this to work, the various utility classes and their methods must no longer be static, which is awkward for utility classes/methods.

I was not able to find an example of someone else doing something like this. What is the most reasonable way for me to use this "Utilities." structure while also keeping the utility classes/methods static?

  • 1
    By using `Namespaces` or partial nested classes? – Sani Huttunen Feb 26 '20 at 21:46
  • You could do nested static classes. – juharr Feb 26 '20 at 21:46
  • I'd prefer namespaces over classes. Classes aren't really meant to *just* categorize other classes. Namespaces on the other hand, basically are. – Broots Waymb Feb 26 '20 at 21:48
  • 1
    Why is it important for that hierarchy to be used at the call site? Just so `Utilities` can be used as a shortcut in IntelliSense? Typically however types are organized is irrelevant when using them because you'd import them with `using`, anyways; that is, you don't see a full or partial namespace at the point of usage because there's no need to. – Lance U. Matthews Feb 26 '20 at 21:58

2 Answers2

1

You can use namespaces (or nested classes) to nest your calls like that. See following example

namespace Utilities
{
    public static class Json
    {
        public static void StaticJsonMethod()
        {
            // Do something
        }
    }
}

you can call that method using Utilities.Json.StaticJsonMethod().

To add another level, you just append the "category" to the namespace:

namespace Utilities.Formats
{
    public static class Json
    {
        public static void StaticJsonMethod()
        {
            // Do something
        }
    }
}

you can call that method using Utilities.Formats.Json.StaticJsonMethod()

Dávid Kaya
  • 924
  • 4
  • 17
  • I agree that this will work and I like the idea. But is it bad that it means the namespace won't be following the project structure? For example, if it is in a project named "Common" then I would expect the namespace to also start with "Common". Also, should I have any concern about this namespace possibly conflicting with other libraries because the name is so generic? – user3847931 Feb 26 '20 at 22:34
0

You can have Utilities.Json.StaticJsonMethod(); if you nest static class Json inside Utilities

public static class Utilities 
{
    public static class Json
    {
         public static void StaticJsonMethod() { }
    }
}
Yehor Androsov
  • 4,885
  • 2
  • 23
  • 40