protected virtual void AddDependency(DependencyModel dependency) { if (dependency.TargetType != null) { if (dependency.TargetType == typeof(IKernel)) { return; } if (HasValidComponent(dependency.TargetType)) { AddGraphDependency(Kernel.GetHandler(dependency.TargetType).ComponentModel); return; } DependenciesByService.Add(dependency.TargetType); } else { if (HasValidComponent(dependency.DependencyKey)) { AddGraphDependency(Kernel.GetHandler(dependency.DependencyKey).ComponentModel); return; } DependenciesByKey.Add(dependency.DependencyKey, String.Empty); } // This handler is considered invalid // until dependencies are satisfied SetNewState(HandlerState.WaitingDependency); // Register itself on the kernel // to be notified if the dependency is satified Kernel.HandlerRegistered += new HandlerDelegate(DependencySatisfied); }
protected void OnAddedAsChildKernel(object sender, EventArgs e) { if (DependenciesByKey.Count == 0 && DependenciesByService.Count == 0) { return; } bool stateChanged = false; Type[] services = new Type[DependenciesByService.Count]; DependenciesByService.CopyTo(services, 0); foreach (Type service in services) { if (Kernel.Parent.HasComponent(service)) { IHandler handler = Kernel.Parent.GetHandler(service); DependencySatisfied(handler, ref stateChanged); } } String[] keys = new String[DependenciesByKey.Count]; DependenciesByKey.Keys.CopyTo(services, 0); foreach (String key in keys) { if (Kernel.Parent.HasComponent(key)) { IHandler handler = Kernel.Parent.GetHandler(key); DependencySatisfied(handler, ref stateChanged); } } }
/// <summary> /// Invoked by the kernel /// when one of registered dependencies were satisfied by /// new components registered. /// </summary> /// <param name="handler"></param> protected virtual void DependencySatisfied(IHandler handler, ref bool stateChanged) { Type[] services = (Type[])DependenciesByService.ToArray(typeof(Type)); foreach (Type service in services) { if (HasValidComponent(service)) { DependenciesByService.Remove(service); AddGraphDependency(handler.ComponentModel); } } foreach (String compKey in DependenciesByKey.Keys) { if (HasValidComponent(compKey)) { DependenciesByKey.Remove(compKey); AddGraphDependency(handler.ComponentModel); } } if (DependenciesByService.Count == 0 && DependenciesByKey.Count == 0) { SetNewState(HandlerState.Valid); stateChanged = true; Kernel.HandlerRegistered -= new HandlerDelegate(DependencySatisfied); // All components with dependencies (that happened to be // We don't need these anymore dependenciesByKey = null; dependenciesByService = null; } }