Exemplo n.º 1
0
        public void ExampleUsage2()
        {
            var IoC_inject = GetInjectorForTest();

            // GetOrAddSingleton will automatically init a singleton instance and setup a provider:
            var singletonRef1 = IoC_inject.GetOrAddSingleton <MyClass1>(this);

            Assert.NotNull(singletonRef1);
            Assert.True(singletonRef1 is MyClass1);

            // When GetOrAddSingleton is called a second time the same instance is returned as before:
            var singletonRef2 = IoC_inject.GetOrAddSingleton <MyClass1>(this);

            // The returned references must be equal:
            Assert.True(Object.ReferenceEquals(singletonRef1, singletonRef2));
            // The same singleton instance can be accessed via the normal IoC.inject.Get(..):
            Assert.True(Object.ReferenceEquals(singletonRef1, IoC_inject.Get <MyClass1>(this)));

            // The singleton provider can be removed again:
            Assert.True(IoC_inject.RemoveAllInjectorsFor <MyClass1>());
            Assert.Null(IoC_inject.Get <MyClass1>(this));

            // If there is already a provider set for MyClass1 GetOrAddSingleton will not create a singleton provider:
            var singletonInstance = new MySubClass1();

            IoC_inject.RegisterInjector <MyClass1>(new object(), (_, createIfNull) => {
                return(createIfNull ? singletonInstance : null);
            });
            Assert.Same(singletonInstance, IoC_inject.Get <MyClass1>(this));
            Assert.Same(singletonInstance, IoC_inject.GetOrAddSingleton <MyClass1>(this));
        }
Exemplo n.º 2
0
        public void TestMissingDefaultConstructor()
        {
            var x1 = new MySubClass1()
            {
                myString = "I am s1", myString2 = "I am s2"
            };
            var jsonString = JsonWriter.GetWriter().Write(x1 as MyClass2);
            var x2         = JsonReader.GetReader().Read <MySubClass2_WithNoDefaultConstructor>(jsonString);

            Assert.Equal(x1.myString, x2.myString);
        }
Exemplo n.º 3
0
        public void TestOverrideOldSubscriber()
        {
            Injector injector         = GetInjectorForTest();
            var      injectionHandler = new object();

            // Register an injector that will be replaced by a second one:
            injector.RegisterInjector <MyClass1>(injectionHandler, (caller, createIfNull) => { throw Log.e("Should be replaced"); });
            // Replace it:
            MySubClass1 myClass1Singleton = new MySubClass1();

            injector.RegisterInjector <MyClass1>(injectionHandler, (caller, createIfNull) => myClass1Singleton);
            Assert.Same(myClass1Singleton, injector.Get <MyClass1>(this));
        }
Exemplo n.º 4
0
        public void TestWithTypedJson()
        {
            // Typed json includes the C# assembly types in the json, so works only in a C# only scenario to parse the
            // json string back into the correct C# class
            MySubClass1 x1 = new MySubClass1()
            {
                myString = "I am s1", myComplexField2 = new MySubClass1()
                {
                    myString = "A2"
                }
            };
            string json = TypedJsonHelper.NewTypedJsonWriter().Write(x1);
            object x2   = TypedJsonHelper.NewTypedJsonReader().Read <object>(json);

            Assert.True(x2 is MySubClass1);
            var x3 = x2 as MySubClass1;

            Assert.Equal(x3.myString, x1.myString);
            Assert.Equal((x3.myComplexField2 as MySubClass1).myString, (x1.myComplexField2 as MySubClass1).myString);
        }
Exemplo n.º 5
0
        public void TestMultipleInjectors2()
        {
            var      IoC_inject = GetInjectorForTest();
            MyClass1 a          = new MySubClass1();
            MyClass1 b          = new MySubClass2();

            var firstInjectorWasUsed  = false;
            var secondInjectorWasUsed = false;

            Assert.False(IoC_inject.HasInjectorRegistered <MyClass1>());
            IoC_inject.RegisterInjector(new object(), (caller, createIfNull) => {
                Log.d("Injector with MySubClass1 was called");
                firstInjectorWasUsed = true;
                return(a);
            });
            Assert.True(IoC_inject.HasInjectorRegistered <MyClass1>());

            IoC_inject.RegisterInjector(new object(), (caller, createIfNull) => {
                Log.d("Injector with MySubClass2 was called");
                secondInjectorWasUsed = true;
                return(b);
            });

            Assert.False(firstInjectorWasUsed);
            Assert.False(secondInjectorWasUsed);
            Assert.Equal(b, IoC_inject.Get <MyClass1>(this));
            Assert.False(firstInjectorWasUsed);
            Assert.True(secondInjectorWasUsed);

            var bothResults = IoC_inject.GetAll <MyClass1>(this);

            Assert.Equal(b, bothResults.First());
            Assert.False(firstInjectorWasUsed); // before accessing .Last the injection was not yet triggered

            Assert.Equal(a, bothResults.Last());
            Assert.True(firstInjectorWasUsed); // after accessing .Last the injection was triggered
        }
Exemplo n.º 6
0
        public void ExampleUsage1()
        {
            // The default injector can be accessed via IoC.inject
            Injector injector = GetInjectorForTest();

            // Requesting an instance of MyClass1 will fail because no injector registered yet to handle requests for the MyClass1 type:
            Assert.Null(injector.Get <MyClass1>(this));

            // Setup an injector that will always return the same instance for MyClass1 when IoC.inject.Get<MyClass1>() is called:
            MySubClass1 myClass1Singleton = new MySubClass1();

            injector.SetSingleton <MyClass1, MySubClass1>(myClass1Singleton);

            // Internally .SetSingleton() will register an injector for the class like this:
            injector.RegisterInjector <MyClass1>(new object(), (caller, createIfNull) => {
                // Whenever injector.Get is called the injector always returns the same instance:
                return(myClass1Singleton);
            });

            // Now calling IoC.inject.Get<MyClass1>() will always result in the same instance:
            MyClass1 myClass1 = injector.Get <MyClass1>(this);

            Assert.Same(myClass1Singleton, myClass1); // Its the same object reference
        }