Exemplo n.º 1
0
        public GameObjectNameGroupNameBinder ByPrefab(Type installerType, GameObject prefab)
        {
            BindingUtil.AssertIsValidPrefab(prefab);
            BindingUtil.AssertIsIInstallerType(installerType);

            var gameObjectInfo = new GameObjectBindInfo();

            SubFinalizer = CreateFinalizer(
                (container) => new SubContainerDependencyProvider(
                    ContractType, SubIdentifier,
                    new SubContainerCreatorByPrefabWithParams(
                        installerType,
                        container,
                        new PrefabProvider(prefab),
                        gameObjectInfo.Name,
                        gameObjectInfo.GroupName)));

            return(new GameObjectNameGroupNameBinder(BindInfo, gameObjectInfo));
        }
        public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabResourceInstaller(
            string resourcePath, Type installerType)
        {
            BindingUtil.AssertIsValidResourcePath(resourcePath);
            Assert.That(installerType.DerivesFrom <InstallerBase>(),
                        "Invalid installer type given during bind command.  Expected type '{0}' to derive from 'Installer<>'", installerType);

            var gameObjectInfo = new GameObjectCreationParameters();

            ProviderFunc =
                (container) => new SubContainerDependencyProvider(
                    ContractType, SubIdentifier,
                    new SubContainerCreatorByNewPrefabInstaller(
                        container,
                        new PrefabProviderResource(resourcePath),
                        gameObjectInfo, installerType, BindInfo.Arguments), false);

            return(new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo));
        }
Exemplo n.º 3
0
        public GameObjectNameGroupNameBinder ByPrefabResource(
            Type installerType, string resourcePath)
        {
            BindingUtil.AssertIsValidResourcePath(resourcePath);

            var gameObjectInfo = new GameObjectBindInfo();

            SubFinalizer = CreateFinalizer(
                (container) => new SubContainerDependencyProvider(
                    ContractType, SubIdentifier,
                    new SubContainerCreatorByPrefabWithParams(
                        installerType,
                        container,
                        new PrefabProviderResource(resourcePath),
                        gameObjectInfo.Name,
                        gameObjectInfo.GroupName)));

            return(new GameObjectNameGroupNameBinder(BindInfo, gameObjectInfo));
        }
Exemplo n.º 4
0
        public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder FromNewComponentOnNewPrefabResource(string resourcePath)
        {
            BindingUtil.AssertIsValidResourcePath(resourcePath);
            BindingUtil.AssertIsComponent(ContractType);
            BindingUtil.AssertIsNotAbstract(ContractType);

            var gameObjectInfo = new GameObjectCreationParameters();

            ProviderFunc =
                (container) => new InstantiateOnPrefabComponentProvider(
                    ContractType,
                    new PrefabInstantiator(
                        container, gameObjectInfo,
                        ContractType, new [] { ContractType }, new List <TypeValuePair>(),
                        new PrefabProviderResource(resourcePath),
                        BindInfo.InstantiatedCallback));

            return(new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo));
        }
Exemplo n.º 5
0
        public GameObjectNameGroupNameBinder ByPrefab(Type installerType, UnityEngine.Object prefab)
        {
            BindingUtil.AssertIsValidPrefab(prefab);

            Assert.That(installerType.DerivesFrom <MonoInstaller>(),
                        "Invalid installer type given during bind command.  Expected type '{0}' to derive from 'MonoInstaller'", installerType.Name());

            var gameObjectInfo = new GameObjectCreationParameters();

            SubFinalizer = CreateFinalizer(
                (container) => new SubContainerDependencyProvider(
                    ContractType, SubIdentifier,
                    new SubContainerCreatorByPrefabWithParams(
                        installerType,
                        container,
                        new PrefabProvider(prefab),
                        gameObjectInfo)));

            return(new GameObjectNameGroupNameBinder(BindInfo, gameObjectInfo));
        }
        public ScopeArgConditionCopyNonLazyBinder FromComponentInChildren(bool excludeSelf = false)
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            return(FromMethodMultiple((ctx) =>
            {
                Assert.That(ctx.ObjectType.DerivesFromOrEqual <MonoBehaviour>());
                Assert.IsNotNull(ctx.ObjectInstance);

                var res = ((MonoBehaviour)ctx.ObjectInstance).GetComponentsInChildren <TContract>()
                          .Where(x => !ReferenceEquals(x, ctx.ObjectInstance));

                if (excludeSelf)
                {
                    res = res.Where(x => (x as Component).gameObject != (ctx.ObjectInstance as Component).gameObject);
                }

                return res;
            }));
        }
