예제 #1
0
        public void ShouldConnectInstantiatedObjectToExistingTree()
        {
            ConnectUp          connectUp = new ConnectUp();
            DependencyInjector pdi       = new DependencyInjector();
            var existing = pdi.CreateAndInjectDependencies <ExistingRoot>();

            pdi.CreateAndInjectDependencies(connectUp, existing.injectionState);
            Assert.AreEqual(connectUp.connectedChild, existing.rootBean.existingChild);
        }
예제 #2
0
        public void ShouldNotWarnIfPrototypeRootObjectWasAlreadyInjected()
        {
            var            pdi = new DependencyInjector();
            InjectionState @is;

            (_, @is) = pdi.CreateAndInjectDependencies <AlreadyInstantiated>();
            (_, @is) = pdi.CreateAndInjectDependencies(new AlreadyInstantiated(), injectionState: @is
                                                       , rootBeanSpec: new RootBeanSpec(scope: BeanScope.Prototype));
            Assert.AreEqual(0, @is.Diagnostics.Groups["RootObjectExists"].Occurrences.Count);
        }
예제 #3
0
        public void ShouldWarnIfRootObjectWasAlreadyInjected()
        {
            var            pdi = new DependencyInjector();
            InjectionState @is;

            (_, @is) = pdi.CreateAndInjectDependencies <AlreadyInstantiated>();
            (_, @is) = pdi.CreateAndInjectDependencies(new AlreadyInstantiated(), injectionState: @is
                                                       );
            Assert.AreEqual(1, @is.Diagnostics.Groups["RootObjectExists"].Occurrences.Count);
        }
예제 #4
0
        public void ShouldCreateTreeForRootObjectWithoutExplictAssemblies()
        {
            DependencyInjector pdi = new DependencyInjector();

            (object inserted, InjectionState InjectionState)
                = pdi.CreateAndInjectDependencies(new InsertedAsObject());
            InferAssembly infer = pdi.CreateAndInjectDependencies <InferAssembly>(InjectionState).rootBean;

            Assert.IsNotNull(infer.inserted);
        }
예제 #5
0
        public void ShouldReferenceUserCreatedObject()
        {
            DependencyInjector pdi = new DependencyInjector();
            var isbu = new IOCCTest.MultipleCallsTestCode.InstantiatedByUser();

            isbu.value = 42;
            (_, InjectionState @is)        = pdi.CreateAndInjectDependencies(isbu);
            (var cup, InjectionState @is2) = pdi.CreateAndInjectDependencies <IOCCTest.MultipleCallsTestCode.CreatedByPureDI>(@is);
            Assert.IsNotNull(cup.instantiatedByUser);
            Assert.AreEqual(42, cup.instantiatedByUser?.value);
        }
예제 #6
0
        public void ShouldRespectConstructorNameOnRootObject()
        {
            var            pdi   = new DependencyInjector();
            var            named = new NamedConstructor(new MyParam());
            InjectionState @is;

            (_, @is) = pdi.CreateAndInjectDependencies(named,
                                                       rootBeanSpec: new RootBeanSpec(rootConstructorName: "MyConstructor"));
            (var other, var is2) = pdi.CreateAndInjectDependencies <AnotherClass>(@is);
            Assert.IsNotNull(other.named);
        }
예제 #7
0
 public (object bean, InjectionState injectionState) Execute(InjectionState injectionState, BeanFactoryArgs args)
 {
     if (System.Environment.GetCommandLineArgs().Length > 1)
     {
         return(iocContainer.CreateAndInjectDependencies <FileListProvider>(injectionState));
     }
     else
     {
         return(iocContainer.CreateAndInjectDependencies <UsageListProvider>(injectionState, rootBeanSpec: new RootBeanSpec(rootBeanName: "usage")));
     }
 }
예제 #8
0
 public (object bean, InjectionState injectionState) Execute(InjectionState injectionState, BeanFactoryArgs args)
 {
     if (System.Diagnostics.Debugger.IsAttached)
     {
         return(iocContainer.CreateAndInjectDependencies <VSOutputWindow>(injectionState
                                                                          , rootBeanSpec: new RootBeanSpec(rootBeanName: "outputWindow")));
     }
     else
     {
         return(iocContainer.CreateAndInjectDependencies <ConsoleDisplay>(injectionState));
     }
 }
