Exemplo n.º 1
0
        /// <summary>Code contracts</summary>
        public bool TryGetAssociation(T key, out LifetimeBase val)
        {
            TurboContract.Ensures((TurboContract.Result <bool>() == true && TurboContract.ValueAtReturn <LifetimeBase>(out val) != null) ||
                                  (TurboContract.Result <bool>() == false && TurboContract.ValueAtReturn <LifetimeBase>(out val) == null));

            throw new NotImplementedException();
        }
Exemplo n.º 2
0
        protected override bool TryAddAssociationInner(T key, LifetimeBase val)
        {
            Contract.Requires((object)key != null);
            Contract.Requires(val != null);

            throw new NotImplementedException();
        }
        protected override bool TryAddAssociationInner(T key, LifetimeBase val)
        {
            TurboContract.Requires(key != null, conditionString: "key != null");
            TurboContract.Requires(val != null, conditionString: "val != null");

            throw new NotImplementedException();
        }
Exemplo n.º 4
0
        protected override bool TryGetAssociationInner(T key, out LifetimeBase val)
        {
            Contract.Requires((object)key != null);
            Contract.Ensures((Contract.Result <bool>() == true && Contract.ValueAtReturn <LifetimeBase>(out val) != null) ||
                             (Contract.Result <bool>() == false && Contract.ValueAtReturn <LifetimeBase>(out val) == null));

            throw new NotImplementedException();
        }
        protected override bool TryGetAssociationInner(T key, out LifetimeBase val)
        {
            TurboContract.Requires(key != null, conditionString: "key != null");
            TurboContract.Ensures((TurboContract.Result <bool>() == true && TurboContract.ValueAtReturn <LifetimeBase>(out val) != null) ||
                                  (TurboContract.Result <bool>() == false && TurboContract.ValueAtReturn <LifetimeBase>(out val) == null));

            throw new NotImplementedException();
        }
        /// <summary>
        /// Adds a new association to the container
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="val">Lifetime object container to add</param>
        protected sealed override void AddAssociationInner(TKey key, LifetimeBase val)
        {
            lock (_storage)
            {
                if (_storage.ContainsKey(key))
                {
                    throw new ItemAlreadyExistsException(string.Format("AssociationContainer already contains the association for the key ({0})", key));
                }

                _storage.Add(key, val);
            }
        }
        /// <summary>
        /// Attempts to add a new association to the container
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="val">Lifetime object container to add</param>
        /// <returns>True if AssociationContainer not contains lifetime container with the same key; overwise false</returns>
        protected sealed override bool TryAddAssociationInner(TKey key, LifetimeBase val)
        {
            lock (_storage)
            {
                if (_storage.ContainsKey(key))
                {
                    return(false);
                }

                _storage.Add(key, val);
            }
            return(true);
        }
Exemplo n.º 8
0
        private T TestContainer <T>(LifetimeBase container)
        {
            TestInjectionResolver resolver = new TestInjectionResolver();

            Assert.AreEqual(typeof(T), container.OutputType);

            var instance = container.GetInstance(resolver);

            Assert.IsNotNull(instance);
            Assert.IsInstanceOfType(instance, typeof(T));

            return((T)instance);
        }
        /// <summary>
        /// Adds a new association to the container
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="val">Lifetime object container to add</param>
        protected sealed override void AddAssociationInner(TKey key, LifetimeBase val)
        {
            TurboContract.Requires(key != null, conditionString: "key != null");
            TurboContract.Requires(val != null, conditionString: "val != null");

            lock (_storage)
            {
                if (_storage.ContainsKey(key))
                {
                    throw new ItemAlreadyExistsException(string.Format("AssociationContainer already contains the association for the key ({0})", key));
                }

                _storage.Add(key, val);
            }
        }
        /// <summary>
        /// Attempts to add a new association to the container
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="val">Lifetime object container to add</param>
        /// <returns>True if AssociationContainer not contains lifetime container with the same key; overwise false</returns>
        protected bool TryAddAssociation(TKey key, LifetimeBase val)
        {
            TurboContract.Requires(key != null, conditionString: "key != null");
            TurboContract.Requires(val != null, conditionString: "val != null");
            TurboContract.Ensures(TurboContract.Result <bool>() == false || this.ContainsInner(key));

            if (!IsGoodTypeForKey(key, val.OutputType))
            {
                throw new AssociationBadKeyForTypeException(string.Format("Bad key ({0}) for the type ({1})", key, val.OutputType));
            }

            CheckContainerState(true);

            return(TryAddAssociationInner(key, val));
        }