Exemplo n.º 7
0
        public NameTransformConditionCopyNonLazyBinder ByNewPrefabMethod(
            UnityEngine.Object prefab,
#if !NET_4_6
            ModestTree.Util.
#endif
            Action <DiContainer, TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TParam8, TParam9, TParam10> installerMethod)
        {
            BindingUtil.AssertIsValidPrefab(prefab);

            var gameObjectInfo = new GameObjectCreationParameters();

            ProviderFunc =
                (container) => new SubContainerDependencyProvider(
                    ContractType, SubIdentifier,
                    new SubContainerCreatorByNewPrefabMethod <TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TParam8, TParam9, TParam10>(container,
                                                                                                                                                         new PrefabProvider(prefab),
                                                                                                                                                         gameObjectInfo, installerMethod), false);

            return(new NameTransformConditionCopyNonLazyBinder(BindInfo, gameObjectInfo));
        }
Exemplo n.º 8
0
        public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder ByNewPrefabResourceMethod(
            string resourcePath,
#if !NET_4_6
            ModestTree.Util.
#endif
            Action <DiContainer, TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TParam8, TParam9, TParam10> installerMethod)
        {
            BindingUtil.AssertIsValidResourcePath(resourcePath);

            var gameObjectInfo = new GameObjectCreationParameters();

            ProviderFunc =
                (container) => new SubContainerDependencyProvider(
                    ContractType, SubIdentifier,
                    new SubContainerCreatorByNewPrefabMethod <TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TParam8, TParam9, TParam10>(container,
                                                                                                                                                         new PrefabProviderResource(resourcePath),
                                                                                                                                                         gameObjectInfo, installerMethod), false);

            return(new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo));
        }
        public NameTransformConditionCopyNonLazyBinder ByPrefab(Type installerType, UnityEngine.Object prefab)
        {
            BindingUtil.AssertIsValidPrefab(prefab);

            Assert.That(installerType.DerivesFrom <MonoInstaller>(),
                        "Invalid installer type given during bind command.  Expected type '{0}' to derive from 'MonoInstaller'", installerType);

            var gameObjectInfo = new GameObjectCreationParameters();

            ProviderFunc =
                (container) => new SubContainerDependencyProvider(
                    ContractType, SubIdentifier,
                    new SubContainerCreatorByPrefabWithParams(
                        installerType,
                        container,
                        new PrefabProvider(prefab),
                        gameObjectInfo));

            return(new NameTransformConditionCopyNonLazyBinder(BindInfo, gameObjectInfo));
        }
Exemplo n.º 10
0
        public ScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentInHierarchy(
            bool includeInactive = true)
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            // Use FromMethodMultiple so that we can return the empty list when context is optional
            return(FromMethodMultiple((ctx) => {
                var res = BindContainer.Resolve <Context>().GetRootGameObjects()
                          .Select(x => x.GetComponentInChildren <TContract>(includeInactive))
                          .Where(x => x != null).FirstOrDefault();

                if (res == null)
                {
                    Assert.That(ctx.Optional, "Could not find component '{0}' through FromComponentInHierarchy binding", typeof(TContract));
                    return Enumerable.Empty <TContract>();
                }

                return new TContract[] { res };
            }));
        }
