Background
To improve my understanding of IOC and how to use it, I want to create an example of all three IOC methods: constructor injection, Setter injection and interface injection without using a third-party structure. I think I have a basic example of a constructor injection, but it struggles with installation and interface.
My question
How do you approach the solution to the problem of entering the input and installation interface from scratch?
Here are my thoughts, let me know if I'm on the right track.
Interface Insert:
- Scrolling through allowed objects created using constructor installation, check which interfaces are implemented in interfaceDependencyMap
- Define some interface interfaceDependencyMap to associate the interface with the implementation.
- Solve an implementation using interfaceDependencyMap
- Assign the appropriate property to the object initialized by the constructor installation
Setter Entry:
- Loop through allowed objects created using constructor installation
- Define some kind of setterInjectionMap
- Allow expected parameter from MethodInfo using constructor mappings
- Call the setter method passing in the allowed parameter object
Here is what I have so far used to install the constructor
public class Program
{
static void Main(string[] args)
{
var resolver = new Resolver();
resolver.Register<Customer, Customer>();
resolver.Register<ICreditCard, Visa>();
var customer = resolver.Resolve<Customer>();
customer.Charge();
}
}
public interface ICreditCard
{
string Charge();
}
public class Visa : ICreditCard
{
public string Charge()
{
return "Charging Visa";
}
}
public class MasterCard : ICreditCard
{
public string Charge()
{
return "Charging MasterCard";
}
}
public class Customer
{
private readonly ICreditCard _creditCard;
public Customer(ICreditCard creditCard)
{
this._creditCard = creditCard;
}
public void Charge()
{
_creditCard.Charge();
}
}
public class Resolver
{
private Dictionary<Type, Type> dependencyMap = new Dictionary<Type, Type>();
public T Resolve<T>()
{
return (T) Resolve(typeof (T));
}
private object Resolve(Type typeToResolve)
{
Type resolvedType = null;
try
{
resolvedType = dependencyMap[typeToResolve];
}
catch
{
throw new Exception(string.Format("could not resolve type {0}", typeToResolve.FullName));
}
var firstConstructor = resolvedType.GetConstructors().First();
var constructorParameters = firstConstructor.GetParameters();
if (constructorParameters.Count() == 0)
return Activator.CreateInstance(resolvedType);
IList<object> parameters = constructorParameters.Select(parameterToResolve => Resolve(parameterToResolve.ParameterType)).ToList();
return firstConstructor.Invoke(parameters.ToArray());
}
public void Register<TFrom, TTo>()
{
dependencyMap.Add(typeof (TFrom), typeof (TTo));
}
}
source
share