Skip to content

Instantly share code, notes, and snippets.

@hudsonmendes
Created April 7, 2017 23:23
Show Gist options
  • Save hudsonmendes/b9d281c7e77f4b34c927250abe363081 to your computer and use it in GitHub Desktop.
Save hudsonmendes/b9d281c7e77f4b34c927250abe363081 to your computer and use it in GitHub Desktop.
O Injetor de Dependencia do Felipe, dum jeitinho mais meu ;)
using System;
using System.Threading;
namespace Magicas {
public static class IoC<TInterface> {
private static ReaderWriterLockSlim syncLock = new ReaderWriterLockSlim();
private static TInterface singleton;
public static TInterface Singleton => NewOrGetSingleton();
public static Func<TInterface> Factory { get; set; }
public static TInterface New() {
if (Factory == null) throw new Exception("The 'Bind' is not setup yet.");
return Factory();
}
private static TInterface NewOrGetSingleton()
{
if (Singleton != null) return Singleton;
syncLock.EnterWriteLock();
try { singleton = Factory(); }
finally { syncLock.ExitWriteLock(); }
return singleton;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment