Skip to content

Instantly share code, notes, and snippets.

View Chibuikekenneth's full-sized avatar
💥
Stabilizing my DSAS

Chibuike Kenneth Chibuikekenneth

💥
Stabilizing my DSAS
View GitHub Profile
var schoolInstance = new School();
var oxford = new OxfordUniversity();
//Inject dependency as method parameter
schoolInstance.AttendClass("Kenneth", oxford);
public class School
{
public string AttendClass(string student, ISchool school)
{
return school.Learn(student);
}
}
var schoolInstance = new School();
schoolInstance.AttendClass("Anthony");
var oxford = new OxfordUniversity();
//Injecting the dependency via a property, which is the School Property
schoolInstance.school = oxford;
schoolInstance.AttendClass("Kenneth");
public class School
{
private ISchool _school;
public ISchool school
{
get
{
if (_school == null) school = new HavardUniversity();
return _school;
School havard = new School(new HavardUniversity());
School oxford = new School(new OxfordUniversity());
var havardStudent = havard.AttendClass("Anthony");
var oxfordStudent = oxford.AttendClass("Kenneth");
public class HavardUniversity : ISchool
{
public string Learn(string student)
{
return string.Format($"My name is {student}, and I attend Havard");
}
}
public class OxfordUniversity : ISchool
{
public interface ISchool
{
public string Learn(string student);
}
public class School
{
private readonly ISchool _school;
public School(ISchool school)
{
this._school = school;
}
public string AttendClass(string student)
@Chibuikekenneth
Chibuikekenneth / webrequest-try-catch.cs
Last active October 29, 2020 14:54
webrequest-try-catch
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;
}
@Chibuikekenneth
Chibuikekenneth / webrequest.cs
Last active October 29, 2020 14:55
webRequest definitions
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;