简体   繁体   中英

Should I use static method (C#)

Should I use static in the following 2 cases:

Case 1)

public class RequestHeader
{
    private string Username { get; set; }
    private string Password { get; set; }
    private string AccessKey { get; set; }

    public string url { get; set; }
    public string pageid { get; set; }
    public string organizationid { get; set; }

    private RequestHeader()
    {
    }

    public static RequestHeader GetRequestHeader(string url, string pageid, string organizationid)
    {
        return new RequestHeader()
        {
            Username = "Some logic to fetch username",
            Password = "Some logic to fetch password",
            AccessKey = "Some access key",
            url = url,
            pageid = pageid,
            organizationid = organizationid,
        };
    }
}

Case 2)

public class HttpClientHelper
{
    public static HttpClient GetHttpClient(RequestHeader header)
    {
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        foreach (var property in header.GetType().GetProperties())
        {
            client.DefaultRequestHeaders.Add(property.Name, property.GetValue(header).ToString());
        }

        return client;
    }
}

I know that static is not used where state is maintained. I believe I am not maintaining any state here. I will be using this in a class library and I will be using these for calling a rest service.

The only thing which makes me want to use static here is not to initialize these class.(I know this is a very baaad reason).

Please let me know your thoughts. Is there something which I am not seeing in this.

Note: 1) I am aware of the small casing for some of the properties. It is in sync with the rest service on which I have absolutely no control. 2) If I have multiple RequestHeader in future, I might create an IRequestHeader which has a method GetRequestHeader. So the different RequestHeaders will implement this. In this case I know I cant keep a static method in interface. Please Keep these 2 conditions away and let me know your thoughts.

What you have here seems to be a version of the Static Factory Pattern . This is a well-known pattern and is perfectly fine to use.

You might also be interested in the non-static version of the Factory Pattern .

I assume HttpClient is not "your class", in which case you of course can't add a method inside the class itself.

The only thing which makes me want to use static here is not to initialize these class.(I know this is a very baaad reason).

Technically you're instantiating and initializing these classes no matter how you do it (factory method or no factory method), the only question is if you are going to use a factory method to do the instantiation and initialization for you.

If you have to use same values for each call you should use static fields, because static fields are used when only one copy of the variable is required. The same static field will share the copy across all the instances.

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