public void DependencyHasPossibleLifestyleMismatch_NestedHybridSingletonToTransient2_ReportsMismatch()
        {
            // Arrange
            Func <bool> selector = () => true;

            var hybridWithDeeplyNestedTransient = Lifestyle.CreateHybrid(selector,
                                                                         Lifestyle.Singleton,
                                                                         Lifestyle.CreateHybrid(selector,
                                                                                                Lifestyle.Singleton,
                                                                                                Lifestyle.CreateHybrid(selector,
                                                                                                                       Lifestyle.Singleton,
                                                                                                                       Lifestyle.CreateHybrid(selector,
                                                                                                                                              Lifestyle.Singleton,
                                                                                                                                              Lifestyle.CreateHybrid(selector,
                                                                                                                                                                     Lifestyle.Singleton,
                                                                                                                                                                     Lifestyle.Transient)))));

            var dependency =
                CreateRelationship(parent: Lifestyle.Singleton, child: hybridWithDeeplyNestedTransient);

            // Act
            bool result = LifestyleMismatchChecker.HasPossibleLifestyleMismatch(dependency);

            // Assert
            Assert.IsTrue(result, "Since the hybrid lifestyle contains a transient lifestyle, this " +
                          "hybrid lifestyle should be considered to be a transient when evaluated on the child.");
        }
        public void DependencyHasPossibleLifestyleMismatch_SingletonToLifetimeWebRequest_ReportsMismatch()
        {
            // Arrange
            var dependency = CreateRelationship(parent: Lifestyle.Singleton, child: Lifestyles.WebRequest);

            // Act
            bool result = LifestyleMismatchChecker.HasPossibleLifestyleMismatch(dependency);

            // Assert
            Assert.IsTrue(result);
        }
        public void DependencyHasPossibleLifestyleMismatch_SingletonToSingleton_DoesNotReportAMismatch()
        {
            // Arrange
            var dependency = CreateRelationship(parent: Lifestyle.Singleton, child: Lifestyle.Singleton);

            // Act
            bool result = LifestyleMismatchChecker.HasPossibleLifestyleMismatch(dependency);

            // Assert
            Assert.IsFalse(result, "Every service can safely depend on a singleton.");
        }
        public void DependencyHasPossibleLifestyleMismatch_SingletonToUnknown_ReportsMismatch()
        {
            // Arrange
            var dependency = CreateRelationship(parent: Lifestyle.Singleton, child: Lifestyle.Unknown);

            // Act
            bool result = LifestyleMismatchChecker.HasPossibleLifestyleMismatch(dependency);

            // Assert
            Assert.IsTrue(result, "The unknown lifestyle will likely be shorter than singleton.");
        }
        public void DependencyHasPossibleLifestyleMismatch_TransientToTransient_DoesNotReportAMismatch()
        {
            // Arrange
            var dependency = CreateRelationship(parent: Lifestyle.Transient, child: Lifestyle.Transient);

            // Act
            bool result = LifestyleMismatchChecker.HasPossibleLifestyleMismatch(dependency);

            // Assert
            Assert.IsFalse(result);
        }
        public void DependencyHasPossibleLifestyleMismatch_UnknownToTransient_ReportsMismatch()
        {
            // Arrange
            var dependency = CreateRelationship(parent: Lifestyle.Unknown, child: Lifestyle.Transient);

            // Act
            bool result = LifestyleMismatchChecker.HasPossibleLifestyleMismatch(dependency);

            // Assert
            Assert.IsTrue(result, "An unknown lifestyle will always be bigger than transient.");
        }
        public void DependencyHasPossibleLifestyleMismatch_TransientToUnknown_DoesNotReportAMismatch()
        {
            // Arrange
            var dependency = CreateRelationship(parent: Lifestyle.Transient, child: Lifestyle.Unknown);

            // Act
            bool result = LifestyleMismatchChecker.HasPossibleLifestyleMismatch(dependency);

            // Assert
            Assert.IsFalse(result, "A transient service can safely depend on any other dependency.");
        }
        public void DependencyHasPossibleLifestyleMismatch_WebRequestToTransient_ReportsMismatch()
        {
            // Arrange
            var dependency = CreateRelationship(parent: Lifestyles.WebRequest, child: Lifestyle.Transient);

            // Act
            bool result = LifestyleMismatchChecker.HasPossibleLifestyleMismatch(dependency);

            // Assert
            Assert.IsTrue(result, "Services can not depend on a dependency with a shorter lifestyle.");
        }
