Skip to content

Instantly share code, notes, and snippets.

Avatar
💥
Stabilizing my DSAS

Chibuike Kenneth Chibuikekenneth

💥
Stabilizing my DSAS
View GitHub Profile
View methodInjectionS.cs
var schoolInstance = new School();
var oxford = new OxfordUniversity();
//Inject dependency as method parameter
schoolInstance.AttendClass("Kenneth", oxford);
View methodInjection.cs
public class School
{
public string AttendClass(string student, ISchool school)
{
return school.Learn(student);
}
}
View injectProperty.cs
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");
View propertyinjectionD.cs
public class School
{
private ISchool _school;
public ISchool school
{
get
{
if (_school == null) school = new HavardUniversity();
return _school;
View diAction.cs
School havard = new School(new HavardUniversity());
School oxford = new School(new OxfordUniversity());
var havardStudent = havard.AttendClass("Anthony");
var oxfordStudent = oxford.AttendClass("Kenneth");
View universities.cs
public class HavardUniversity : ISchool
{
public string Learn(string student)
{
return string.Format($"My name is {student}, and I attend Havard");
}
}
public class OxfordUniversity : ISchool
{
View constructorInjectionInterface.cs
public interface ISchool
{
public string Learn(string student);
}
View constructorInjectionClass.cs
public class School
{
private readonly ISchool _school;
public School(ISchool school)
{
this._school = school;
}
public string AttendClass(string student)
View webrequest-try-catch.cs
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 Oct 29, 2020
webRequest definitions
View webrequest.cs
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;