I'm learning C# as a web developer rather than a C# developer learning web development. I've gotten the hang of creating models and controllers (or so I think) for a web project, and I've read some tutorials on general C# and I'm just putting it all together. I'd like to know how I can best refactor this code.
Let's say I have the following model
// Models for Lite Requests
public class RequestLite
{
public string action { get; set; }
public string action2 { get; set; }
public string action3 { get; set; }
public ParametersLite parameters { get; set; }
}
public class RequestHeavy
{
public string action { get; set; }
public string action2 { get; set; }
public string action3 { get; set; }
public ParametersHeavy parameters { get; set; }
}
// Models for Heavy Requests
public class ParametersLite
{
public List<string> parameter { get; set; }
}
public class ParametersHeavy
{
public List<string> parameter { get; set; }
public string membership { get; set; }
public string featured { get; set; }
}
RequestLite and RequestHeavy are pretty much the same except for the fact that RequestHeavy has some additional parameters. The remote service can only accept one specific kind of data request set per query. If I were to send a RequestHeavy model with membership and featured set as null to a Query that only accepts RequestLite's model, it'd break.
Question: what's the best way to refactor these models? It's redundant to have ParametersLite and a ParametersHeavy if the only difference is two additional parameters. How can I just have one type of Object where I can have it send those parameters only when needed?
Here's the rest of the code
public class HomeController : Controller
{
public string endpoint = "http://generic.api.com:12345/query";
public string ConnectToEndpoint(string data)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(endpoint);
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
var result = "";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(data);
streamWriter.Flush();
streamWriter.Close();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
}
return result;
}
[HttpGet]
public ActionResult Index()
{
var ReqLite = new RequestLite {
action = "GetUsers",
action2 = "xyz",
action3 = "zyx",
parameters = new ParametersLite {
parameter = new List<string> {
"female", "20+", "single"
}
}
};
var ReqHeavy = new RequestHeavy() {
action = "GetSilverFeaturedUsers",
action2 = "xyz",
action3 = "zyx",
parameters = new ParametersHeavy {
parameter = new List<string> {
"female", "20+", "single"
},
membership = "silver",
featured = "yes"
},
};
// assign both responses to a view model object
return View();
}
}
I've been doing all from one controller as this was the best way to learn what I needed to. Now I want to clean it up a bit
Questions: The ConnectToEndpoint method along with the endpoint string, for the purpose of just making a web request and returning a response, where should I place this so all other controllers can use it? Should I make it a reference instead, rather than say putting it on a base controller? What should I change to make it even more generic so that it can be used to connect to other endpoints that take an object or two? The ReqLite and ReqHeavy objects, since both requests share the same values, action2, action3 and parameter, what's a better way to create something more generic and reusable with those values already populated?
The ideal approach would be to have many reusable methods and classes outside of the project from an mvc perspective but easily accessible to the project and also portability of these reusable methods and classes to other solutions as well.