Created
April 7, 2017 23:23
-
-
Save hudsonmendes/b9d281c7e77f4b34c927250abe363081 to your computer and use it in GitHub Desktop.
O Injetor de Dependencia do Felipe, dum jeitinho mais meu ;)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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