예제 #9
0
        public void ShouldInjectDependenciesForRootObjectBaseClass()
        {
            var            pdi   = new DependencyInjector();
            var            @base = new BaseClasses();
            InjectionState @is;

            (_, @is) = pdi.CreateAndInjectDependencies(@base);
            (SomeClassUser scu, InjectionState is2) = pdi.CreateAndInjectDependencies <SomeClassUser>(@is);
            Assert.IsNotNull(scu.BaseClasses);
            Assert.IsNotNull(scu.SomeBaseClass);
            Assert.IsNotNull(scu.SomeInterface);
        }
예제 #10
0
        [TestMethod]    // singleton deferred
        public void ShouldRespectDeferredFlag()
        {
            var            pdi = new DependencyInjector();
            InjectionState @is;
            var            rootBean = new AlreadyInstantiated();

            (_, @is) = pdi.CreateAndInjectDependencies(rootBean
                                                       , deferDepedencyInjection: true);
            Assert.IsNull(rootBean?.Child);
            (_, @is) = pdi.CreateAndInjectDependencies <AlreadyInstantiated>(@is);
            Assert.IsNotNull(rootBean?.Child);
        }
예제 #11
0
        public void ShouldHookUpWithRootObjwectFromAnotherEntryPoint()
        {
            DependencyInjector pdi = new DependencyInjector();
            object             obj;
            InjectionState     injectionState;

            (obj, injectionState) = pdi.CreateAndInjectDependencies(new DeepHierarchy());
            SomeUser someUser = pdi.CreateAndInjectDependencies <SomeUser>(injectionState
                                                                           , assemblies: new Assembly[] { this.GetType().Assembly }
                                                                           ).rootBean;

            Assert.IsNotNull(someUser.deep);
        }
        public void ShouldNotInitializeMembersWhenDDIFlagSetForRootObject()
        {
            var simple = new Simple();
            var pdi    = new DependencyInjector();

            (_, InjectionState @is) = pdi.CreateAndInjectDependencies(simple, deferDepedencyInjection: true);
            Assert.IsNull(simple?.SimpleChild);
            Assert.IsNull(simple?.NotSimpleChild);
            Assert.AreEqual(1, @is.Diagnostics.Groups["IncompleteInjections"].Occurrences.Count);
            (_, @is) = pdi.CreateAndInjectDependencies(simple, @is);
            Assert.IsNotNull(simple?.SimpleChild);
            Assert.IsNotNull(simple?.NotSimpleChild);
            Assert.AreEqual(0, @is.Diagnostics.Groups["IncompleteInjections"].Occurrences.Count);
        }
예제 #13
0
        public (object bean, InjectionState injectionState) Execute(InjectionState injectionState, BeanFactoryArgs args)
        {
            switch ((int)args.FactoryParmeter)
            {
            case 1:
                return(pDependencyInjector.CreateAndInjectDependencies <ChildOne>(injectionState));

            case 2:
                return(pDependencyInjector.CreateAndInjectDependencies <ChildTwo>(injectionState));

            default:
                throw new Exception("Execute failed");
            }
        }
예제 #14
0
        public void ShouldCreateUniqueInstancesWithNamedRootObject()
        {
            Instance       instanceInstance = new Instance();
            var            pdi = new DependencyInjector();
            InjectionState @is, is2;

            (_, @is) = pdi.CreateAndInjectDependencies(instanceInstance, rootBeanSpec: new RootBeanSpec("MyInstance"));
            MultipleInstances multiple;

            (multiple, is2) = pdi.CreateAndInjectDependencies <MultipleInstances>(@is);
            Assert.IsNotNull(multiple.InstanceInstance);
            Assert.IsNotNull(multiple.ClassInstance);
            Assert.AreNotEqual(multiple.ClassInstance, multiple.InstanceInstance);
        }