Exemplo n.º 11
0
        /// <summary>
        /// Attempts to get the lifetime object container by its key
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="val">Associated lifetime container (null when key not exists)</param>
        /// <returns>True if the lifetime container for the speicifed key exists in AssociatnioContainer</returns>
        bool IAssociationSource <TKey> .TryGetAssociation(TKey key, out LifetimeBase val)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (_isDisposed)
            {
                val = null;
                return(false);
            }

            return(TryGetAssociationInner(key, out val));
        }
        /// <summary>
        /// Attempts to add a new association to the container
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="val">Lifetime object container to add</param>
        /// <returns>True if AssociationContainer not contains lifetime container with the same key; overwise false</returns>
        protected sealed override bool TryAddAssociationInner(TKey key, LifetimeBase val)
        {
            TurboContract.Requires(key != null, conditionString: "key != null");
            TurboContract.Requires(val != null, conditionString: "val != null");

            lock (_storage)
            {
                if (_storage.ContainsKey(key))
                {
                    return(false);
                }

                _storage.Add(key, val);
            }
            return(true);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Gets the lifetime object container by its key
        /// </summary>
        /// <param name="key">Key</param>
        /// <returns>Lifetime container for the specified key</returns>
        LifetimeBase IAssociationSource <TKey> .GetAssociation(TKey key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (_isDisposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            LifetimeBase res = null;

            if (!TryGetAssociationInner(key, out res))
            {
                throw new KeyNotFoundException(string.Format("Key {0} not found in Association Container", key));
            }

            return(res);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Adds a new association to the container
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="val">Lifetime object container to add</param>
        protected void AddAssociation(TKey key, LifetimeBase val)
        {
            Contract.Requires(key != null);
            Contract.Requires(val != null);
            Contract.Ensures(this.ContainsInner(key));

            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (val == null)
            {
                throw new ArgumentNullException("val");
            }

            if (!IsGoodTypeForKey(key, val.OutputType))
            {
                throw new AssociationBadKeyForTypeException(string.Format("Bad key ({0}) for type ({1})", key, val.OutputType));
            }

            CheckContainerState(true);

            AddAssociationInner(key, val);
        }
 internal bool TryGetAssociation(TKey key, out LifetimeBase val)
 {
     return(_storage.TryGetValue(key, out val));
 }
        /// <summary>
        /// Attempts to get an association from the container by the specified key
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="val">Lifetime container for the specified key (in case of success)</param>
        /// <returns>True if the AssociationContainer contains the lifetime container for the specified key</returns>
        protected sealed override bool TryGetAssociationInner(TKey key, out LifetimeBase val)
        {
            TurboContract.Requires(key != null, conditionString: "key != null");

            return(_storage.TryGetValue(key, out val));
        }
Exemplo n.º 17
0
 /// <summary>
 /// Attempts to get an association from the container by the specified key. Should be implemented in derived type.
 /// </summary>
 /// <param name="key">Key</param>
 /// <param name="val">Lifetime container for the specified key (in case of success)</param>
 /// <returns>True if the AssociationContainer contains the lifetime container for the specified key</returns>
 protected abstract bool TryGetAssociationInner(TKey key, out LifetimeBase val);
 /// <summary>
 /// Attempts to get an association from the container by the specified key
 /// </summary>
 /// <param name="key">Key</param>
 /// <param name="val">Lifetime container for the specified key (in case of success)</param>
 /// <returns>True if the AssociationContainer contains the lifetime container for the specified key</returns>
 protected sealed override bool TryGetAssociationInner(TKey key, out LifetimeBase val)
 {
     return(_storage.TryGetValue(key, out val));
 }
Exemplo n.º 19
0
 /// <summary>
 /// Attempts to add a new association to the container. Should be implemented in derived type.
 /// </summary>
 /// <param name="key">Key</param>
 /// <param name="val">Lifetime object container to add</param>
 /// <returns>True if AssociationContainer not contains lifetime container with the same key; overwise false</returns>
 protected abstract bool TryAddAssociationInner(TKey key, LifetimeBase val);
Exemplo n.º 20
0
 /// <summary>
 /// Adds a new association to the container. Should be implemented in derived type.
 /// </summary>
 /// <param name="key">Key</param>
 /// <param name="val">Lifetime object container to add</param>
 protected abstract void AddAssociationInner(TKey key, LifetimeBase val);
Exemplo n.º 21
0
        /// <summary>
        /// Adds a range of types with attributes to the container
        /// </summary>
        /// <typeparam name="TAttr">The type of the attribute that is used to mark a type (should be derived from LocatorTargetObjectAttribute)</typeparam>
        /// <param name="typeSource">The sequence of all types to scan and add to the container</param>
        /// <param name="attrCmpPredicate">Predicate that allows to filter out non relevant types (can be null)</param>
        /// <param name="keyGenerator">Function to create a key by the type and attribute</param>
        /// <param name="modeOver">Overrides the instantiation mode from attribute</param>
        /// <param name="multiAttr">Allows processing of multiple attributes on the same type</param>
        /// <param name="combineIfPossible">Allows to combine instances of the same type with different keys</param>
        protected void AddTypeRangeWithStrictAttrPlain <TAttr>(IEnumerable <Type> typeSource, Func <TAttr, bool> attrCmpPredicate, Func <Type, TAttr, TKey> keyGenerator,
                                                               OverrideObjectInstantiationMode modeOver = OverrideObjectInstantiationMode.None, bool multiAttr = true, bool combineIfPossible = true)
            where TAttr : LocatorTargetObjectAttribute
        {
            Contract.Requires <ArgumentNullException>(typeSource != null);
            Contract.Requires <ArgumentNullException>(keyGenerator != null);


            Type         curAnalizeType     = null;
            LifetimeBase singletonLf        = null;
            LifetimeBase deferedSingletonLf = null;
            LifetimeBase perThreadLf        = null;

            ScanTypeRangeWithStrictAttr <TAttr>(typeSource, attrCmpPredicate, (tp, attr) =>
            {
                if (tp != curAnalizeType)
                {
                    curAnalizeType     = tp;
                    singletonLf        = null;
                    deferedSingletonLf = null;
                    perThreadLf        = null;
                }

                var key = keyGenerator(tp, attr);

                ObjectInstantiationMode instMode = TransformInstMode(attr.Mode, modeOver);

                if (combineIfPossible)
                {
                    switch (instMode)
                    {
                    case ObjectInstantiationMode.Singleton:
                        if (singletonLf == null)
                        {
                            singletonLf = ProduceResolveInfo(key, tp, LifetimeFactories.Singleton);
                        }
                        AddAssociation(key, singletonLf);
                        break;

                    case ObjectInstantiationMode.DeferedSingleton:
                        if (deferedSingletonLf == null)
                        {
                            deferedSingletonLf = ProduceResolveInfo(key, tp, LifetimeFactories.DeferedSingleton);
                        }
                        AddAssociation(key, deferedSingletonLf);
                        break;

                    case ObjectInstantiationMode.PerThread:
                        if (perThreadLf == null)
                        {
                            perThreadLf = ProduceResolveInfo(key, tp, LifetimeFactories.PerThread);
                        }
                        AddAssociation(key, perThreadLf);
                        break;

                    default:
                        AddAssociation(key, tp, LifetimeFactories.GetLifetimeFactory(instMode));
                        break;
                    }
                }
                else
                {
                    AddAssociation(key, tp, LifetimeFactories.GetLifetimeFactory(instMode));
                }
            }, multiAttr);
        }
        /// <summary>
        /// Removes the association from the container for the specified key
        /// </summary>
        /// <param name="key">Key</param>
        /// <returns>True if the association was presented in container</returns>
        protected sealed override bool RemoveAssociationInner(TKey key)
        {
            LifetimeBase tmp = null;

            return(_storage.TryRemove(key, out tmp));
        }
 /// <summary>
 /// Attempts to get the lifetime object container by its key
 /// </summary>
 /// <param name="key">Key</param>
 /// <param name="val">Associated lifetime container (null when key not exists)</param>
 /// <returns>True if the lifetime container for the speicifed key exists in AssociatnioContainer</returns>
 bool IAssociationSource <TKey> .TryGetAssociation(TKey key, out LifetimeBase val)
 {
     return(_storage.TryGetValue(key, out val));
 }