简体   繁体   中英

Creating a static utility class in .net core web API is correct approach?

I am planning to create a general utility class for base 64 encoding and decoding of strings in asp.net core web api 3.1 application.

Following is my method

namespace CoreAPI.Utilities
{
    public class Base64
    {
        public static string Base64Encode(string plainText)
        {
            var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
            return System.Convert.ToBase64String(plainTextBytes);
        }
        public static string Base64Decode(string base64EncodedData)
        {
            var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
            return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
        }
    }
}

And called the same from my controllers as shown below

 [HttpGet("api/clients/{clientid}")] 
 public ClientData GetClientDetails(int clientid)
 {
      string encodedclientdata="TestData" //actually fetched from DB
      string Decodedvalue = Base64.Base64Encode(encodedclientdata);
      //added other logic to return clientData object
 }

Now my doubt is that creating static utilities will create any issues while users parallelly accessing the api? will that create any concurrent issues? creating static classes in web api / web application is a best practice?

If this is a wrong approach how can i add general utility method in .net core web api/web applications? Please guide me !

You are could implement a extension method like this:

namespace CoreAPI.Utilities
{
    public static class StringBase64Extensions
    {
        public static string Base64Encode(this string plainText)
        {
            var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
            return System.Convert.ToBase64String(plainTextBytes);
        }
        public static string Base64Decode(this string base64EncodedData)
        {
            var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
            return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
        }
    }
}

And use it like this:

 [HttpGet("api/clients/{clientid}")] 
 public ClientData GetClientDetails(int clientid)
 {
      string encodedclientdata="TestData".Base64Encode() //actually fetched from DB
      string Decodedvalue = encodedclientdata.Base64Decode();
      //added other logic to return clientData object
 }

This Extension Method approach is widely used in c# for example in System.Linq there mostly extension methods.

An extension method would probably be a nicer way to implement this ( Kuechlin's answer ), but you won't have problems with parallel threading this way either, because you only use local variables in your methods. These local variables only exist in that specific thread, that executes the function, and can't be accessed from outside the method's block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM