public IRegistration LookupType(Query targetType, ResolverContext context) { IRegistration registration = underlying.LookupType(targetType, context); if (registration != null) { return registration; } Type type = targetType.ServiceType; if (!type.IsInterface && !type.IsAbstract && targetType.Name == null) { lock (createdRegistrations) { if (createdRegistrations.ContainsKey(type)) return createdRegistrations[type]; var reg = new Registration { Service = targetType.ServiceType, Implementor = targetType.ServiceType, Lifestyle = lifestyle }; createdRegistrations.Add(type, reg); return reg; } } return null; }
public virtual object CreateType(Query query, ResolverContext context) { //Need to create a deep copy of the Context to make splitting the graph possible var localContext = ResolverContext.CreateContextFromContext(context); var registration = FindSuitableImplementor(query, localContext); var retVal = registration.Lifestyle.Execute(() => { var creationContext = GenerateCreationContext(registration, localContext); return activator.CreateInstance(creationContext); }); return retVal; }
public virtual IRegistration LookupType(Query query, ResolverContext context) { IList<IRegistration> localParents = new List<IRegistration>(context.UsedRegistrations); var registrations = componentStore.GetRegistrationsForService(query.ServiceType); if (query.Name != null) { var @default = registrations.SingleOrDefault(p => p.Name == query.Name); if (@default == null) throw new ServiceNotFoundException(query.ServiceType, query.Name); return @default; } var registration = SkipParents(registrations, localParents); if (registration == null) return null; context.ConsumeRegistration(registration); return registration; }
public virtual CreationContext GenerateCreationContext(IRegistration registration, ResolverContext context) { var componentType = registration.Implementor; var constructors = componentType.GetConstructors() .OrderByDescending(p => p.GetParameters().Count()); IList<DependencyMissingException> missingDependencies = new List<DependencyMissingException>(); foreach (var info in constructors) { missingDependencies = new List<DependencyMissingException>(); var parameters = info.GetParameters(); if (parameters.Length == 0) //Fast way out. return CreateReturnContext(componentType, null); IList<object> resolvedParameters = new List<object>(); foreach (var parameter in parameters) { string explicitDependencyName = null; var parameterName = parameter.Name; if (registration.Parameters.ContainsKey(parameterName)) explicitDependencyName = registration.Parameters[parameterName]; Type type = parameter.ParameterType; try { var query = new Query {ServiceType = type, Name = explicitDependencyName}; resolvedParameters.Add(CreateType(query, context)); } catch (ServiceNotFoundException exception) { missingDependencies.Add(new DependencyMissingException(exception.Message, registration.Service.Name)); } } if (resolvedParameters.Count == parameters.Length) return CreateReturnContext(componentType, resolvedParameters.ToArray()); } throw missingDependencies.First(); //TODO: Expose all missing dependencies }
public virtual object CreateType(Query query) { return CreateType(query, new ResolverContext()); }
private IRegistration FindSuitableImplementor(Query query, ResolverContext context) { var suitableImplementor = componentLookup.LookupType(query, context); if (suitableImplementor == null) throw new ServiceNotFoundException(query.ServiceType); return suitableImplementor; }