简体   繁体   English

我应该使用静态方法(C#)

[英]Should I use static method (C#)

Should I use static in the following 2 cases: 我应该在以下两种情况下使用静态:

Case 1) 情况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) 案例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. 注意:1)我知道一些属性的小套管。 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. 2)如果将来我有多个RequestHeader,我可能会创建一个具有GetRequestHeader方法的IRequestHeader。 So the different RequestHeaders will implement this. 所以不同的RequestHeaders将实现这一点。 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 . 您可能还对Factory Pattern的非静态版本感兴趣。

I assume HttpClient is not "your class", in which case you of course can't add a method inside the class itself. 我假设HttpClient不是“你的类”,在这种情况下你当然不能在类本身内添加一个方法。

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. 相同的静态字段将在所有实例之间共享副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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