private static IEnumerable <InstanceProducer> GetSelfAndDependentProducers(InstanceProducer producer, HashSet <InstanceProducer> set = null) { set = set ?? new HashSet <InstanceProducer>(ReferenceEqualityComparer <InstanceProducer> .Instance); // Prevent stack overflow exception in case the graph is cyclic. if (set.Contains(producer)) { yield break; } // Return self yield return(set.AddReturn(producer)); // Return dependent producers foreach (var relationship in producer.GetRelationships()) { yield return(set.AddReturn(relationship.Dependency)); foreach (var dependentProducer in GetSelfAndDependentProducers(relationship.Dependency, set)) { yield return(set.AddReturn(dependentProducer)); } } }
public void GetRelationships_ForInstanceProducerThatSharesThRegistrationWithAnOtherProducer_HasItsOwnSetOfRegistrations() { // Arrange var container = ContainerFactory.New(); var registration = Lifestyle.Transient.CreateRegistration <OneAndTwo, OneAndTwo>(container); container.AddRegistration(typeof(IOne), registration); container.AddRegistration(typeof(ITwo), registration); // Decorator to wrap around IOne (and not ITwo). container.RegisterDecorator(typeof(IOne), typeof(OneDecorator)); InstanceProducer twoProducer = container.GetRegistration(typeof(ITwo)); container.Verify(); // Act KnownRelationship[] relationships = twoProducer.GetRelationships(); // Assert Assert.IsFalse(relationships.Any(), "The InstanceProducer for ITwo was expected to have no relationships. Current: " + relationships.Select(r => r.ImplementationType).ToFriendlyNamesText()); }
private static string VisualizeIndentedObjectGraph(this InstanceProducer producer, int indentingDepth, HashSet <InstanceProducer> set) { var visualizedDependencies = from relationship in producer.GetRelationships() let dependency = relationship.Dependency let subGraph = dependency.VisualizeIndentedObjectSubGraph(indentingDepth + 1, set) select Environment.NewLine + subGraph; return(string.Format(CultureInfo.InvariantCulture, "{0}{1}({2})", new string(' ', indentingDepth * 4), producer.ImplementationType.ToFriendlyName(), string.Join(",", visualizedDependencies))); }
private static IEnumerable <InstanceProducer> GetSelfAndDependentProducers(InstanceProducer producer) { // Return self yield return(producer); // Return dependent producers foreach (var relationship in producer.GetRelationships()) { yield return(relationship.Dependency); foreach (var dependentProducer in GetSelfAndDependentProducers(relationship.Dependency)) { yield return(dependentProducer); } } }
private static void VisualizeIndentedObjectGraph(this InstanceProducer producer, int indentingDepth, bool last, HashSet <InstanceProducer> set, ObjectGraphBuilder objectGraphBuilder) { objectGraphBuilder.BeginInstanceProducer(producer); var dependencies = producer .GetRelationships() .Select(relationship => relationship.Dependency) .ToList(); for (int counter = 0; counter < dependencies.Count; counter++) { var dependency = dependencies[counter]; dependency.VisualizeIndentedObjectSubGraph(indentingDepth + 1, counter + 1 == dependencies.Count, set, objectGraphBuilder); } objectGraphBuilder.EndInstanceProducer(last); }
private static IEnumerable <string> VisualizeInlinedDependencies( this InstanceProducer producer, int maxLength) { var relationships = new Stack <KnownRelationship>(producer.GetRelationships().Reverse()); if (!relationships.Any()) { yield break; } while (maxLength > 0 && relationships.Any()) { var relationship = relationships.Pop(); bool lastDependency = !relationships.Any(); string childGraph = relationship.Dependency.VisualizeInlinedAndTruncatedObjectGraph( !lastDependency ? maxLength - ", ...".Length : maxLength); maxLength -= childGraph.Length; bool displayingThisGraphWillCauseAnOverflow = (!lastDependency && maxLength < ", ...".Length) || maxLength < 0; if (displayingThisGraphWillCauseAnOverflow) { yield return("..."); yield break; } else { yield return(childGraph); } maxLength -= ", ".Length; } if (relationships.Any()) { yield return("..."); } }
protected ServiceTypeDecoratorInfo GetServiceTypeInfo(Expression originalExpression, InstanceProducer registeredProducer, Type registeredServiceType, Lifestyle lifestyle) { // registeredProducer.ServiceType and registeredServiceType are different when called by // container uncontrolled decorator. producer.ServiceType will be IEnumerable<T> and // registeredServiceType will be T. Func <Type, InstanceProducer> producerBuilder = implementationType => { // The InstanceProducer created here is used to do correct diagnostics. We can't return the // registeredProducer here, since the lifestyle of the original producer can change after // the ExpressionBuilt event has ran, which means that this would invalidate the diagnostic // results. var registration = new ExpressionRegistration(originalExpression, implementationType, lifestyle, this.Container); registration.ReplaceRelationships(registeredProducer.GetRelationships()); return(new InstanceProducer(registeredServiceType, registration)); }; return(this.GetServiceTypeInfo(originalExpression, registeredProducer, producerBuilder)); }
private static void BuildDependencies(Container container, InstanceProducer producer, List <DependencyInfo> acc, HashSet <InstanceProducer> visited) { if (visited.Contains(producer)) { return; } visited.Add(producer); foreach (var rel in producer.GetRelationships()) { var child = rel.Dependency.Registration.ImplementationType; if (child.IsArray) { var interfaceType = child.GetElementType(); foreach (var dep in container.GetAllInstances(interfaceType)) { acc.Add(new DependencyInfo(producer.ServiceType, dep.GetType(), interfaceType)); BuildDependencies(container, dep.GetType(), acc, visited); } } else if (child.IsGenericType && child.GetGenericTypeDefinition() == typeof(IEnumerable <>)) { var interfaceType = child.GetGenericArguments()[0]; foreach (var dep in container.GetAllInstances(interfaceType)) { acc.Add(new DependencyInfo(producer.ServiceType, dep.GetType(), interfaceType)); BuildDependencies(container, dep.GetType(), acc, visited); } } else { acc.Add(new DependencyInfo(producer.Registration.ImplementationType, rel.Dependency.Registration.ImplementationType, rel.Dependency.ServiceType)); BuildDependencies(container, rel.Dependency, acc, visited); } } }
private static IEnumerable <InstanceProducer> GetSelfAndDependentProducers(InstanceProducer producer, HashSet <InstanceProducer> set) { // Prevent stack overflow exception in case the graph is cyclic. if (set.Contains(producer)) { yield break; } // Return self yield return(set.AddReturn(producer)); // Return dependent producers foreach (var relationship in producer.GetRelationships()) { if (relationship.UseForVerification) { foreach (var dependentProducer in GetSelfAndDependentProducers(relationship.Dependency, set)) { yield return(set.AddReturn(dependentProducer)); } } } }
private static IEnumerable<InstanceProducer> GetSelfAndDependentProducers(InstanceProducer producer, HashSet<InstanceProducer> set = null) { set = set ?? new HashSet<InstanceProducer>(ReferenceEqualityComparer<InstanceProducer>.Instance); // Prevent stack overflow exception in case the graph is cyclic. if (set.Contains(producer)) { yield break; } // Return self yield return set.AddReturn(producer); // Return dependent producers foreach (var relationship in producer.GetRelationships()) { yield return set.AddReturn(relationship.Dependency); foreach (var dependentProducer in GetSelfAndDependentProducers(relationship.Dependency, set)) { yield return set.AddReturn(dependentProducer); } } }