예제 #15
0
        public void ShouldCreateTreeForMultipleProfiles()
        {
            Diagnostics diagnostics = null;

            try
            {
                Assembly           assembly = CreateAssembly($"{TestResourcePrefix}.ProfileTestData.ComplexProfile.cs");
                DependencyInjector sic1     = new DependencyInjector(profiles: new string[] { "P1", "P2", "P3" });
                //sic1.SetAssemblies(assembly.GetName().Name);
                DependencyInjector sic2 = new DependencyInjector(profiles: new string[] { "P1", "P4" });
                //sic2.SetAssemblies(assembly.GetName().Name);
                (object rootBean1, InjectionState injectionState) =
                    sic1.CreateAndInjectDependencies("IOCCTest.ProfileTestData.ComplexProfile", assemblies: new Assembly[] { assembly });
                var result1 = rootBean1 as IResultGetter;
                diagnostics = injectionState.Diagnostics;
                var result2 =
                    sic2.CreateAndInjectDependencies("IOCCTest.ProfileTestData.ComplexProfile", assemblies: new Assembly[] { assembly }).rootBean as
                    IResultGetter;
                System.Diagnostics.Debug.WriteLine(diagnostics);
                Assert.IsNotNull(result1?.GetResults().ChildP2);
                Assert.IsNotNull(result1?.GetResults().ChildP3);
                Assert.IsNull(result1?.GetResults().ChildP4);
                Assert.IsNull(result2?.GetResults().ChildP2);
                Assert.IsNull(result2?.GetResults().ChildP3);
                Assert.IsNotNull(result2?.GetResults().ChildP4);
                Assert.IsTrue(diagnostics.HasWarnings);
            }
            catch (DIException)
            {
                System.Diagnostics.Debug.WriteLine(diagnostics);
                Assert.Fail();
            }
        }
예제 #16
0
        public void ShouldCreateTreeForInstantiatedObjectInHierarchy()
        {
            DependencyInjector pdi     = new DependencyInjector();
            Complex            complex = pdi.CreateAndInjectDependencies <Complex>().rootBean;

            Assert.AreEqual("ValueOne", complex.value1.val);
            Assert.AreEqual("ValueTwo", complex.value2.val);
        }
예제 #17
0
        public void SHouldCreateTreeForInstantiatedObject()
        {
            Simple             simple = new Simple();
            DependencyInjector pdi    = new DependencyInjector();

            pdi.CreateAndInjectDependencies(simple, assemblies: new Assembly[] { this.GetType().Assembly });
            Assert.IsNotNull(simple.GetResults().Child);
        }
예제 #18
0
    public static void Main()
    {
        DependencyInjector pdi = new DependencyInjector();
        MyService          ms  = pdi.CreateAndInjectDependencies <MyService>()
                                 .rootBean;

        Console.WriteLine(ms.DoStuff());   // prints "doing the real thing"
    }
예제 #19
0
    public static void Main()
    {
        DependencyInjector pdi = new DependencyInjector();
        var prog = pdi.CreateAndInjectDependencies <IntroductionDemo>()
                   .rootBean;

        prog.SaySomething();  // prints "Hello Simple"
    }
예제 #20
0
 public void ShouldThrowExceptionForNullBeanName3()
 {
     Assert.ThrowsException <ArgumentNullException>(
         () =>
     {
         var pdi = new DependencyInjector();
         pdi.CreateAndInjectDependencies("CheckArgumentsTest", rootBeanSpec: new RootBeanSpec(rootBeanName: null));
     });
 }
예제 #21
0
        static void Main(string[] args)
        {
            DependencyInjector pdi = new DependencyInjector();
            TodoProcessor      tdp = pdi.CreateAndInjectDependencies <TodoProcessor>().rootBean;

            tdp.Process();
            Thread.Sleep(5000);
            Console.Read();
        }
예제 #22
0
 public void ShouldThrowExceptionForNullType()
 {
     Assert.ThrowsException <ArgumentNullException>(
         () =>
     {
         var pdi = new DependencyInjector();
         pdi.CreateAndInjectDependencies((string)null);
     });
 }
예제 #23
0
 public void ShouldCreateBeanWithCreateBeanMethod()
 {
     try
     {
         var pdi = new DependencyInjector();
         (var unnamedBean, var @is) = pdi.CreateBean <UnnamedBean>();
         Assert.IsNotNull(unnamedBean);
         (var _, var is2) = pdi.CreateAndInjectDependencies(unnamedBean, @is
                                                            , rootBeanSpec: new RootBeanSpec(rootBeanName: "BeanWithAName")
                                                            );
         (var bean, var is3) = pdi.CreateAndInjectDependencies <Bean>(is2);
         Assert.IsNotNull(bean?.RefToUnnamedBean);
     }
     catch (DIException dix)
     {
         Console.WriteLine(dix.Diagnostics.AllToString());
         Assert.Fail();
     }
 }
