/// <summary> /// Gets a service. /// </summary> /// <param name="serviceType">The type of the service to be found.</param> /// <param name="ensureExists">If true, will throw an exception if the service is not found.</param> /// <returns>The service instance, if present; null if not (and ensureExists is false).</returns> /// <exception cref="ServiceMissingException">Thrown if ensureExists is true and the service is not found.</exception> public object Get(Type serviceType, bool ensureExists) { Guard.ArgumentNotNull(serviceType, "serviceType"); if (Contains(serviceType, SearchMode.Local)) { object result = locator.Get(new DependencyResolutionLocatorKey(serviceType, null)); DemandAddPlaceholder placeholder = result as DemandAddPlaceholder; if (placeholder != null) { Remove(serviceType); result = Build(placeholder.TypeToCreate, serviceType, null); } return(result); } if (parent != null) { return(parent.Get(serviceType, ensureExists)); } if (ensureExists) { throw new ServiceMissingException(serviceType); } return(null); }
private TItem Build(Type typeToBuild, string idToBuild, object item) { if (idToBuild != null && Contains(idToBuild, SearchMode.Local, true)) { throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Properties.Resources.DuplicateID, idToBuild)); } if (item != null && Object.ReferenceEquals(item, locator.Get(new DependencyResolutionLocatorKey(typeof(WorkItem), null)))) { throw new ArgumentException(Properties.Resources.CannotAddWorkItemToItself, "item"); } if (item == null) { item = BuildFirstTimeItem(typeToBuild, idToBuild, null); } else if (!container.Contains(item)) { item = BuildFirstTimeItem(typeToBuild, idToBuild, item); } else { BuildRepeatedItem(typeToBuild, idToBuild, item); } return((TItem)item); }
/// <summary> /// Initializes an instance of the <see cref="ManagedObjectCollection{TItem}"/> class. /// </summary> public ManagedObjectCollection( ILifetimeContainer container, IReadWriteLocator locator, IBuilder <WCSFBuilderStage> builder, SearchMode searchMode, IndexerCreationDelegate indexerCreationDelegate, Predicate <TItem> filter, ManagedObjectCollection <TItem> parentCollection) { _lifetimeContainer = container; _locator = locator; _builder = builder; _searchMode = searchMode; _indexerCreationDelegate = indexerCreationDelegate; _filter = filter; _parentCollection = parentCollection; _container = locator.Get <CompositionContainer>(new DependencyResolutionLocatorKey(typeof(CompositionContainer), null)); if (_container != null) { _container.ObjectAdded += new EventHandler <DataEventArgs <object> >(Container_ItemAdded); _container.ObjectRemoved += new EventHandler <DataEventArgs <object> >(Container_ItemRemoved); } }
public void OnDisposed(object sender, EventArgs e) { locator.Remove(key); ILifetimeContainer lifetime = locator.Get <ILifetimeContainer>(); if (lifetime != null) { lifetime.Remove(sender); } }
/// <summary> /// Initializes an instance of the <see cref="ManagedObjectCollection{TItem}"/> class. /// </summary> public ManagedObjectCollection(ILifetimeContainer container, IReadWriteLocator locator, IBuilder <BuilderStage> builder, SearchMode searchMode, IndexerCreationDelegate indexerCreationDelegate, Predicate <TItem> filter, ManagedObjectCollection <TItem> parentCollection) { this.container = container; this.locator = locator; this.builder = builder; this.searchMode = searchMode; this.indexerCreationDelegate = indexerCreationDelegate; this.filter = filter; this.parentCollection = parentCollection; this.workItem = locator.Get <WorkItem>(new DependencyResolutionLocatorKey(typeof(WorkItem), null)); if (this.workItem != null) { this.workItem.ObjectAdded += new EventHandler <DataEventArgs <object> >(WorkItem_ItemAdded); this.workItem.ObjectRemoved += new EventHandler <DataEventArgs <object> >(WorkItem_ItemRemoved); } }