Skip to content

Instantly share code, notes, and snippets.

View PoisonousJohn's full-sized avatar

Ivan Fateev PoisonousJohn

View GitHub Profile
@PoisonousJohn
PoisonousJohn / incorrect_dependency_injection.cs
Created February 21, 2018 09:28
Incorrect dependency injection
public class APIService : IAPIService
{
private readonly Lazy<ILogger> _logger;
public APIService()
{
_logger = Services.log;
}
public string UserId { get; private set; }
@PoisonousJohn
PoisonousJohn / cross_dependency.cs
Last active February 21, 2018 08:28
Solving cross-dependency with Lazy
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class Services
{
public static Lazy<ILogger> log = new Lazy<ILogger>(() =>
{
return new UserIdUnityLogger(api);
});
@PoisonousJohn
PoisonousJohn / Lazy.cs
Last active February 20, 2018 18:00
Reference Lazy<T> implementation for Unity
using System;
public class Lazy<T> {
public Lazy(Func<T> factory)
{
if (factory == null)
{
throw new ArgumentNullException("Lazy<T> doesn't accept null factory");
}
using System;
public static class Services
{
public static IAPIService api { get { return _api.Value; } }
public static bool isOffline { get; set; }
private static Lazy<IAPIService> _api = new Lazy<IAPIService>(() => isOffline
? (IAPIService)new OfflineAPIService()
: new APIService());
@PoisonousJohn
PoisonousJohn / Service-offline-emulation-virtual.cs
Created February 20, 2018 16:34
Offline emulation using virtual methods
public class APIService {
public virtual string UserId {
get {
return _userId;
}
}
public virtual void Login()
{
// send request to server here
@PoisonousJohn
PoisonousJohn / Service-offline-emulation.cs
Last active February 20, 2018 16:39
Offline emulation mode per Service's method
public static class Services
{
public static bool isOffline
{
get; set;
}
}
public class APIService
{
@PoisonousJohn
PoisonousJohn / Services.cs
Last active February 20, 2018 16:03
Example of static-class locator anti-pattern
public static class Services
{
public static AdService ads { get; set; }
public static APIService api { get; set; }
// tons of other services
public static AnalyticsService analytics { get; set; }
}
public class Loader : MonoBehaviour {
private void Awake()
@PoisonousJohn
PoisonousJohn / Coupling.cs
Created February 20, 2018 14:27
Coupling sample
using System;
public class Logger
{
public void Info(string msg)
{
Console.WriteLine(msg);
}
}
@PoisonousJohn
PoisonousJohn / Sample.cs
Created February 20, 2018 11:49
Code sample
using System.Console;
public class Test
{
public Test()
{
Console.WriteLine("Let's test it");
}
}
@PoisonousJohn
PoisonousJohn / function.json
Created December 15, 2017 13:43
Template for publishing precompiled azure function
{
"generatedBy": "Microsoft.NET.Sdk.Functions-1.0.0.0",
"configurationSource": "attributes",
"bindings": [
{
"type": "eventHubTrigger",
"path": "default",
"connection": "EventHub",
"name": "myEventHubMessage"
}