Exemplo n.º 11
0
        public ScopeArgConditionCopyNonLazyBinder FromComponentInParents(bool excludeSelf = false)
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            return(FromMethodMultiple((ctx) =>
            {
                var monoBehaviourContext = ctx.ToMonoBehaviourContext();
                Assert.IsNotNull(monoBehaviourContext);

                var res = monoBehaviourContext.GetComponentsInParent <TContract>()
                          .Where(x => !ReferenceEquals(x, ctx.ObjectInstance));

                if (excludeSelf)
                {
                    res = res.Where(x => (x as Component).gameObject != (ctx.ObjectInstance as Component).gameObject);
                }

                return res;
            }));
        }
Exemplo n.º 12
0
        public ScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentInChildren(bool includeInactive = true)
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            // Use FromMethodMultiple so that we can return the empty list when context is optional
            return(FromMethodMultiple((ctx) => {
                Assert.That(ctx.ObjectType.DerivesFromOrEqual <MonoBehaviour>(),
                            "Cannot use FromComponentInChildren to inject data into non monobehaviours!");
                Assert.IsNotNull(ctx.ObjectInstance);

                var res = ((MonoBehaviour)ctx.ObjectInstance).GetComponentInChildren <TContract>(includeInactive);

                if (res == null)
                {
                    Assert.That(ctx.Optional, "Could not find component '{0}' through FromComponentInChildren binding", typeof(TContract));
                    return Enumerable.Empty <TContract>();
                }

                return new TContract[] { res };
            }));
        }
Exemplo n.º 13
0
        protected ScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentsInChildrenBase(
            bool excludeSelf, Func <Component, bool> predicate, bool includeInactive)
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            BindInfo.RequireExplicitScope = false;

            // Don't know how it's created so can't assume here that it violates AsSingle
            BindInfo.MarkAsCreationBinding = false;

            SubFinalizer = new ScopableBindingFinalizer(
                BindInfo,
                (container, concreteType) => new MethodMultipleProviderUntyped(ctx =>
            {
                Assert.That(ctx.ObjectType.DerivesFromOrEqual <MonoBehaviour>(),
                            "Cannot use FromComponentsInChildren to inject data into non monobehaviours!");

                Assert.IsNotNull(ctx.ObjectInstance);

                var monoBehaviour = (MonoBehaviour)ctx.ObjectInstance;

                var res = monoBehaviour.GetComponentsInChildren(concreteType, includeInactive)
                          .Where(x => !ReferenceEquals(x, ctx.ObjectInstance));

                if (excludeSelf)
                {
                    res = res.Where(x => x.gameObject != monoBehaviour.gameObject);
                }

                if (predicate != null)
                {
                    res = res.Where(predicate);
                }

                return(res.Cast <object>());
            },
                                                                               container));

            return(this);
        }
Exemplo n.º 14
0
        public ScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentsInParents(
            bool excludeSelf = false, bool includeInactive = true)
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            return(FromMethodMultiple((ctx) =>
            {
                Assert.That(ctx.ObjectType.DerivesFromOrEqual <MonoBehaviour>(),
                            "Cannot use FromComponentInParents to inject data into non monobehaviours!");
                Assert.IsNotNull(ctx.ObjectInstance);

                var res = ((MonoBehaviour)ctx.ObjectInstance).GetComponentsInParent <TContract>(includeInactive)
                          .Where(x => !ReferenceEquals(x, ctx.ObjectInstance));

                if (excludeSelf)
                {
                    res = res.Where(x => (x as Component).gameObject != (ctx.ObjectInstance as Component).gameObject);
                }

                return res;
            }));
        }
Exemplo n.º 15
0
        public GameObjectNameGroupNameBinder FromGameObject()
        {
            var gameObjectInfo = new GameObjectBindInfo();

            if (ContractType == typeof(GameObject))
            {
                SubFinalizer = CreateFinalizer(
                    (container) => new EmptyGameObjectProvider(
                        container, gameObjectInfo.Name, gameObjectInfo.GroupName));
            }
            else
            {
                BindingUtil.AssertIsComponent(ContractType);
                BindingUtil.AssertIsNotAbstract(ContractType);

                SubFinalizer = CreateFinalizer(
                    (container) => new AddToNewGameObjectComponentProvider(
                        container, ContractType, null,
                        new List <TypeValuePair>(), gameObjectInfo.Name, gameObjectInfo.GroupName));
            }

            return(new GameObjectNameGroupNameBinder(BindInfo, gameObjectInfo));
        }
