private object Resolve(Type dependencyType, ServiceImplementations implementationName = ServiceImplementations.None) { if (dependencyType.IsGenericType && (dependencyType.GetGenericTypeDefinition() == typeof(IEnumerable <>))) { return(GetAllImplementations(dependencyType.GetGenericArguments()[0])); } if (dependencyType.IsGenericType && dependencies.ContainsKey(dependencyType.GetGenericTypeDefinition())) { var implementationType = dependencies[dependencyType.GetGenericTypeDefinition()][0].implementationType; implementationType = implementationType.MakeGenericType(dependencyType.GetGenericArguments()); if (dependencyType.IsAssignableFrom(implementationType)) { return(CreateInstance(implementationType)); } } if (!dependencies.ContainsKey(dependencyType)) { return(null); } var implementationInfo = GetImplementationInfo(dependencyType, implementationName); if (!dependencyType.IsAssignableFrom(implementationInfo.implementationType)) { return(null); } if (implementationInfo.lifetime == Lifetime.Transient) { return(CreateInstance(implementationInfo.implementationType)); } if (implementationInfo.lifetime == Lifetime.Singleton) { return(Singleton.GetInstance(implementationInfo.implementationType, CreateInstance)); } return(null); }
private ImplementationInfo GetImplementationInfo(Type dependencyType, ServiceImplementations name) { var implementations = dependencies[dependencyType]; return(implementations.Where(info => info.implementationName == name).First()); }
public T Resolve <T>(ServiceImplementations implementationName = ServiceImplementations.None) where T : class { Type dependencyType = typeof(T); return((T)Resolve(dependencyType, implementationName)); }
public void Register(Type dependency, Type implementationType, Lifetime lifetime = Lifetime.Transient, ServiceImplementations implementation = ServiceImplementations.None) { var newImplInfo = new ImplementationInfo(implementationType, lifetime, implementation); if (!dependencies.ContainsKey(dependency)) { dependencies.Add(dependency, new List <ImplementationInfo>()); } dependencies[dependency].Add(newImplInfo); }
public void Register <TDependency, TImplementation>(Lifetime lifetime = Lifetime.Transient, ServiceImplementations implementation = ServiceImplementations.None) where TDependency : class where TImplementation : TDependency { var newImplInfo = new ImplementationInfo(typeof(TImplementation), lifetime, implementation); if (!dependencies.ContainsKey(typeof(TDependency))) { dependencies.Add(typeof(TDependency), new List <ImplementationInfo>()); } dependencies[typeof(TDependency)].Add(newImplInfo); }
public ImplementationInfo(Type implType, Lifetime lifetime, ServiceImplementations implementation) { this.lifetime = lifetime; implementationType = implType; implementationName = implementation; }
public DependencyKeyAttribute(ServiceImplementations name) { implementationName = name; }