patterncsharpMinor
TravelAdvice class
Viewed 0 times
traveladviceclassstackoverflow
Problem
I have never really had to use Json.net until now, so I'm very rusty at it. For that reason, I would appreciate it if someone could have a look over my code and let me know if the way I have done it is correct. It works as I want it; I just need a more professional eye.
For the class
For the class
TravelAdvice, I've used http://json2csharp.com/.public class TravelAdvice
{
public string Id { get; set; }
public string WebUrl { get; set; }
public string Title { get; set; }
public string Format { get; set; }
public string UpdatedAt { get; set; }
public List Tags { get; set; }
public List Related { get; set; }
public Details3 Details { get; set; }
public List RelatedExternalLinks { get; set; }
public ResponseInfo ResponseInfo { get; set; }
}
public class FcoTravelAdvice : IFcoTravelAdvice
{
public IEnumerable GetFcoTravelAdvice(string country)
{
var url = "https://www.gov.uk/api/foreign-travel-advice/british-virgin-islands.json";
var syncClient = new WebClient();
var content = syncClient.DownloadString(url);
// JObject obj = JObject.Parse(content);
// JArray arr = (JArray)obj["fields"];
var fCoData = JsonConvert.DeserializeObject(content);
return new[] {fCoData};
//return test.Details.summary;
//return fCoData.ToString();
//DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(RootObject));
//using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(content)))
//{
//var data = (RootObject)s.ReadObject(ms);
//}
// return content;
}
}Solution
all examples I have seen used lots of code, inside the FcoTravelAdvice class I have used about 5 lines, so I was wondering if I have done it correctly.
If it works, you definitely have!
Still, I have a couple suggestions.
Don't make synchronous requests
It's 2014, and we don't write synchronous applications anymore. If your method if called from GUI app, the app will freeze until the request has completed. Imagine if user has a network problem—the app will just be unresponsive, and you don't seem to have provided any means to cancel the request.
Instead, you should use
Read about
Minor considerations
If it works, you definitely have!
Still, I have a couple suggestions.
Don't make synchronous requests
It's 2014, and we don't write synchronous applications anymore. If your method if called from GUI app, the app will freeze until the request has completed. Imagine if user has a network problem—the app will just be unresponsive, and you don't seem to have provided any means to cancel the request.
Instead, you should use
DownloadStringTaskAsync to provide asynchronous method:public class FcoTravelAdvice : IFcoTravelAdvice
{
public async Task GetFcoTravelAdvice(string country)
{
var url = "https://www.gov.uk/api/foreign-travel-advice/british-virgin-islands.json";
var client = new WebClient();
var content = await client.DownloadStringTaskAsync(url);
return JsonConvert.DeserializeObject(content);
}
}Read about
async if you haven't used it yet. It's life-changing.Minor considerations
- Why
fCoDataand notfcoData? The capitalization looks weird. Moreover, "whateverData" is often a bad kind of name. Why not call itadvice? After all, that's what you're deserialising.
- I would dump
FCOprefix from code altogether (TravelAdvice,GetTravelAdvice,advice). It doesn't make code more meaningful. Do you really plan to support travel advice not from FCO?
- Finally, why is your API always return single item, but the interface returns
IEnumerable? It's a mystery to me.
Code Snippets
public class FcoTravelAdvice : IFcoTravelAdvice
{
public async Task<TravelAdvice> GetFcoTravelAdvice(string country)
{
var url = "https://www.gov.uk/api/foreign-travel-advice/british-virgin-islands.json";
var client = new WebClient();
var content = await client.DownloadStringTaskAsync(url);
return JsonConvert.DeserializeObject<TravelAdvice>(content);
}
}Context
StackExchange Code Review Q#38464, answer score: 3
Revisions (0)
No revisions yet.