Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Christian Findlay MelbourneDeveloper

🏠
Working from home
View GitHub Profile
@MelbourneDeveloper
MelbourneDeveloper / LogCheckExtensions.cs
Last active Apr 10, 2021
ILogger Verification with Moq
View LogCheckExtensions.cs
using Microsoft.Extensions.Logging;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
// More detailed samples here:
// https://github.com/MelbourneDeveloper/RestClient.Net/blob/79cca66e02f83a1043c44f215d374139f40f8c12/src/RestClient.Net.UnitTests/UnitTests.cs#L758
@MelbourneDeveloper
MelbourneDeveloper / Observer.cs
Created Feb 21, 2021
Observer with Null Object Pattern
View Observer.cs
internal class Observer<T> : IObserver<T>
{
static Action<Exception> onError = ((e) => { });
static Action onCompleted = (() => { });
private readonly Action<T> _onNext;
private readonly Action<Exception> _onError;
private readonly Action _onCompleted;
public Observer(
@MelbourneDeveloper
MelbourneDeveloper / Program.cs
Last active Feb 14, 2021
Null Object Example
View Program.cs
#nullable enable
namespace NullObjectExample
{
class Program
{
static void Main()
{
var someService = new SomeService();
}
View NullGuardExample.cs
#nullable enable
using System;
namespace Sample
{
public class NullGuardExample
{
public string NotNullableProperty { get; }
public string? NullableProperty { get; }
@MelbourneDeveloper
MelbourneDeveloper / Program.cs
Created Feb 13, 2021
Nullable Reference Types Minimal Console App Sample
View Program.cs
#nullable enable
using Sample;
using System;
var testRecord = new TestRecord("Hi", null);
Console.WriteLine(testRecord.NotNullableProperty);
namespace Sample
{
@MelbourneDeveloper
MelbourneDeveloper / PersonController.cs
Last active Feb 7, 2021
Smallest Controller Saple
View PersonController.cs
using Microsoft.AspNetCore.Mvc;
namespace WebApplication1.Controllers
{
[ApiController]
[Route("[controller]")]
public class PersonController : ControllerBase
{
[HttpGet]
public string Get(string firstName, string lastName) => $"{firstName} {lastName}";
View temperature.cs
private static async Task DisplayTemperature()
{
//Connect to the device by product id and vendor id
var temperDevice = await new FilterDeviceDefinition(vendorId: 0x413d, productId: 0x2107, usagePage: 65280)
.CreateWindowsHidDeviceFactory(_loggerFactory)
.ConnectFirstAsync()
.ConfigureAwait(false);
//Create the observable
var observable = Observable
@MelbourneDeveloper
MelbourneDeveloper / retry.cs
Created Jan 31, 2021
Perform Control Transfer With Retry
View retry.cs
public static Task<T> PerformControlTransferWithRetry<T>(
this IUsbDevice usbDevice,
Func<IUsbDevice, Task<T>> func,
int retryCount = 3,
int sleepDurationMilliseconds = 250)
{
var retryPolicy = Policy
.Handle<ApiException>()
.Or<ControlTransferException>()
.WaitAndRetryAsync(
View timeout.cs
var cancellationTokenSource = new CancellationTokenSource();
//Fire a timeout after 1000 milliseconds
cancellationTokenSource.CancelAfter(1000);
var result = await device.WriteAndReadAsync(writeData, cancellationTokenSource.Token);
@MelbourneDeveloper
MelbourneDeveloper / ClearStatus.cs
Created Jan 31, 2021
Control Transfer Clear Status
View ClearStatus.cs
public static Task ClearStatusAsync(this IUsbDevice usbDevice)
=> usbDevice.PerformControlTransferAsync(new SetupPacket
(
requestType: new UsbDeviceRequestType(
RequestDirection.In,
RequestType.Class,
RequestRecipient.Interface),
request: DFU_CLEARSTATUS,
length: 0
));