Exemplo n.º 16
0
        public GameObjectNameGroupNameScopeArgBinder FromGameObject()
        {
            BindingUtil.AssertIsAbstractOrComponentOrGameObject(BindInfo.ContractTypes);
            BindingUtil.AssertIsComponentOrGameObject(ConcreteTypes);

            var gameObjectInfo = new GameObjectBindInfo();

            if (ConcreteTypes.All(x => x == typeof(GameObject)))
            {
                SubFinalizer = new ScopableBindingFinalizer(
                    BindInfo, SingletonTypes.ToGameObject, gameObjectInfo,
                    (container, type) =>
                {
                    Assert.That(BindInfo.Arguments.IsEmpty(), "Cannot inject arguments into empty game object");
                    return(new EmptyGameObjectProvider(
                               container, gameObjectInfo.Name, gameObjectInfo.GroupName));
                });
            }
            else
            {
                BindingUtil.AssertIsComponent(ConcreteTypes);
                BindingUtil.AssertTypesAreNotAbstract(ConcreteTypes);

                SubFinalizer = new ScopableBindingFinalizer(
                    BindInfo, SingletonTypes.ToGameObject, gameObjectInfo,
                    (container, type) => new AddToNewGameObjectComponentProvider(
                        container,
                        type,
                        BindInfo.ConcreteIdentifier,
                        BindInfo.Arguments,
                        gameObjectInfo.Name,
                        gameObjectInfo.GroupName));
            }

            return(new GameObjectNameGroupNameScopeArgBinder(BindInfo, gameObjectInfo));
        }
Exemplo n.º 17
0
        public ScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentInChildren(
            bool includeInactive = true)
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            BindInfo.RequireExplicitScope = false;

            // Don't know how it's created so can't assume here that it violates AsSingle
            BindInfo.MarkAsCreationBinding = false;

            SubFinalizer = new ScopableBindingFinalizer(
                BindInfo,
                (container, concreteType) => new MethodMultipleProviderUntyped(ctx =>
            {
                Assert.That(ctx.ObjectType.DerivesFromOrEqual <MonoBehaviour>(),
                            "Cannot use FromComponentInChildren to inject data into non monobehaviours!");

                Assert.IsNotNull(ctx.ObjectInstance);

                var monoBehaviour = (MonoBehaviour)ctx.ObjectInstance;

                var match = monoBehaviour.GetComponentInChildren(concreteType, includeInactive);

                if (match == null)
                {
                    Assert.That(ctx.Optional,
                                "Could not find any component with type '{0}' through FromComponentInChildren binding", concreteType);
                    return(Enumerable.Empty <object>());
                }

                return(new object[] { match });
            },
                                                                               container));

            return(this);
        }
        void FinalizeBindingConcrete(DiContainer container, List <Type> concreteTypes)
        {
            if (concreteTypes.Count == 0)
            {
                // This can be common when using convention based bindings
                return;
            }

            var scope = GetScope();

            switch (scope)
            {
            case ScopeTypes.Transient:
            {
                RegisterProvidersForAllContractsPerConcreteType(
                    container, concreteTypes, _providerFactory);
                break;
            }

            case ScopeTypes.Singleton:
            {
                RegisterProvidersForAllContractsPerConcreteType(
                    container,
                    concreteTypes,
                    (_, concreteType) =>
                    BindingUtil.CreateCachedProvider(
                        _providerFactory(container, concreteType)));
                break;
            }

            default:
            {
                throw Assert.CreateException();
            }
            }
        }
Exemplo n.º 19
0
 public ScopeConcreteIdArgConditionCopyNonLazyBinder FromMethodMultiple(Func <InjectContext, IEnumerable <TContract> > method)
 {
     BindingUtil.AssertIsDerivedFromTypes(typeof(TContract), AllParentTypes);
     return(FromMethodMultipleBase <TContract>(method));
 }