2

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.

6
  • This might be better on codereview.stackexchange.com. It's not really a good fit for Stack Overflow since it isn't a specific programming or IDE software problem. Commented Apr 9, 2015 at 12:09
  • Is there anyway I can put this into stackexchange. Or do i have to create a new post there. Commented Apr 9, 2015 at 12:14
  • 1
    Hold on before moving it anywhere. I don't think this is really off-topic for Stack Overflow, so I see no reason in moving it. Commented Apr 9, 2015 at 12:15
  • @Simon Andre Forsberg Oh ok Commented Apr 9, 2015 at 12:16
  • 3
    They're factory methods; they work perfectly well as either static or non-static. Frankly, I'm of the opinion "be open to refactoring", in which case it doesn't matter whether you have made it static or non-static: if it turns out it might have been easier another way, you change it. As such, I'd default to static and worry about something more important. Commented Apr 9, 2015 at 12:18

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

you are right. HttpClient is from System.Net.Http. So seems to me that the factory pattern would be fine here. :)
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.