Beispiel #1
0
        public void Test_PostCallback()
        {
            Injector injector = new Injector();

            //  Add first binding
            injector.AddBinding <SimpleClassA>().ToType <SimpleClassA>().SetPostInjectionCallback(Callback);
            injector.AddBinding <SimpleClassB>().ToValue(new SimpleClassB());

            //  Validate binding
            Assert.Equal(2, injector.BindingCount);
            Assert.True(injector.HasBindingForType(typeof(SimpleClassA)));
            Assert.True(injector.HasBindingForType(typeof(SimpleClassB)));

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Set fields
            _IscallbackCalled = false;
            _CallbackCaller   = null;

            //  Get Instance
            SimpleClassA value = injector.GetInstance <SimpleClassA>();

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Validate binding
            Assert.True(_IscallbackCalled);
            Assert.Equal(value, _CallbackCaller);
        }
        public void Test_UnifyTypedProvidersWithSameType()
        {
            Injector injector = new Injector();

            //  Add first binding
            injector.AddBinding <ISimpleInterfaceA>().ToType <SimpleClassA>();

            //  Check bindings and providers
            Assert.Equal(1, injector.BindingCount);
            Assert.Equal(1, injector.ProviderCount);

            //  Check errors
            Assert.Equal(0, injector.ErrorCount);

            //  Add second binding with same provider type
            injector.AddBinding <ISimpleInterfaceAA>().ToType <SimpleClassA>();

            //  Check bindings and providers
            Assert.Equal(2, injector.BindingCount);
            Assert.Equal(1, injector.ProviderCount);

            //  Add third binding with same provider type
            injector.AddBinding <SimpleClassA>().ToType <SimpleClassA>();

            //  Check bindings and providers
            Assert.Equal(3, injector.BindingCount);
            Assert.Equal(1, injector.ProviderCount);

            //  Check errors
            Assert.Equal(0, injector.ErrorCount);
        }
        public void Test_Error_AlreadyAddedTypedProviderWithDifferentProvider()
        {
            Injector injector = new Injector();

            //  Add first binding to a value
            injector.AddBinding <ISimpleInterfaceAA>().ToValue(new SimpleClassA());

            //  Check bindings and providers
            Assert.Equal(1, injector.BindingCount);
            Assert.Equal(1, injector.ProviderCount);

            //  Check errors
            Assert.Equal(0, injector.ErrorCount);

            //  Add second binding with same provider type
            injector.AddBinding <ISimpleInterfaceA>().ToType <SimpleClassA>();

            //  Check bindings and providers
            Assert.Equal(2, injector.BindingCount);
            Assert.Equal(1, injector.ProviderCount);

            //  Check errors
            Assert.Equal(1, injector.ErrorCount);
            Assert.Equal(InjectionErrorType.AlreadyAddedTypeWithDifferentProvider, injector.GetError(0).Error);
        }
    //  METHODS
    void Awake()
    {
        //  Create injector
        _Injector = new Injector(true);

        //  Add bindings
        //  As long as they are all assignable to binding type you exchange between different implementations of classes.
        //  This gives you easy way to achieve follwing without touching rest of your code.
        //  - test special cases to find spesific errors
        //  - isolate some part of your programs for easy debugging
        //  - change between different implementation for different platforms or plugins

        //  In this example given implemetation of Setting class is always muted and will not be changed with user input
        _Injector.AddBinding <ISettings>().ToType <Settings_AlwaysMuted>();
        _Injector.AddBinding <IReadonlySettings>().ToType <Settings_AlwaysMuted>();

        //  Iterate all object in list
        foreach (GameObject go in InjectionTargetList)
        {
            MonoBehaviour[] scriptlist = go.GetComponents <MonoBehaviour>();

            //  iterate all scripts
            foreach (MonoBehaviour sc in scriptlist)
            {
                //  Perform injections
                _Injector.InjectInto(sc);
            }
        }
    }
