示例#1
0
        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);
        }
示例#2
0
        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);
        }
示例#3
0
 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);
        }
示例#5
0
 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);
        }
示例#7
0
        public SerializableContextual(IContextual contextual, IContextualStore contextualStore)
        {
            _cachedContextualStore = contextualStore;
            if (contextual.GetType().IsSerializable)
            {
                _serializable = contextual;
            }
            else
            {
                _id = contextualStore.PutIfAbsent(contextual);
            }

            _cached = contextual;
        }
示例#8
0
 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);
 }
示例#9
0
        /// <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;
            }));
        }
示例#10
0
 public ICreationalContext CreateCreationalContext(IContextual contextual)
 {
     return(new WeldCreationalContext(contextual));
 }
示例#11
0
 public object Get(IContextual contextual)
 {
     return(Get(contextual, null));
 }
示例#12
0
 public object GetIncompleteInstance(IContextual contextual)
 {
     return(_incompleteInstanceMap.GetOrDefault(contextual));
 }
示例#13
0
 public ICreationalContext GetCreationalContext(IContextual contextual)
 {
     return(new WeldCreationalContext(contextual,
                                      new Dictionary <IContextual, object>(_incompleteInstanceMap),
                                      _dependentInstances));
 }
示例#14
0
 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;
 }