public void EnsureInitialized(ContainerService service, ContainerContext containerContext, ContainerService root) { if (!Owned) return; var componentInstance = Instance as IInitializable; if (componentInstance == null) return; if (!initialized) lock (this) if (!initialized) { var name = new ServiceName(Instance.GetType(), service.UsedContracts); if (containerContext.infoLogger != null) containerContext.infoLogger(name, "initialize started"); try { componentInstance.Initialize(); } catch (Exception e) { throw new SimpleContainerException(string.Format("exception initializing {0}\r\n\r\n{1}", name, root.GetConstructionLog(containerContext)), e); } if (containerContext.infoLogger != null) containerContext.infoLogger(name, "initialize finished"); initialized = true; } }
public SimpleContainer(ConfigurationRegistry configurationRegistry, ContainerContext containerContext, LogError errorLogger) { Configuration = configurationRegistry; implementationSelectors = configurationRegistry.GetImplementationSelectors(); dependenciesInjector = new DependenciesInjector(this); this.containerContext = containerContext; this.errorLogger = errorLogger; }
public SimpleContainer(GenericsAutoCloser genericsAutoCloser, ConfigurationRegistry configurationRegistry, TypesList typesList, LogError errorLogger, LogInfo infoLogger, Dictionary<Type, Func<object, string>> valueFormatters) { Configuration = configurationRegistry; implementationSelectors = configurationRegistry.GetImplementationSelectors(); this.genericsAutoCloser = genericsAutoCloser; this.typesList = typesList; dependenciesInjector = new DependenciesInjector(this); this.errorLogger = errorLogger; containerContext = new ContainerContext { infoLogger = infoLogger, typesList = typesList, valueFormatters = valueFormatters }; }
public void EnsureInitialized(ContainerContext containerContext, ContainerService root) { if (Status != ServiceStatus.Ok) return; if (!initialized) lock (lockObject) if (!initialized) { initializing = true; try { if (dependencies != null) foreach (var dependency in dependencies) if (dependency.ContainerService != null) dependency.ContainerService.EnsureInitialized(containerContext, root); foreach (var instance in instances) instance.EnsureInitialized(this, containerContext, root); initialized = true; } finally { initializing = false; } } }
public void LinkTo(ContainerContext containerContext, ContainerService childService, string comment) { var dependency = childService.GetLinkedDependency(containerContext); dependency.Comment = comment; AddDependency(dependency, true); UnionUsedContracts(childService); if (target.Status.IsGood()) foreach (var instance in childService.instances) if (!instances.Contains(instance)) instances.Add(instance); }
private ServiceDependency GetLinkedDependency(ContainerContext containerContext) { if (Status == ServiceStatus.Ok) return ServiceDependency.Service(this, GetAllValues(containerContext)); if (Status == ServiceStatus.NotResolved) return ServiceDependency.NotResolved(this); return ServiceDependency.ServiceError(this); }
private void CheckStatusIsGood(ContainerContext containerContext) { if (Status.IsGood()) return; var errorTarget = SearchForError(); if (errorTarget == null) throw new InvalidOperationException("assertion failure: can't find error target"); var constructionLog = GetConstructionLog(containerContext); var currentConstructionException = errorTarget.service != null ? errorTarget.service.constructionException : null; var message = currentConstructionException != null ? string.Format("service [{0}] construction exception", errorTarget.service.Type.FormatName()) : (errorTarget.service != null ? errorTarget.service.comment : errorTarget.dependency.Comment); throw new SimpleContainerException(message + "\r\n\r\n" + constructionLog, currentConstructionException); }
public void WriteConstructionLog(ISimpleLogWriter writer, ContainerContext containerContext) { WriteConstructionLog(new ConstructionLogContext(writer, containerContext.valueFormatters)); }
public object GetSingleValue(ContainerContext containerContext, bool hasDefaultValue, object defaultValue) { CheckStatusIsGood(containerContext); if (instances.Length == 1) return instances[0].Instance; if (instances.Length == 0 && hasDefaultValue) return defaultValue; string message; if (instances.Length == 0) { var targetType = typeof (Delegate).IsAssignableFrom(Type) ? Type.DeclaringType : Type; message = string.Format("no instances for [{0}]", targetType.FormatName()); var notResolvedRoot = SearchForNotResolvedRoot(); if (notResolvedRoot != this) message += string.Format(" because [{0}] has no instances", notResolvedRoot.Type.FormatName()); } else message = FormatManyImplementationsMessage(); var assemblies = containerContext.typesList.GetAssemblies() .OrderBy(x => x.GetName().Name) .Select(x => "\t" + x.GetName().Name).JoinStrings("\r\n"); throw new SimpleContainerException(string.Format("{0}\r\n\r\n{1}\r\nscanned assemblies\r\n{2}", message, GetConstructionLog(containerContext), assemblies)); }
public string GetConstructionLog(ContainerContext containerContext) { var writer = new SimpleTextLogWriter(); WriteConstructionLog(writer, containerContext); return writer.GetText(); }
public IEnumerable<object> GetAllValues(ContainerContext containerContext) { CheckStatusIsGood(containerContext); return typedArray ?? (typedArray = instances.Select(x => x.Instance).CastToObjectArrayOf(Type)); }