Starting way before the SOLID principles, you're code is very incoherent. You have some static BuildAPI
- so either decide on sticking to it or don't use it. Because it's static, this decision should be based on whether you suspect that in the future these constructions will need to be polymorphic, or not.
Second note: do you really need two constructors? Seems like only one is used.
Third note: You initialize some of the properties in constructor, and some with the property initializer - why not just stick to one paradigm (POD object, or a class)?
Finally, as for the SOLID principles - your RequestObject
clearly violates the Single Responsibility Principle which is evident from the fact, that you pass an enum
to the constructor. So this class represents in fact few classes which are fit together. Consider making RequestObject
abstract, and implement a separate sub-class for each request type.
Something like this:
abstract class RequestObject
{
//properties and methods common to all requests
protected RequestObject(...)
{
//initialize common properties
}
}
class GetRequest : RequestObject
{
public GetRequest()
{
this.Encoding = System.Text.Encoding.UTF8;
System.Net.ServicePointManager.Expect100Continue = false;
}
//... other specific things for GET
}
class PutRequest : RequestObject
{
public PutRequest()
{
this.AddPostContent(ContentArray);
this.AddHeader("Content-Type", "application/json");
}
//... specific things for PUT
}
... you get the picture. Probably you could make also another level between RequestObject
and Post
, Put
as they also probably share some behaviour.