コード例 #1
0
        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());
        }
コード例 #2
0
ファイル: TransientProvider.cs プロジェクト: nintorii/Zenject
        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;
        }
コード例 #3
0
 public override object GetInstance(Type contractType, InjectContext context)
 {
     return _creator.GetInstance(contractType);
 }
コード例 #4
0
ファイル: TransientProvider.cs プロジェクト: nintorii/Zenject
 public override IEnumerable<ZenjectResolveException> ValidateBinding(Type contractType, InjectContext context)
 {
     return BindingValidator.ValidateObjectGraph(_container, _concreteType);
 }
コード例 #5
0
ファイル: ProviderBase.cs プロジェクト: Zerophase/DyMProject
 public bool Matches(InjectContext context)
 {
     // Identifier will be null most of the time
         return IdentifiersMatch(context.Identifier) && _condition(context);
 }
コード例 #6
0
ファイル: InstanceProvider.cs プロジェクト: nintorii/Zenject
 public override object GetInstance(Type contractType, InjectContext context)
 {
     // _instance == null during validation sometimes
     Assert.That(_instance == null || _instance.GetType().DerivesFromOrEqual(contractType));
     return _instance;
 }
コード例 #7
0
 public override object GetInstance(Type contractType, InjectContext context)
 {
     return _container.Resolve(contractType, context);
 }
コード例 #8
0
ファイル: BindingValidator.cs プロジェクト: nintorii/Zenject
 public static IEnumerable<ZenjectResolveException> ValidateContract(
     DiContainer container, Type contractType, InjectContext context)
 {
     return ValidateContract(container, contractType, context, false);
 }
コード例 #9
0
 public List <TContract> ResolveMany <TContract>(InjectContext context)
 {
     return((List <TContract>)ResolveMany(typeof(TContract), context));
 }
コード例 #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));
 }
コード例 #11
0
 public IEnumerable <ZenjectResolveException> ValidateResolve <TContract>(InjectContext context)
 {
     return(ValidateResolve(typeof(TContract), context));
 }
コード例 #12
0
 public override IEnumerable <ZenjectResolveException> ValidateBinding(Type contractType, InjectContext context)
 {
     return(BindingValidator.ValidateObjectGraph(_container, typeof(T)));
 }
コード例 #13
0
 public override object GetInstance(Type contractType, InjectContext context)
 {
     Assert.That(typeof(T).DerivesFromOrEqual(contractType));
     return(Instantiator.Instantiate <T>(_template));
 }
コード例 #14
0
ファイル: ProviderBase.cs プロジェクト: Zerophase/DyMProject
 public abstract IEnumerable<ZenjectResolveException> ValidateBinding(Type contractType, InjectContext context);
コード例 #15
0
ファイル: ProviderBase.cs プロジェクト: Zerophase/DyMProject
 public abstract object GetInstance(Type contractType, InjectContext context);
コード例 #16
0
 // Wrap IEnumerable<> to avoid LINQ mistakes
 internal List <ProviderBase> GetProviderMatches(Type contractType, InjectContext context)
 {
     return(GetProviderMatches(contractType, context, false));
 }
コード例 #17
0
ファイル: BindingValidator.cs プロジェクト: nintorii/Zenject
        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()));
                }
            }
        }
コード例 #18
0
 // Wrap IEnumerable<> to avoid LINQ mistakes
 internal List <ProviderBase> GetProviderMatches(
     Type contractType, InjectContext context, bool soft)
 {
     return(GetProviderMatchesInternal(contractType, context, soft).ToList());
 }
コード例 #19
0
ファイル: BindingValidator.cs プロジェクト: nintorii/Zenject
        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()));
                        }
                    }
                }
            }
        }
コード例 #20
0
 public object ResolveMany(Type contract, InjectContext context)
 {
     // Soft == false, always create new instances when possible
     return(ResolveMany(contract, context, false));
 }
コード例 #21
0
 public override IEnumerable<ZenjectResolveException> ValidateBinding(Type contractType, InjectContext context)
 {
     return _container.ValidateResolve(contractType);
 }
コード例 #22
0
 public TContract Resolve <TContract>(InjectContext context)
 {
     return((TContract)Resolve(typeof(TContract), context));
 }
コード例 #23
0
ファイル: InstanceProvider.cs プロジェクト: nintorii/Zenject
 public override IEnumerable<ZenjectResolveException> ValidateBinding(Type contractType, InjectContext context)
 {
     return Enumerable.Empty<ZenjectResolveException>();
 }
コード例 #24
0
ファイル: DiContainer.cs プロジェクト: harjup/WizardCombat
 // 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)));
 }