public static T Get <T>(this IResolutionRoot resolutionRoot) { var service = typeof(T); var provider = resolutionRoot.ResolveProvider <T>(); var request = new ResolveRequest(resolutionRoot.Root, service); return((T)provider.ResolveValue(request)); }
public static void ResolveSingletons(this IResolutionRoot resolutionRoot) { var bindingsToResolve = resolutionRoot.Bindings.Where(binding => binding.Configuration.Provider is SingletoneBindingProvider); var toResolve = bindingsToResolve.ToList(); if (!toResolve.Any()) { return; } foreach (var binding in toResolve) { var request = new ResolveRequest(resolutionRoot.Root, binding.Service); binding.Configuration.Provider.ResolveValue(request); } }
public override object ResolveValue(IResolveRequest request) { if (!Contracts.Contains(request.Service)) { return(null); } var constructors = Implementation.GetConstructors(BindingFlags.Instance | BindingFlags.Public); var constructor = constructors.OrderBy(ct => ct.GetParameters().Length).First(); var constructorParams = constructor.GetParameters().ToDictionary(pr => pr.Name, pr => pr.ParameterType); var bindings = request.Root.RootBindings; if (!constructorParams.Any()) { return(constructor.Invoke(null)); } var parameters = new Dictionary <string, object>(); foreach (var param in constructorParams) { if (bindings.All(bi => bi.Service != param.Value)) { throw new MissingConstructorParamException(request.Service, param.Key); } if (param.Value == request.Service) { throw new CyclicDependencyException(param.Value, request.Service); } var binding = bindings.First(bi => bi.Service == param.Value); var resolutionProvider = binding.Configuration.Provider; var resolutionRequest = new ResolveRequest(request.Root, param.Value); parameters.Add(param.Key, resolutionProvider.ResolveValue(resolutionRequest)); } var instance = constructor.Invoke(parameters.Values.ToArray()); return(instance); }