Beispiel #5
0
        public void Test_AddingBindings()
        {
            Injector injector = new Injector();

            //  Add first binding
            injector.AddBinding <SimpleClassA>();

            //  Validate binding
            Assert.Equal(1, injector.BindingCount);
            Assert.True(injector.HasBindingForType(typeof(SimpleClassA)));

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Add second binding
            injector.AddBinding <SimpleClassB>();

            //  Validate binding
            Assert.Equal(2, injector.BindingCount);
            Assert.True(injector.HasBindingForType(typeof(SimpleClassA)));
            Assert.True(injector.HasBindingForType(typeof(SimpleClassB)));

            //  Check error
            Assert.Equal(0, injector.ErrorCount);
        }
        public void Test_Error_AddingValueProviderToWrongType()
        {
            Injector injector = new Injector();

            //  Add first binding and set value provider
            injector.AddBinding <SimpleClassA>().ToValue(new SimpleClassB());

            //  Validate binding
            Assert.Equal(1, injector.BindingCount);
            Assert.Equal(0, injector.ProviderCount);

            //  Check error
            Assert.Equal(1, injector.ErrorCount);
            Assert.Equal(InjectionErrorType.ValueNotAssignableToBindingType, injector.GetError(0).Error);

            //  Add second binding and set value provider
            injector.AddBinding <SimpleClassB>().ToValue(new SimpleClassA());

            //  Validate binding
            Assert.Equal(2, injector.BindingCount);
            Assert.Equal(0, injector.ProviderCount);

            //  Check error
            Assert.Equal(2, injector.ErrorCount);
            Assert.Equal(InjectionErrorType.ValueNotAssignableToBindingType, injector.GetError(1).Error);
        }
        public void Test_GetAssignableInstances()
        {
            Injector injector = new Injector(true);

            //  Add first binding
            injector.AddBinding <SimpleClassA>().ToType <SimpleClassA>();
            injector.AddBinding <SimpleClassAA>().ToType <SimpleClassAA>();
            injector.AddBinding <SimpleClassB>().ToType <SimpleClassB>();
            injector.AddBinding <SimpleClassC>().ToValue(new SimpleClassC());

            //  Validate binding
            Assert.Equal(4, injector.BindingCount);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Check instance types and count
            IEnumerator <ISimpleInterfaceAA> instances = injector.GetAssignableInstances <ISimpleInterfaceAA>();
            int instanceCounter = 0;

            while (instances.MoveNext())
            {
                Assert.IsAssignableFrom <ISimpleInterfaceAA>(instances.Current);
                instanceCounter++;
            }
            Assert.Equal(2, instanceCounter);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);
        }
Beispiel #8
0
        public void Test_Error_ReAddingExistingBindings()
        {
            Injector injector = new Injector();

            //  Add two bindings
            injector.AddBinding <SimpleClassA>();
            injector.AddBinding <SimpleClassB>();

            //  Validate bindings
            Assert.Equal(2, injector.BindingCount);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Try re-adding first binding
            injector.AddBinding <SimpleClassA>();

            //  Check error
            Assert.Equal(1, injector.ErrorCount);
            Assert.Equal(InjectionErrorType.AlreadyAddedBindingForType, injector.GetError(0).Error);

            //  Try re-adding second binding
            injector.AddBinding <SimpleClassB>();

            //  Check error
            Assert.Equal(2, injector.ErrorCount);
            Assert.Equal(InjectionErrorType.AlreadyAddedBindingForType, injector.GetError(1).Error);
        }
    //  METHODS
    void Awake()
    {
        //  Create injector
        _Injector = new Injector(true);

        //  Add bindings
        //  Usage 1 : Bind to type; all bindings to same type will receive same instance when injected.
        //  You can use this when you want the object to be instantiated when it is first needed.
        _Injector.AddBinding <ISettings>().ToType <Settings>();
        _Injector.AddBinding <IReadonlySettings>().ToType <Settings>();

        //  Usage 2 : Bind to value; all bindings to same value will receive this instance when injected.
        //  You can use this when you want to use an already instantiated object.
        //Settings settings = new Settings();
        //settings.SetSoundVolume(0.2f);
        //settings.ToggleMusic();
        //_Injector.AddBinding<ISettings>().ToValue(settings);
        //_Injector.AddBinding<IReadonlySettings>().ToValue(settings);

        //  Iterate all object in list
        foreach (GameObject go in InjectionTargetList)
        {
            MonoBehaviour[] scriptlist = go.GetComponents <MonoBehaviour>();

            //  iterate all scripts
            foreach (MonoBehaviour sc in scriptlist)
            {
                //  Perform injections
                _Injector.InjectInto(sc);
            }
        }
    }
        public void Test_Error_BindingAfterInjectInto()
        {
            Injector injector = new Injector();

            //  Add a bindings
            injector.AddBinding <SimpleClassA>().ToType <SimpleClassA>();

            //  Validate bindings
            Assert.Equal(1, injector.BindingCount);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Get instance
            ClassThatUses_SimpleClassA value = new ClassThatUses_SimpleClassA();

            injector.InjectInto(value);

            //  Try adding a new binding
            injector.AddBinding <SimpleClassB>();

            //  Check error
            Assert.Equal(1, injector.ErrorCount);
            Assert.Equal(InjectionErrorType.BindingAfterInjection, injector.GetError(0).Error);
        }
