Skip to content

Instantly share code, notes, and snippets.

View MelbourneDeveloper's full-sized avatar
🏠
Working from home

Christian Findlay MelbourneDeveloper

🏠
Working from home
View GitHub Profile
using System.Net;
namespace Urls
{
public record QueryParameter
{
private string? fieldValue;
public string FieldName { get; init; }
public string? Value
/// <summary>
/// Dependency Injection abstraction for Rest Clients. Use the CreateClient delegate to create an IClient when more than one is needed for an application.
/// </summary>
public interface IClient
{
/// <summary>
/// Sends a strongly typed request to the server and waits for a strongly typed response
/// </summary>
/// <typeparam name="TResponseBody">The expected type of the response body</typeparam>
/// <param name="request">The request that will be translated to a HTTP request</param>
@MelbourneDeveloper
MelbourneDeveloper / DI.cs
Created May 26, 2021 21:52
ASP .NET Core Dependency Injection (RestClient.Net)
var serviceCollection = new ServiceCollection()
//Add a service which has an IClient dependency
.AddSingleton<IGetString, GetString1>()
//Add RestClient.Net with a default Base Url of http://www.test.com
.AddRestClient((o) => o.BaseUrl = "http://www.test.com".ToAbsoluteUrl());
//Use HttpClient dependency injection
_ = serviceCollection.AddHttpClient();
[<TestMethod>]
member this.TestComposition () =
let uri =
"host.com".ToHttpUrlFromHost(5000)
.AddQueryParameter("fieldname1", "field<>Value1")
.WithCredentials("username", "password")
.AddQueryParameter("FieldName2", "field<>Value2")
.WithFragment("frag")
.WithPath("pathpart1", "pathpart2")
var client = new Client(
new NewtonsoftSerializationAdapter(),
baseUrl: testServerBaseUri,
defaultRequestHeaders:
HeadersExtensions
.FromJsonContentType()
//Non-destructive mutation to create a new headers collection
.WithBearerTokenAuthentication(bearerToken));
@MelbourneDeveloper
MelbourneDeveloper / JsonValue.cs
Created June 24, 2021 07:44
Json Data Structure
using System.Collections.Immutable;
namespace JsonDataStructure
{
public record JsonValue
{
public ValueType ValueType { get; }
public string? StringValue { get; init; }
public bool? BooleanValue { get; init; }
@MelbourneDeveloper
MelbourneDeveloper / JsonExtensions.cs
Created June 24, 2021 08:32
Converts Json to a Json Object (ImmutableDictionary<string, JsonValue>)
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
#pragma warning disable format
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
#pragma warning disable CS8604 // Possible null reference argument.
@MelbourneDeveloper
MelbourneDeveloper / Example.cs
Last active July 29, 2021 07:42
Null Object Pattern Example
#nullable enable
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TestProject4
{
public interface IDependency { void DoSomething(); }
public class NullDependency : IDependency
{
@MelbourneDeveloper
MelbourneDeveloper / MultiTargeting.csproj
Created December 12, 2021 04:58
Multiple Target csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0, net45, netstandard2_0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
@MelbourneDeveloper
MelbourneDeveloper / CodeAnalysis.csproj
Created December 19, 2021 10:22
Code Analysis csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<AnalysisLevel>latest</AnalysisLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>