예제 #24
0
        [TestMethod]    // prototype not deferred
        public void ShouldCreateDependenciesForPrototypeRootObject()
        {
            var            pdi = new DependencyInjector();
            InjectionState @is;
            var            rootBean = new AlreadyInstantiated();

            (_, @is) = pdi.CreateAndInjectDependencies(rootBean
                                                       , rootBeanSpec: new RootBeanSpec(scope: BeanScope.Prototype));
            Assert.IsNotNull(rootBean?.Child);
        }
예제 #25
0
 public void ShouldThrowExceptionForNullBeanNameWithTypeParam()
 {
     Assert.ThrowsException <ArgumentNullException>(
         () =>
     {
         var pdi = new DependencyInjector();
         pdi.CreateAndInjectDependencies(this.GetType(), null, null, new RootBeanSpec(rootBeanName: null));
     }
         );
 }
예제 #26
0
 public void ShouldThrowExceptionForNullConstructorName()
 {
     Assert.ThrowsException <ArgumentNullException>(
         () =>
     {
         var pdi = new DependencyInjector();
         pdi.CreateAndInjectDependencies <CheckArgumentsTest>(rootBeanSpec: new RootBeanSpec(rootBeanName: Constants.DefaultBeanName, rootConstructorName: null)
                                                              );
     });
 }
예제 #27
0
        [TestMethod]    // prototype deferred
        public void ShouldNotWarnOfPrototypeRootObjectWithDeferredFlag()
        {
            var            pdi = new DependencyInjector();
            InjectionState @is;
            var            rootBean = new AlreadyInstantiated();

            (_, @is) = pdi.CreateAndInjectDependencies(rootBean
                                                       , rootBeanSpec: new RootBeanSpec(scope: BeanScope.Prototype), deferDepedencyInjection: true);
            // pointless but we don't warn.
            Assert.IsNull(rootBean?.Child);
        }
예제 #28
0
        public void ShouldRejectAttemptToCreateTreeForASecondTimeWithoutState()
        {
            //DependencyInjector pdi = Utils.CreateIOCCinAssembly("MultipleCallsTestData", "Fails");
            DependencyInjector pdi = new DependencyInjector();

            // remote assembly refuses to work (Bad IL Format) in this test despite
            // being identical to the one above
            Assert.ThrowsException <ArgumentException>(() =>
            {
                (object simpleBean, InjectionState injectionState)
                    = pdi.CreateAndInjectDependencies(
                          "IOCCTest.MultipleCallsTestData.Fails",
                          assemblies: new Assembly[] { this.GetType().Assembly });
                (object furtherBean, InjectionState injectionState2)
                    = pdi.CreateAndInjectDependencies(
                          "IOCCTest.MultipleCallsTestData.FurtherFails"
                          , assemblies: new Assembly[] { this.GetType().Assembly });
            }
                                                       );
        }
예제 #29
0
        public void SelfTest()
        {
            DependencyInjector iocc
                = new DependencyInjector();
            //iocc.SetAssemblies("mscorlib", "System", "SimpleIOCContainerTest");
            TestRoot twf
                = iocc.CreateAndInjectDependencies <TestRoot>(
                      assemblies: new System.Reflection.Assembly[] { this.GetType().Assembly }).rootBean;

            Assert.IsNotNull(twf.test);
        }
예제 #30
0
        public (object bean, InjectionState injectionState) Execute(InjectionState injectionState, BeanFactoryArgs args)
        {
            var pdi = new DependencyInjector();

            if (args.FactoryParmeter as Type == typeof(DiverseA))
            {
                return(pdi.CreateAndInjectDependencies <DiverseA>(injectionState));
            }
            else if (args.FactoryParmeter as Type == typeof(DiverseB))
            {
                return(pdi.CreateAndInjectDependencies(args.FactoryParmeter as Type, injectionState));
            }
            else if (args.FactoryParmeter as Type == typeof(DiverseC))
            {
                return(pdi.CreateAndInjectDependencies((args.FactoryParmeter as Type).FullName, injectionState));
            }
            else
            {
                var diverseD = new DiverseD();
                return(pdi.CreateAndInjectDependencies(diverseD, injectionState));
            }
        }