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());
        }
Example #2
0
        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);
 }
Example #4
0
 public override IEnumerable<ZenjectResolveException> ValidateBinding(Type contractType, InjectContext context)
 {
     return BindingValidator.ValidateObjectGraph(_container, _concreteType);
 }
Example #5
0
 public bool Matches(InjectContext context)
 {
     // Identifier will be null most of the time
         return IdentifiersMatch(context.Identifier) && _condition(context);
 }
Example #6
0
 public override object GetInstance(Type contractType, InjectContext context)
 {
     // _instance == null during validation sometimes
     Assert.That(_instance == null || _instance.GetType().DerivesFromOrEqual(contractType));
     return _instance;
 }
Example #7
0
 public override object GetInstance(Type contractType, InjectContext context)
 {
     return _container.Resolve(contractType, context);
 }
Example #8
0
 public static IEnumerable<ZenjectResolveException> ValidateContract(
     DiContainer container, Type contractType, InjectContext context)
 {
     return ValidateContract(container, contractType, context, false);
 }
Example #9
0
 public List <TContract> ResolveMany <TContract>(InjectContext context)
 {
     return((List <TContract>)ResolveMany(typeof(TContract), context));
 }
Example #10
0
 // 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));
 }
Example #11
0
 public IEnumerable <ZenjectResolveException> ValidateResolve <TContract>(InjectContext context)
 {
     return(ValidateResolve(typeof(TContract), context));
 }
Example #12
0
 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));
 }
Example #14
0
 public abstract IEnumerable<ZenjectResolveException> ValidateBinding(Type contractType, InjectContext context);
Example #15
0
 public abstract object GetInstance(Type contractType, InjectContext context);
Example #16
0
 // Wrap IEnumerable<> to avoid LINQ mistakes
 internal List <ProviderBase> GetProviderMatches(Type contractType, InjectContext context)
 {
     return(GetProviderMatches(contractType, context, false));
 }
Example #17
0
        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()));
                }
            }
        }
Example #18
0
 // Wrap IEnumerable<> to avoid LINQ mistakes
 internal List <ProviderBase> GetProviderMatches(
     Type contractType, InjectContext context, bool soft)
 {
     return(GetProviderMatchesInternal(contractType, context, soft).ToList());
 }
Example #19
0
        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()));
                        }
                    }
                }
            }
        }
Example #20
0
 public object ResolveMany(Type contract, InjectContext context)
 {
     // Soft == false, always create new instances when possible
     return(ResolveMany(contract, context, false));
 }
Example #21
0
 public override IEnumerable<ZenjectResolveException> ValidateBinding(Type contractType, InjectContext context)
 {
     return _container.ValidateResolve(contractType);
 }
Example #22
0
 public TContract Resolve <TContract>(InjectContext context)
 {
     return((TContract)Resolve(typeof(TContract), context));
 }
Example #23
0
 public override IEnumerable<ZenjectResolveException> ValidateBinding(Type contractType, InjectContext context)
 {
     return Enumerable.Empty<ZenjectResolveException>();
 }
Example #24
0
 // 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)));
 }