Last active
February 28, 2016 21:16
-
-
Save fcmendoza/f5672dfd70db97f31f11 to your computer and use it in GitHub Desktop.
Poor man's IoC containers. From: https://ayende.com/blog/2886/building-an-ioc-container-in-15-lines-of-code and: http://kenegozi.com/blog/2008/01/17/its-my-turn-to-build-an-ioc-container-in-15-minutes-and-33-lines/
This file contains 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
public class PoorContainer { | |
public void Register<T>(T t) { | |
types.Add(typeof(T), t); | |
} | |
public T Resolve<T>() { | |
return (T)types[typeof(T)]; | |
} | |
private readonly Dictionary<Type, object> types = new Dictionary<Type, object>(); | |
} | |
/* | |
Test container | |
*/ | |
public class ContainerTester { | |
public void TestContainer() { | |
/* | |
* | |
*/ | |
var container = new PoorContainer(); | |
container.Register<ITransactionRepository>(new TransactionRepository()); | |
container.Register<IClient>(new TravelClient(container.Resolve<ITransactionRepository>())); | |
var client = container.Resolve<IClient>(); | |
client.Run(); | |
} | |
} |
This file contains 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
public static class IoC { | |
static readonly IDictionary<Type, Type> types = new Dictionary<Type, Type>(); | |
public static void Register<TContract, TImplementation>() { | |
types[typeof(TContract)] = typeof(TImplementation); | |
} | |
public static T Resolve<T>() { | |
return (T)Resolve(typeof(T)); | |
} | |
public static object Resolve(Type contract) { | |
Type implementation = types[contract]; | |
ConstructorInfo constructor = implementation.GetConstructors()[0]; | |
ParameterInfo[] constructorParameters = constructor.GetParameters(); | |
if (constructorParameters.Length == 0) | |
return Activator.CreateInstance(implementation); | |
List<object> parameters = new List<object>(constructorParameters.Length); | |
foreach (ParameterInfo parameterInfo in constructorParameters) | |
parameters.Add(Resolve(parameterInfo.ParameterType)); | |
return constructor.Invoke(parameters.ToArray()); | |
} | |
} | |
public class ContainerTester { | |
public void TestContainer() { | |
/* | |
* | |
*/ | |
IoC.Register<IFileSystemAdapter, FileSystemAdapter>(); | |
IoC.Register<IBuildDirectoryStructureService, BuildDirectoryStructureService>(); | |
var service = IoC.Resolve<IBuildDirectoryStructureService>(); | |
service.Run(); | |
IoC.Register<ITransactionRepository, TransactionRepository>(); | |
IoC.Register<IClient, TravelClient>(); | |
var clientIoc = IoC.Resolve<IClient>(); | |
clientIoc.Run(); | |
} | |
} |
This file contains 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
/* | |
* Interfaces | |
*/ | |
public interface ITransactionRepository { | |
void SaveTransactions(IEnumerable<Transaction> transactions); | |
IEnumerable<Transaction> GetTransactions(); | |
} | |
public interface IClient { | |
void Run(); | |
} | |
/* | |
* Concrete implementations | |
*/ | |
public class TransactionRepository : ITransactionRepository { | |
public void SaveTransactions(IEnumerable<Transaction> transactions) { | |
// Done. | |
} | |
public IEnumerable<Transaction> GetTransactions() { | |
var transactions = new List<Transaction>(); | |
return transactions; | |
} | |
} | |
public class TravelClient : IClient { | |
public TravelClient(ITransactionRepository repo) { | |
_repo = repo; | |
} | |
public void Run() { | |
var transactions = _repo.GetTransactions(); | |
Console.WriteLine("Transactions retrieved: {0}.", transactions != null ? transactions.Count() : 0); | |
} | |
ITransactionRepository _repo; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment