private void AddDependentInstance(object instance, IContextual contextual, IWeldCreationalContext creationalContext) { // by this we are making sure that the dependent instance has no transitive dependency with @PreDestroy / disposal method if (!creationalContext.DependentInstances.Any()) { var classComponent = contextual as ClassComponent; if (classComponent != null) { if (!classComponent.IsDisposable /* TODO: && component.HasInterceptors && component.HasDefaultProducer */) { return; } } var producer = contextual as ProducerMethod; if (producer != null) { /*TODO: if (producer.DisposalMethod == null && producer.HasDefaultProducer) */ return; } } var componentInstance = new SerializableContextualInstance(contextual, instance, creationalContext, Container.Instance.ContextualStore); creationalContext.AddDependentInstance(componentInstance); }
public object Get(IContextual contextual, ICreationalContext creationalContext) { if (!IsActive) { throw new ContextNotActiveException(Scope); } var store = ComponentStore; if (store == null) { return(null); } if (contextual == null) { throw new ArgumentNullException("contextual"); } var contextualStore = Container.Instance.ContextualStore; var id = contextualStore.PutIfAbsent(contextual); var instance = store.GetOrPut(id, _ => { var i = contextual.Create(creationalContext); return(new SerializableContextualInstance(contextual, i, creationalContext, contextualStore)); }); if (instance != null) { return(instance.Instance); } return(null); }
private WeldCreationalContext(IContextual contextual, Dictionary <IContextual, object> incompleteInstanceMap, List <IContextualInstance> parentDependentInstances) { _contextual = contextual; _incompleteInstanceMap = incompleteInstanceMap; _parentDependentInstances = parentDependentInstances; }
/// <summary>Remove contextual values from the tracker.</summary> /// <param name="value">The context value to stop tracking. Null values are ignored.</param> public AggregateContextual Remove(IContextual value) { if (value != null && this.ValuesImpl.Remove(value)) { this.IsMutable = this.ValuesImpl.Any(p => p.IsMutable); this.IsReady = this.ValuesImpl.All(p => p.IsReady); } return(this); }
public void Release(IContextual contextual, object instance) { foreach (var dependentInstance in _dependentInstances) { if (contextual == null || !Equals(dependentInstance.Contextual, contextual)) { Destroy(dependentInstance); } } }
/********* ** Accessors *********/ /// <summary>Add contextual values to the tracker.</summary> /// <param name="value">The context value to track. Null values are ignored.</param> public AggregateContextual Add(IContextual value) { if (value != null) { this.ValuesImpl.Add(value); this.IsMutable = this.IsMutable || value.IsMutable; this.IsReady = this.IsReady && value.IsReady; } return(this); }
public SerializableContextual(IContextual contextual, IContextualStore contextualStore) { _cachedContextualStore = contextualStore; if (contextual.GetType().IsSerializable) { _serializable = contextual; } else { _id = contextualStore.PutIfAbsent(contextual); } _cached = contextual; }
public object Get(IContextual contextual, ICreationalContext creationalContext) { if (!IsActive) { throw new ContextNotActiveException(Scope); } if (creationalContext != null) { var instance = contextual.Create(creationalContext); var weldContext = creationalContext as IWeldCreationalContext; if (weldContext != null) { AddDependentInstance(instance, contextual, weldContext); } return(instance); } return(null); }
/// <summary> /// Add a contextual (if not already present) to the store, and return it's /// id. If the contextual is passivation capable, it's id will be used, /// otherwise an id will be generated /// </summary> /// <param name="contextual">contextual the contexutal to add</param> /// <returns>the current id for the contextual</returns> public ComponentIdentifier PutIfAbsent(IContextual contextual) { var passivationCapable = contextual as IPassivationCapable <ComponentIdentifier>; if (passivationCapable != null) { var id = passivationCapable.Id; _passivationCapableContextuals.TryAdd(id, contextual); return(id); } return(_contextuals.GetOrAdd(contextual, _ => { var id = new ComponentIdentifier( string.Format("{0}{1}", GeneratedIdPrefix, Interlocked.Increment(ref _idIncrement))); _contextualsInverse[id] = contextual; return id; })); }
public ICreationalContext CreateCreationalContext(IContextual contextual) { return(new WeldCreationalContext(contextual)); }
public object Get(IContextual contextual) { return(Get(contextual, null)); }
public object GetIncompleteInstance(IContextual contextual) { return(_incompleteInstanceMap.GetOrDefault(contextual)); }
public ICreationalContext GetCreationalContext(IContextual contextual) { return(new WeldCreationalContext(contextual, new Dictionary <IContextual, object>(_incompleteInstanceMap), _dependentInstances)); }
public WeldCreationalContext(IContextual contextual) : this(contextual, new Dictionary <IContextual, object>(), new List <IContextualInstance>()) { }
public SerializableContextualInstance(IContextual contextual, object instance, ICreationalContext creationalContext, IContextualStore contextualStore) { Contextual = new SerializableContextual(contextual, contextualStore); Instance = instance; CreationalContext = creationalContext; }