示例#9
0
 public DiagnosticResult[] Analyze(IEnumerable <InstanceProducer> producers)
 {
     return((
                from producer in producers
                from relationship in producer.GetRelationships()
                where LifestyleMismatchChecker.HasPossibleLifestyleMismatch(relationship)
                select new PotentialLifestyleMismatchDiagnosticResult(
                    serviceType: producer.ServiceType,
                    description: BuildRelationshipDescription(relationship),
                    relationship: relationship))
            .ToArray());
 }
        public void DependencyHasPossibleLifestyleMismatch_UnknownToLifetimeScope_ReportsMismatch()
        {
            // Arrange
            var dependency = CreateRelationship(parent: Lifestyle.Unknown, child: Lifestyles.LifetimeScope);

            // Act
            bool result = LifestyleMismatchChecker.HasPossibleLifestyleMismatch(dependency);

            // Assert
            Assert.IsTrue(result, "An unknown lifestyle can not safely depend on a lifestyle that is " +
                          "shorter than singleton, since this unknown lifestyle could act like a singleton.");
        }
        public void DependencyHasPossibleLifestyleMismatch_LifetimeScopeToUnknown_ReportsMismatch()
        {
            // Arrange
            var dependency = CreateRelationship(parent: Lifestyles.LifetimeScope, child: Lifestyle.Unknown);

            // Act
            bool result = LifestyleMismatchChecker.HasPossibleLifestyleMismatch(dependency);

            // Assert
            Assert.IsTrue(result, "A lifestyle longer than transient can not safely depend on an unknown " +
                          "lifestyle, since this unknown lifestyle could act like a transient.");
        }
        public void DependencyHasPossibleLifestyleMismatch_ShortHybridToLongHybrid_ReportsMismatch()
        {
            // Arrange
            var parentHybrid = Lifestyle.CreateHybrid(() => true, Lifestyle.Singleton, Lifestyle.Singleton);
            var childHybrid  = Lifestyle.CreateHybrid(() => true, Lifestyle.Singleton, Lifestyle.Transient);

            var dependency = CreateRelationship(parent: parentHybrid, child: childHybrid);

            // Act
            bool result = LifestyleMismatchChecker.HasPossibleLifestyleMismatch(dependency);

            // Assert
            Assert.IsTrue(result, "Both lifestyles of the parent are longer than those of the child.");
        }
        public void DependencyHasPossibleLifestyleMismatch_HybridToTheSameHybridInstance_DoesNotReportAMismatch()
        {
            // Arrange
            var hybrid = Lifestyle.CreateHybrid(() => true, Lifestyle.Transient, Lifestyle.Singleton);

            var dependency = CreateRelationship(parent: hybrid, child: hybrid);

            // Act
            bool result = LifestyleMismatchChecker.HasPossibleLifestyleMismatch(dependency);

            // Assert
            Assert.IsFalse(result, "Since the both the parent and child have exactly the same lifestyle, " +
                           "there is expected to be no mismatch.");
        }
 public DiagnosticResult[] Analyze(IEnumerable <InstanceProducer> producers)
 {
     return((
                from producer in producers
                where producer.Registration.ShouldNotBeSuppressed(this.DiagnosticType)
                from relationship in producer.GetRelationships()
                let container = producer.Registration.Container
                                where LifestyleMismatchChecker.HasPossibleLifestyleMismatch(container, relationship)
                                select new PotentialLifestyleMismatchDiagnosticResult(
                    serviceType: producer.ServiceType,
                    description: BuildRelationshipDescription(relationship),
                    relationship: relationship))
            .ToArray());
 }
 private static bool HasPossibleLifestyleMismatch(KnownRelationship dependency)
 {
     return(LifestyleMismatchChecker.HasPossibleLifestyleMismatch(new Container(), dependency));
 }