Skip to content

Instantly share code, notes, and snippets.

@fcmendoza
Last active February 28, 2016 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fcmendoza/f5672dfd70db97f31f11 to your computer and use it in GitHub Desktop.
Save fcmendoza/f5672dfd70db97f31f11 to your computer and use it in GitHub Desktop.
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();
}
}
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();
}
}
/*
* 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