public override IEnumerable<ZenjectResolveException> ValidateBinding(Type contractType, InjectContext context) { if (_creator.HasInstance()) { // This would be the case if given an instance at binding time with ToSingle(instance) return Enumerable.Empty<ZenjectResolveException>(); } return BindingValidator.ValidateObjectGraph(_container, GetInstanceType()); }
public override object GetInstance(Type contractType, InjectContext context) { if (_instantiator == null) { _instantiator = _container.Resolve<Instantiator>(); } var obj = _instantiator.Instantiate(GetTypeToInstantiate(contractType)); Assert.That(obj != null); return obj; }
public override object GetInstance(Type contractType, InjectContext context) { return _creator.GetInstance(contractType); }
public override IEnumerable<ZenjectResolveException> ValidateBinding(Type contractType, InjectContext context) { return BindingValidator.ValidateObjectGraph(_container, _concreteType); }
public bool Matches(InjectContext context) { // Identifier will be null most of the time return IdentifiersMatch(context.Identifier) && _condition(context); }
public override object GetInstance(Type contractType, InjectContext context) { // _instance == null during validation sometimes Assert.That(_instance == null || _instance.GetType().DerivesFromOrEqual(contractType)); return _instance; }
public override object GetInstance(Type contractType, InjectContext context) { return _container.Resolve(contractType, context); }
public static IEnumerable<ZenjectResolveException> ValidateContract( DiContainer container, Type contractType, InjectContext context) { return ValidateContract(container, contractType, context, false); }
public List <TContract> ResolveMany <TContract>(InjectContext context) { return((List <TContract>)ResolveMany(typeof(TContract), context)); }
// Walk the object graph for the given type // Returns all ZenjectResolveExceptions found public IEnumerable <ZenjectResolveException> ValidateResolve(Type contractType, InjectContext context) { return(BindingValidator.ValidateContract(this, contractType, context)); }
public IEnumerable <ZenjectResolveException> ValidateResolve <TContract>(InjectContext context) { return(ValidateResolve(typeof(TContract), context)); }
public override IEnumerable <ZenjectResolveException> ValidateBinding(Type contractType, InjectContext context) { return(BindingValidator.ValidateObjectGraph(_container, typeof(T))); }
public override object GetInstance(Type contractType, InjectContext context) { Assert.That(typeof(T).DerivesFromOrEqual(contractType)); return(Instantiator.Instantiate <T>(_template)); }
public abstract IEnumerable<ZenjectResolveException> ValidateBinding(Type contractType, InjectContext context);
public abstract object GetInstance(Type contractType, InjectContext context);
// Wrap IEnumerable<> to avoid LINQ mistakes internal List <ProviderBase> GetProviderMatches(Type contractType, InjectContext context) { return(GetProviderMatches(contractType, context, false)); }
public static IEnumerable<ZenjectResolveException> ValidateObjectGraph( DiContainer container, Type concreteType, params Type[] extras) { using (container.PushLookup(concreteType)) { var typeInfo = TypeAnalyzer.GetInfo(concreteType); var extrasList = extras.ToList(); foreach (var dependInfo in typeInfo.AllInjectables) { Assert.IsEqual(dependInfo.EnclosingType, concreteType); if (TryTakingFromExtras(dependInfo.ContractType, extrasList)) { continue; } var context = new InjectContext( dependInfo, DiContainer.LookupsInProgress.ToList(), null); foreach (var error in ValidateContract( container, dependInfo.ContractType, context, dependInfo.Optional)) { yield return error; } } if (!extrasList.IsEmpty()) { yield return new ZenjectResolveException( "Found unnecessary extra parameters passed when injecting into '{0}' with types '{1}'. \nObject graph:\n{2}" .With(concreteType.Name(), String.Join(",", extrasList.Select(x => x.Name()).ToArray()), DiContainer.GetCurrentObjectGraph())); } } }
// Wrap IEnumerable<> to avoid LINQ mistakes internal List <ProviderBase> GetProviderMatches( Type contractType, InjectContext context, bool soft) { return(GetProviderMatchesInternal(contractType, context, soft).ToList()); }
public static IEnumerable<ZenjectResolveException> ValidateContract( DiContainer container, Type contractType, InjectContext context, bool isOptional) { var matches = container.GetProviderMatches(contractType, context); if (matches.IsLength(1)) { foreach (var error in matches.Single().ValidateBinding(contractType, context)) { yield return error; } } else { if (ReflectionUtil.IsGenericList(contractType)) { var subType = contractType.GetGenericArguments().Single(); matches = container.GetProviderMatches(subType, context); if (matches.IsEmpty()) { if (!isOptional) { if (container.FallbackProvider != null) { foreach (var error in container.FallbackProvider.ValidateBinding(contractType, context)) { yield return error; } } else { yield return new ZenjectResolveException( "Could not find dependency with type 'List<{0}>'{1}. If the empty list is also valid, you can allow this by using the [InjectOptional] attribute.' \nObject graph:\n{2}" .With( subType.Name(), (context.EnclosingType == null ? "" : " when injecting into '{0}'".With(context.EnclosingType.Name())), DiContainer.GetCurrentObjectGraph())); } } } else { foreach (var match in matches) { foreach (var error in match.ValidateBinding(contractType, context)) { yield return error; } } } } else { if (!isOptional) { if (matches.IsEmpty()) { if (container.FallbackProvider != null) { foreach (var error in container.FallbackProvider.ValidateBinding(contractType, context)) { yield return error; } } else { yield return new ZenjectResolveException( "Could not find required dependency with type '{0}'{1} \nObject graph:\n{2}" .With( contractType.Name(), (context.EnclosingType == null ? "" : " when injecting into '{0}'".With(context.EnclosingType.Name())), DiContainer.GetCurrentObjectGraph())); } } else { yield return new ZenjectResolveException( "Found multiple matches when only one was expected for dependency with type '{0}'{1} \nObject graph:\n{2}" .With( contractType.Name(), (context.EnclosingType == null ? "" : " when injecting into '{0}'".With(context.EnclosingType.Name())), DiContainer.GetCurrentObjectGraph())); } } } } }
public object ResolveMany(Type contract, InjectContext context) { // Soft == false, always create new instances when possible return(ResolveMany(contract, context, false)); }
public override IEnumerable<ZenjectResolveException> ValidateBinding(Type contractType, InjectContext context) { return _container.ValidateResolve(contractType); }
public TContract Resolve <TContract>(InjectContext context) { return((TContract)Resolve(typeof(TContract), context)); }
public override IEnumerable<ZenjectResolveException> ValidateBinding(Type contractType, InjectContext context) { return Enumerable.Empty<ZenjectResolveException>(); }
// Be careful with this method since it is a coroutine IEnumerable <ProviderBase> GetProviderMatchesInternal( Type contractType, InjectContext context) { return(GetProvidersForContract(contractType).Where(x => x.Matches(context))); }