Esempio n. 1
0
        public void CanBindAndGet()
        {
            const string customSingletonId = "customSingleton";
            const string customPrototypeId = "customPrototype";

            TestEmptyClass defaultSingletonEmptyObj = new TestEmptyClass();
            TestEmptyClass customSingletonEmptyObj  = new TestEmptyClass();
            TestEmptyClass customPrototypeEmptyObj  = new TestEmptyClass();

            Injector injector = new Injector();

            injector.Bind <TestEmptyClass>(defaultSingletonEmptyObj);
            injector.Bind <TestEmptyClass>(customSingletonEmptyObj, customSingletonId);
            injector.Bind <TestEmptyClass>(customPrototypeEmptyObj, InstantiationType.PROTOTYPE, customPrototypeId);

            // Default singleton
            TestEmptyClass resultDefaultSingletonObj = injector.Get <TestEmptyClass>();

            Assert.AreSame(defaultSingletonEmptyObj, resultDefaultSingletonObj);

            // custom singleton
            TestEmptyClass resultCustomSingletonObj = injector.Get <TestEmptyClass>(customSingletonId);

            Assert.AreSame(customSingletonEmptyObj, resultCustomSingletonObj);
            Assert.AreNotSame(defaultSingletonEmptyObj, resultCustomSingletonObj);

            // custom prototype
            TestEmptyClass resultCustomPrototypeObj = injector.Get <TestEmptyClass>(customPrototypeId);

            Assert.AreNotSame(customPrototypeEmptyObj, resultCustomPrototypeObj);
            Assert.AreEqual(typeof(TestEmptyClass), resultCustomPrototypeObj.GetType());
        }
Esempio n. 2
0
        public void CanBindNewInstance()
        {
            const string defaultId = "default";
            const string childId   = "child";

            Injector injector = new Injector();

            injector.BindNewInstance <TestEmptyClass>();
            injector.BindNewInstance <TestEmptyClass, TestEmptyChildClass>();

            Assert.IsTrue(injector.ContainsCustomId <TestEmptyClass>(defaultId));
            Assert.IsTrue(injector.ContainsCustomId <TestEmptyClass>(childId));

            TestEmptyClass childClass = injector.Get <TestEmptyClass>(childId);

            Assert.AreEqual(typeof(TestEmptyChildClass), childClass.GetType());
        }
        public void CanGet()
        {
            TestEmptyClass defaultSingleton = StaticContainer.Get <TestEmptyClass>();

            Assert.AreSame(DEFAULT_EMPTY_CLASS, defaultSingleton);

            TestEmptyClass customSingleton = StaticContainer.Get <TestEmptyClass>(CUSTOM_ID);

            Assert.AreSame(CUSTOM_EMPTY_CLASS, customSingleton);

            TestEmptyClass prototype = StaticContainer.Get <TestEmptyClass>(PROTOTYPE_ID);

            Assert.AreNotSame(PROTOTYPE_EMPTY_CLASS, prototype);
            Assert.AreEqual(typeof(TestEmptyClass), prototype.GetType());

            TestEmptyChildClass childClass = (TestEmptyChildClass)StaticContainer.Get <TestEmptyClass>(SUBCLASS_ID);

            Assert.AreSame(EMPTY_CHILD_CLASS, childClass);
        }
Esempio n. 4
0
 public TestClassWithCustomConstructor([ID("custom")] TestEmptyClass cls)
 {
     this.TestEmptyCls = cls;
 }
Esempio n. 5
0
 private TestClassWithCustomConstructor()
 {
     this.TestEmptyCls = null;
 }
Esempio n. 6
0
 public TestClassWithConstructor(TestEmptyClass cls)
 {
     this.TestEmptyCls = cls;
 }
Esempio n. 7
0
 public TestClassWithConstructorInjectionFail(float testFloat, TestEmptyClass emptyCls)
 {
     this.TestFloat    = testFloat;
     this.TestEmptyCls = emptyCls;
 }
Esempio n. 8
0
 public TestClassWithConstructorInjection(TestEmptyClass emptyClass,
                                          [ID("custom")] int intValue)
 {
     _intValue  = intValue;
     EmptyClass = emptyClass;
 }