Beispiel #11
0
        public void Test_SettingValueProvider()
        {
            Injector injector = new Injector();

            //  Add bindings
            injector.AddBinding <CrossReferenceClassA>().ToType <CrossReferenceClassA>();
            injector.AddBinding <CrossReferenceClassB>().ToType <CrossReferenceClassB>();

            //  Create injection targets
            CrossReferenceContainer target = new CrossReferenceContainer();

            //  Check before injection
            Assert.Null(target.valueA);
            Assert.Null(target.valueB);

            //  Inject
            injector.InjectInto(target);

            //  Check after injection
            Assert.NotNull(target.valueA);
            Assert.NotNull(target.valueB);
            Assert.NotNull(target.valueA.value);
            Assert.NotNull(target.valueB.value);
            Assert.Equal(target.valueA.value, target.valueB);
            Assert.Equal(target.valueB.value, target.valueA);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);
        }
Beispiel #12
0
    public static Injector WithBinding(this Injector injector, IBinding binding)
    {
        Injector childInjector = injector.CreateChildInjector();

        childInjector.AddBinding(binding);
        return(childInjector);
    }
        public void Test_ValueInjectionToNestedMembers()
        {
            Injector injector = new Injector();

            //  Add first binding
            SimpleClassA value = new SimpleClassA();

            injector.AddBinding <SimpleClassA>().ToValue(value);

            //  Create injection target
            ExtendedClassThatUses_SimpleClassA target = new ExtendedClassThatUses_SimpleClassA();

            //  Check before injection
            Assert.Null(target.value1);
            Assert.Null(target.value2);

            //  Inject
            injector.InjectInto(target);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Check after injection
            Assert.NotNull(target.value1);
            Assert.NotNull(target.value2);
            Assert.Same(value, target.value1);
            Assert.Same(value, target.value2);
        }
        public void Test_Error_ValueInjectionToWrongType()
        {
            Injector injector = new Injector();

            //  Add first binding
            SimpleClassB value = new SimpleClassB();

            injector.AddBinding <SimpleClassB>().ToValue(value);

            //  Create injection target
            ClassThatUses_SimpleClassA target = new ClassThatUses_SimpleClassA();

            //  Check before injection
            Assert.Null(target.value1);
            Assert.Null(target.value2);

            //  Inject
            injector.InjectInto(target);

            //  Check error
            Assert.Equal(2, injector.ErrorCount);
            Assert.Equal(InjectionErrorType.CanNotFindBindingForType, injector.GetError(0).Error);
            Assert.Equal(InjectionErrorType.CanNotFindBindingForType, injector.GetError(1).Error);

            //  Check after injection
            Assert.Null(target.value1);
            Assert.Null(target.value2);
        }
    //  METHODS
    void Awake()
    {
        //  Create injector
        //  When an error occures injector creates an error data stores it in a list and optionally thorws an exception with this error.
        //  Only parameter in constructor indicates if injector should throw this exceptions.
        //  You can alternatively disable exceptions and individually check for errors after every binding and injection
        _Injector = new Injector(true);

        try
        {
            //  Add bindings
            //  Wrong binding: object does not implement ISettings interface
            _Injector.AddBinding <ISettings>().ToValue(new object());
            //  Missing binding: IReadonlySettings is never bind

            //  Iterate all object in list
            foreach (GameObject go in InjectionTargetList)
            {
                MonoBehaviour[] scriptlist = go.GetComponents <MonoBehaviour>();

                //  iterate all scripts
                foreach (MonoBehaviour sc in scriptlist)
                {
                    //  Perform injections
                    _Injector.InjectInto(sc);
                }
            }
        }
        catch (InjectionException e)
        {
            Debug.Log(e.Message);
            Debug.Break();
        }
    }
    private void FillLayout()
    {
        SongMeta songMeta   = sceneData.SongMeta;
        string   titleText  = (String.IsNullOrEmpty(songMeta.Title)) ? "" : songMeta.Title;
        string   artistText = (String.IsNullOrEmpty(songMeta.Artist)) ? "" : " - " + songMeta.Artist;

        songLabel.text = titleText + artistText;

        int        i = 0;
        GameObject selectedLayout = GetSelectedLayout();

        foreach (PlayerProfile playerProfile in sceneData.PlayerProfiles)
        {
            sceneData.PlayerProfileToMicProfileMap.TryGetValue(playerProfile, out MicProfile micProfile);
            PlayerScoreControllerData playerScoreData = sceneData.GetPlayerScores(playerProfile);
            SongRating songRating = GetSongRating(playerScoreData.TotalScore);

            Injector childInjector = UniInjectUtils.CreateInjector(injector);
            childInjector.AddBindingForInstance(playerProfile);
            childInjector.AddBindingForInstance(micProfile);
            childInjector.AddBindingForInstance(playerScoreData);
            childInjector.AddBindingForInstance(songRating);
            childInjector.AddBinding(new Binding("playerProfileIndex", new ExistingInstanceProvider <int>(i)));

            SingingResultsPlayerUiController[] uiControllers = selectedLayout.GetComponentsInChildren <SingingResultsPlayerUiController>();
            if (i < uiControllers.Length)
            {
                childInjector.InjectAllComponentsInChildren(uiControllers[i]);
            }
            i++;
        }
    }
        public void Test_AddingValueProviderToAssignableType()
        {
            Injector injector = new Injector();

            //  Add first binding and a typed provider
            injector.AddBinding <ISimpleInterfaceA>().ToValue(new SimpleClassA());

            //  Validate
            Assert.Equal(1, injector.BindingCount);
            Assert.Equal(1, injector.ProviderCount);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Add second binding and set typed provider
            injector.AddBinding <ISimpleInterfaceB>().ToValue(new SimpleClassB());

            //  Validate
            Assert.Equal(2, injector.BindingCount);
            Assert.Equal(2, injector.ProviderCount);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);
        }
        public void Test_AddingTypedProvider()
        {
            Injector injector = new Injector();

            //  Add first binding and a typed provider
            injector.AddBinding <SimpleClassA>().ToType <SimpleClassA>();

            //  Validate
            Assert.Equal(1, injector.BindingCount);
            Assert.Equal(1, injector.ProviderCount);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Add second binding and set typed provider
            injector.AddBinding <SimpleClassB>().ToType <SimpleClassB>();

            //  Validate
            Assert.Equal(2, injector.BindingCount);
            Assert.Equal(2, injector.ProviderCount);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);
        }
Beispiel #19
0
        public void Test_TypedInjectionToAssignableType()
        {
            Injector injector = new Injector();

            //  Add first binding
            injector.AddBinding <ISimpleInterfaceA>().ToType <SimpleClassA>();

            //  Create injection target
            ClassThatUses_SimpleInterfaceA target = new ClassThatUses_SimpleInterfaceA();

            //  Check before injection
            Assert.Null(target.value);

            //  Inject
            injector.InjectInto(target);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Check after injection
            Assert.NotNull(target.value);
        }