This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Post for a REST api | |
/// </summary> | |
/// <param name="token">authorization token</param> | |
/// <param name="url">REST url for the resource</param> | |
/// <param name="content">content</param> | |
/// <returns>response from the rest url</returns> | |
public async Task<JObject> PostAsync(string token, string url, string content) | |
{ | |
byte[] data = Encoding.UTF8.GetBytes(content); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try | |
{ | |
WebResponse response = await request.GetResponseAsync(); | |
using (StreamReader reader = new StreamReader(response.GetResponseStream())) | |
{ | |
string responseContent = reader.ReadToEnd(); | |
JObject adResponse = | |
Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(responseContent); | |
return adResponse; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
byte[] data = Encoding.UTF8.GetBytes(content); | |
WebRequest request = WebRequest.Create(url); | |
request.Method = "POST"; | |
request.ContentType = "application/json"; | |
request.Headers.Add("Authorization", "Bearer " + token); | |
request.ContentLength = data.Length; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ISchool | |
{ | |
public string Learn(string student); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HavardUniversity : ISchool | |
{ | |
public string Learn(string student) | |
{ | |
return string.Format($"My name is {student}, and I attend Havard"); | |
} | |
} | |
public class OxfordUniversity : ISchool | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var schoolInstance = new School(); | |
var oxford = new OxfordUniversity(); | |
//Inject dependency as method parameter | |
schoolInstance.AttendClass("Kenneth", oxford); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class School | |
{ | |
public string AttendClass(string student, ISchool school) | |
{ | |
return school.Learn(student); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class School | |
{ | |
private ISchool _school; | |
public ISchool school | |
{ | |
get | |
{ | |
if (_school == null) school = new HavardUniversity(); | |
return _school; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
School havard = new School(new HavardUniversity()); | |
School oxford = new School(new OxfordUniversity()); | |
var havardStudent = havard.AttendClass("Anthony"); | |
var oxfordStudent = oxford.AttendClass("Kenneth"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class School | |
{ | |
private readonly ISchool _school; | |
public School(ISchool school) | |
{ | |
this._school = school; | |
} | |
public string AttendClass(string student) |
OlderNewer