コード例 #1
0
 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;
             }
 }
コード例 #2
0
 public SimpleContainer(ConfigurationRegistry configurationRegistry, ContainerContext containerContext,
     LogError errorLogger)
 {
     Configuration = configurationRegistry;
     implementationSelectors = configurationRegistry.GetImplementationSelectors();
     dependenciesInjector = new DependenciesInjector(this);
     this.containerContext = containerContext;
     this.errorLogger = errorLogger;
 }
コード例 #3
0
 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
     };
 }
コード例 #4
0
 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;
                 }
             }
 }
コード例 #5
0
 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);
 }
コード例 #6
0
 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);
 }
コード例 #7
0
 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);
 }
コード例 #8
0
 public void WriteConstructionLog(ISimpleLogWriter writer, ContainerContext containerContext)
 {
     WriteConstructionLog(new ConstructionLogContext(writer, containerContext.valueFormatters));
 }
コード例 #9
0
 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));
 }
コード例 #10
0
 public string GetConstructionLog(ContainerContext containerContext)
 {
     var writer = new SimpleTextLogWriter();
     WriteConstructionLog(writer, containerContext);
     return writer.GetText();
 }
コード例 #11
0
 public IEnumerable<object> GetAllValues(ContainerContext containerContext)
 {
     CheckStatusIsGood(containerContext);
     return typedArray ?? (typedArray = instances.Select(x => x.Instance).CastToObjectArrayOf(Type));
 }