/// <summary> /// Enumerates through all seen types and retrieves a KeyValuePair for each. /// </summary> /// <returns></returns> public IEnumerator <KeyValuePair <Type, object> > GetEnumerator() { IReadableLocator currentLocator = _locator; Dictionary <Type, object> seenTypes = new Dictionary <Type, object>(); do { foreach (KeyValuePair <object, object> pair in currentLocator) { if (pair.Key is DependencyResolutionLocatorKey) { DependencyResolutionLocatorKey depKey = (DependencyResolutionLocatorKey)pair.Key; if (depKey.ID == null) { Type type = depKey.Type; if (!seenTypes.ContainsKey(type)) { seenTypes[type] = String.Empty; yield return(new KeyValuePair <Type, object>(type, pair.Value)); } } } } } while ((currentLocator = currentLocator.ParentLocator) != null); }
/// <summary> /// Removes a service registration from the <see cref="CompositionContainer"/>. /// </summary> /// <param name="serviceType">The service type to remove.</param> public void Remove(Type serviceType) { Guard.ArgumentNotNull(serviceType, "serviceType"); DependencyResolutionLocatorKey key = new DependencyResolutionLocatorKey(serviceType, null); if (_locator.Contains(key, SearchMode.Local)) { object serviceInstance = _locator.Get(key, SearchMode.Local); bool isLastInstance = true; _locator.Remove(new DependencyResolutionLocatorKey(serviceType, null)); foreach (KeyValuePair <object, object> kvp in _locator) { if (ReferenceEquals(kvp.Value, serviceInstance)) { isLastInstance = false; break; } } if (isLastInstance) { _builder.TearDown(_locator, serviceInstance); _container.Remove(serviceInstance); if (!(serviceInstance is DemandAddPlaceholder)) { OnRemoved(serviceInstance); } } } }
/// <summary> /// Gets an enumerator which returns all the objects in the container, along with their /// names. /// </summary> /// <returns>The enumerator.</returns> public IEnumerator <KeyValuePair <string, TItem> > GetEnumerator() { IReadableLocator currentLocator = _locator; Dictionary <string, object> seenNames = new Dictionary <string, object>(); do { foreach (KeyValuePair <object, object> pair in currentLocator) { if (pair.Key is DependencyResolutionLocatorKey) { DependencyResolutionLocatorKey depKey = (DependencyResolutionLocatorKey)pair.Key; if (depKey.ID != null && pair.Value is TItem) { string id = depKey.ID; TItem value = (TItem)pair.Value; if (!seenNames.ContainsKey(id) && (_filter == null || _filter(value))) { seenNames[id] = String.Empty; yield return(new KeyValuePair <string, TItem>(id, value)); } } } } } while (_searchMode == SearchMode.Up && (currentLocator = currentLocator.ParentLocator) != null); }
public override object GetValue(IBuilderContext context) { DependencyResolutionLocatorKey key = new DependencyResolutionLocatorKey(typeof(ITraceSourceCatalogService), null); ITraceSourceCatalogService svc = (ITraceSourceCatalogService)context.Locator.Get(key); return(svc != null?svc.GetTraceSource(sourceName) : null); }
private TItem Get(string id, SearchMode searchMode, bool filtered) { Guard.ArgumentNotNull(id, "id"); foreach (KeyValuePair <object, object> pair in _locator) { if (pair.Key is DependencyResolutionLocatorKey) { DependencyResolutionLocatorKey depKey = (DependencyResolutionLocatorKey)pair.Key; if (Equals(depKey.ID, id)) { TItem result = (TItem)pair.Value; if (!filtered || _filter == null || _filter(result)) { return(result); } } } } if (searchMode == SearchMode.Up && _parentCollection != null) { return(_parentCollection.Get(id)); } return(default(TItem)); }
/// <summary> /// 定位指定服务对象(不指定名称) /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public T Locate <T>() { string key = getdefault(typeof(T)); DependencyResolutionLocatorKey locatorkey = new DependencyResolutionLocatorKey(typeof(T), key); return(locator.Get <T>(locatorkey)); }
/// <summary> /// 创建指定业务对象并缓存 /// </summary> /// <typeparam name="T">业务服务类型</typeparam> /// <param name="servicekey">服务名称</param> /// <param name="parameters">参数</param> /// <returns></returns> public T BuildUpAndSaveObject <T>(string servicekey, params object[] parameters) { T result = this.BuildUp <T>(servicekey); DependencyResolutionLocatorKey locatorkey = new DependencyResolutionLocatorKey(typeof(T), servicekey); locator.Add(locatorkey, result); return(result); }
private void LocateWorkItem(Type workItemType) { DependencyResolutionLocatorKey key = new DependencyResolutionLocatorKey(workItemType, null); if (!locator.Contains(key, SearchMode.Local)) { locator.Add(key, this); } }
/// <summary> /// 创建指定业务对象并缓存 /// </summary> /// <typeparam name="T">业务服务类型</typeparam> /// <param name="parameters">参数</param> /// <returns></returns> public T BuildUpAndSaveObject <T>(params object[] parameters) { T result = this.BuildUp <T>(); string key = getdefault(typeof(T)); DependencyResolutionLocatorKey locatorkey = new DependencyResolutionLocatorKey(typeof(T), key); locator.Add(locatorkey, result); return(result); }
private void LocateContainer(Type containerType) { DependencyResolutionLocatorKey key = new DependencyResolutionLocatorKey(containerType, null); if (!_locator.Contains(key, SearchMode.Local)) { _locator.Add(key, this); } }
public void CanRegisterObjectByTypeAndID() { object o = new object(); IReadWriteLocator locator = new Locator(); DependencyResolutionLocatorKey key = new DependencyResolutionLocatorKey(typeof(object), "foo"); locator.Add(key, o); Assert.IsNotNull(locator.Get(key)); Assert.AreSame(o, locator.Get(key)); }
/// <summary> /// 更新业务对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="obj"></param> public void UpdateObject <T>(string key, T obj) { DependencyResolutionLocatorKey dkey = new DependencyResolutionLocatorKey(typeof(T), key); if (locator.Contains(dkey)) { locator.Remove(dkey); } locator.Add(dkey, obj); }
/// <summary> /// Maps one type/ID pair to another, using the locator to find the /// current <see cref="CompositionContainer"/>. The container holds /// the current set of type mappings. /// </summary> /// <param name="locator">The <see cref="IReadableLocator"/> being used in the /// current BuildUp operation.</param> /// <param name="incomingTypeIdPair"><see cref="DependencyResolutionLocatorKey"/> that identifies /// the type of the current BuildUp operation.</param> /// <returns>The new type to return; returns the original key if no type mapping is defined.</returns> public DependencyResolutionLocatorKey Map( IReadableLocator locator, DependencyResolutionLocatorKey incomingTypeIdPair) { DependencyResolutionLocatorKey result = incomingTypeIdPair; CompositionContainer container = GetCompositionContainerFromLocator(locator); if (container != null) { result = new DependencyResolutionLocatorKey(container.GetMappedType(result.Type), incomingTypeIdPair.ID); } return(result); }
private void WorkItem_ItemRemoved(object sender, DataEventArgs <object> e) { if (Removed != null && e.Data is TItem) { TItem value = (TItem)e.Data; DependencyResolutionLocatorKey key = FindObjectInLocator(value); if (key != null && key.ID != null && PassesFilter(value)) { Removed(this, new DataEventArgs <TItem>(value)); } } }
private Type GetTypeFromKey(object key) { DependencyResolutionLocatorKey locationKey = key as DependencyResolutionLocatorKey; if (locationKey != null) { return(locationKey.Type); } if (key is Type) { return(key as Type); } return(null); }
public void PolicyShouldRequestTypeMappingFromContainer() { TestableRootCompositionContainer container = new TestableRootCompositionContainer(); container.RegisterTypeMapping <IFoo, Foo>(); ContainerAwareTypeMappingPolicy policy = new ContainerAwareTypeMappingPolicy(); string requestId = "My mapped object"; DependencyResolutionLocatorKey requestKey = new DependencyResolutionLocatorKey(typeof(IFoo), requestId); DependencyResolutionLocatorKey result = policy.Map(container.Locator, requestKey); Assert.AreEqual(typeof(Foo), result.Type); Assert.AreEqual(requestId, result.ID); }
/// <summary> /// Build up the requested object. /// </summary> /// <param name="context">Current build context</param> /// <param name="typeToBuild">Type of object to build.</param> /// <param name="existing">Existing object (if any)</param> /// <param name="idToBuild">Id of object to build.</param> /// <returns>The constructed object.</returns> public override object BuildUp( IBuilderContext context, Type typeToBuild, object existing, string idToBuild) { if (existing == null) { IContainerAwareTypeMappingPolicy policy = context.Policies.Get <IContainerAwareTypeMappingPolicy>(typeToBuild, idToBuild); if (policy != null) { DependencyResolutionLocatorKey key = policy.Map(context.Locator, new DependencyResolutionLocatorKey(typeToBuild, idToBuild)); return(base.BuildUp(context, key.Type, existing, idToBuild)); } } return(base.BuildUp(context, typeToBuild, existing, idToBuild)); }
public void PolicyCallsUpToParentContainerToGetMapping() { TestableRootCompositionContainer container = new TestableRootCompositionContainer(); container.RegisterTypeMapping <IFoo, Foo>(); CompositionContainer child = container.Containers.AddNew <CompositionContainer>(); ContainerAwareTypeMappingPolicy policy = new ContainerAwareTypeMappingPolicy(); string requestId = "My mapped object"; DependencyResolutionLocatorKey requestKey = new DependencyResolutionLocatorKey(typeof(IFoo), requestId); DependencyResolutionLocatorKey result = policy.Map(child.Locator, requestKey); Assert.AreEqual(typeof(Foo), result.Type); Assert.AreEqual(requestId, result.ID); }
/// <summary> /// 定位对象 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public T Locate <T>() { ServiceDesc desc = packagesearcher.SearchDefault(typeof(T)); if (desc == null) { return(default(T)); } ServiceKey key = new ServiceKey(desc); //构造定位器关键字 DependencyResolutionLocatorKey locatorkey = new DependencyResolutionLocatorKey(desc.ImpleType, key.ID); if (this.locator.Contains(locatorkey)) { return(this.locator.Get <T>(locatorkey)); } return(default(T)); }
public override object GetValue(IBuilderContext context) { DependencyResolutionLocatorKey key = new DependencyResolutionLocatorKey(typeof(WorkItem), null); WorkItem wi = (WorkItem)context.Locator.Get(key); if (id != null) { return(wi.State[id]); } foreach (string stateID in wi.State.Keys) { if (type.IsAssignableFrom(wi.State[stateID].GetType())) { return(wi.State[stateID]); } } return(null); }
public void StrategyShouldDoTypeMapping() { TestableRootCompositionContainer root = new TestableRootCompositionContainer(); root.RegisterTypeMapping <IFoo, Foo>(); ContainerAwareTypeMappingStrategy strategy = new ContainerAwareTypeMappingStrategy(); MockBuilderContext builderContext = new MockBuilderContext(strategy, new ReturnRequestedTypeStrategy()); builderContext.Policies.SetDefault <IContainerAwareTypeMappingPolicy>( new ContainerAwareTypeMappingPolicy()); builderContext.Locator = root.Locator; object result = strategy.BuildUp(builderContext, typeof(IFoo), null, "Test object"); DependencyResolutionLocatorKey key = result as DependencyResolutionLocatorKey; Assert.IsNotNull(key); Assert.AreEqual(typeof(Foo), key.Type); }
/// <summary> /// Adds a service that will not be created until the first time it is requested. /// </summary> /// <param name="serviceType">The type of service</param> /// <param name="registerAs">The type to register the service as</param> public void AddOnDemand(Type serviceType, Type registerAs) { Guard.ArgumentNotNull(serviceType, "serviceType"); if (registerAs == null) { registerAs = serviceType; } DependencyResolutionLocatorKey key = new DependencyResolutionLocatorKey(registerAs, null); if (locator.Contains(key, SearchMode.Local)) { throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Properties.Resources.DuplicateService, registerAs.FullName)); } DemandAddPlaceholder placeholder = new DemandAddPlaceholder(serviceType); locator.Add(key, placeholder); container.Add(placeholder); }
/// <summary> /// Determines if at least one object with the given <paramref name="objectId"/> exists /// in the container. /// </summary> /// <param name="objectId">The identifier to search for.</param> /// <returns><see langword="true"/> if the key exists in the container; <see langword="false"/> otherwise.</returns> /// <remarks> /// Container-managed objects are always singletons by id and type. Therefore, /// <paramref name="objectId"/> is not guaranteed to be unique in the container, /// only within that pair id/type. /// </remarks> public bool Contains(string objectId) { IReadableLocator results = locator.FindBy(delegate(KeyValuePair <object, object> entry) { string stringKey = entry.Key as string; if (stringKey != null && stringKey == objectId) { return(true); } DependencyResolutionLocatorKey key = entry.Key as DependencyResolutionLocatorKey; if (key != null && key.ID == objectId) { return(true); } return(false); }); return(results.Count > 0); }
/// <summary> /// Determines if at least one object of the given <paramref name="type"/> exists /// in the container. /// </summary> /// <param name="type">The type of the object to search for.</param> /// <returns><see langword="true"/> if the key exists in the container; <see langword="false"/> otherwise.</returns> public bool Contains(Type type) { IReadableLocator results = locator.FindBy(delegate(KeyValuePair <object, object> entry) { Type typeKey = entry.Key as Type; if (typeKey != null && typeKey == type) { return(true); } DependencyResolutionLocatorKey key = entry.Key as DependencyResolutionLocatorKey; if (key != null && key.Type == type) { return(true); } return(false); }); return(results.Count > 0); }
/// <summary> /// Implementation of <see cref="IBuilderStrategy.BuildUp"/>. /// </summary> /// <param name="context">The build context.</param> /// <param name="typeToBuild">The type of the object being built.</param> /// <param name="existing">The existing instance of the object.</param> /// <param name="idToBuild">The ID of the object being built.</param> /// <returns>The built object.</returns> public override object BuildUp(IBuilderContext context, Type typeToBuild, object existing, string idToBuild) { DependencyResolutionLocatorKey key = null; ILifetimeContainer lifeTime = GetLifetime(context); if (context.Locator != null) { key = new DependencyResolutionLocatorKey(typeToBuild, idToBuild); if (context.Locator.Contains(key, SearchMode.Local)) { return(context.Locator.Get(key)); } } existing = base.BuildUp(context, typeToBuild, existing, idToBuild); if (context.Locator != null && lifeTime != null && IsSingleton(context, key)) { context.Locator.Add(key, existing); lifeTime.Add(existing); } return(existing); }
public DisposedHandlerClosure(IReadWriteLocator locator, DependencyResolutionLocatorKey key) { this.locator = locator; this.key = key; }
private static bool IsSingleton(IBuilderContext context, DependencyResolutionLocatorKey key) { ISingletonPolicy policy = context.Policies.Get <ISingletonPolicy>(key.Type, key.ID); return(policy != null && policy.IsSingleton); }
/// <summary> /// 定位对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="id"></param> /// <returns></returns> public T Locate <T>(string id) { DependencyResolutionLocatorKey locatorkey = new DependencyResolutionLocatorKey(typeof(T), id); return(locator.Get <T>(locatorkey)); }
/// <summary> /// 加载指定业务对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> public T LoadObject <T>(string key) { DependencyResolutionLocatorKey dkey = new DependencyResolutionLocatorKey(typeof(T), key); return(locator.Get <T>(dkey)); }
/// <summary> /// /保存指定业务对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="obj"></param> public void SaveObject <T>(string key, T obj) { DependencyResolutionLocatorKey dkey = new DependencyResolutionLocatorKey(typeof(T), key); locator.Add(dkey, obj); }