Exemplo n.º 1
0
        public void GenerateGraphWithNoDependencies()
        {
            Mock <IRegistrationCache> moqCache = new Mock <IRegistrationCache>();

            var testDetails          = GetSimpleRegDetails();
            DependencyGenerator ctor = new DependencyGenerator(moqCache.Object);

            ctor.BuildGraph(testDetails);

            Assert.NotNull(testDetails.Instructions.ConstructorToUse);
        }
Exemplo n.º 2
0
        public void GenerateGraphWithNoUseableConstructor()
        {
            Mock <IRegistrationCache> moqCache = new Mock <IRegistrationCache>();
            var testDetails = new RegistrationDetails
            {
                RegisteredType = typeof(ISimpleInterface),
                ResolvedType   = typeof(UnresolveableConstructorClass),
                Lifetime       = LifetimeEnum.Transient
            };

            DependencyGenerator ctor = new DependencyGenerator(moqCache.Object);

            InvalidOperationException ex = Assert.Throws <InvalidOperationException>(() => { ctor.BuildGraph(testDetails); });

            Assert.NotNull(ex);
        }
Exemplo n.º 3
0
        public object Resolve(RegistrationDetails details)
        {
            // Ensure we don't already have a type. Don't build instances or dependency graphs for types already created.
            // Just short circuit the thing

            if (details.IsCreated)
            {
                return(_allManagers[details.Lifetime].CreateType(details));
            }

            // Ensure the dependency graph for a given object is built
            if (details.Instructions == null)
            {
                DependencyGenerator buildDependencies = new DependencyGenerator();
                buildDependencies.BuildGraph(details);
            }

            return(_allManagers[details.Lifetime].CreateType(details));
        }
Exemplo n.º 4
0
        public void GenerateGraphWithRegisteredDependentConstructor()
        {
            Mock <IRegistrationCache> moqCache = new Mock <IRegistrationCache>();

            moqCache.Setup(t => t.Contains(typeof(ISimpleInterface))).Returns(true);
            moqCache.Setup(t => t.Get(typeof(ISimpleInterface))).Returns(GetSimpleRegDetails());

            DependencyGenerator ctor = new DependencyGenerator(moqCache.Object);

            var complexDetails = GetComplexDetails();

            ctor.BuildGraph(complexDetails);

            Assert.NotNull(complexDetails.Instructions.ConstructorToUse);
            Assert.Equal(1, complexDetails.Instructions.ConstructorToUse.GetParameters().Length);
            Assert.NotNull(complexDetails.Instructions);
            Assert.NotNull(complexDetails.Instructions.Dependencies);
            Assert.Equal(complexDetails.Instructions.Dependencies.Count, 1);
            Assert.Equal(complexDetails.Instructions.Dependencies[0].TypeToCreate, typeof(SimpleClass));
        }
Exemplo n.º 5
0
        public void GenerateGraphComplexClassWithDefaultConstructorWhenSimpleClassNotRegistered()
        {
            Mock <IRegistrationCache> moqCache = new Mock <IRegistrationCache>();

            moqCache.Setup(t => t.Contains(typeof(ISimpleInterface))).Returns(false);

            RegistrationDetails details = new RegistrationDetails
            {
                Lifetime       = LifetimeEnum.Transient,
                RegisteredType = typeof(IComplexInterface),
                ResolvedType   = typeof(ComplexClassWithDefaultConstructor)
            };

            DependencyGenerator generator = new DependencyGenerator(moqCache.Object);

            generator.BuildGraph(details);

            Assert.NotNull(details.Instructions);
            Assert.Equal(0, details.Instructions.ConstructorToUse.GetParameters().Length);
            Assert.Equal(0, details.Instructions.Dependencies.Count());
        }