Esempio n. 1
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);
        }
Esempio n. 2
0
        /// <summary>
        /// Transform ObjectInstantiationMode by the specified OverrideObjectInstantiationMode
        /// </summary>
        /// <param name="src">Original ObjectInstantiationMode value</param>
        /// <param name="overrideMod">Override mode</param>
        /// <returns>Transformed instantiation mode</returns>
        protected static ObjectInstantiationMode TransformInstMode(ObjectInstantiationMode src, OverrideObjectInstantiationMode overrideMod)
        {
            switch (overrideMod)
            {
            case OverrideObjectInstantiationMode.ToSingleton:
                return(ObjectInstantiationMode.Singleton);

            case OverrideObjectInstantiationMode.ToDeferedSingleton:
                return(ObjectInstantiationMode.DeferedSingleton);

            case OverrideObjectInstantiationMode.ToPerThread:
                return(ObjectInstantiationMode.PerThread);

            case OverrideObjectInstantiationMode.ToPerCall:
                return(ObjectInstantiationMode.PerCall);

            case OverrideObjectInstantiationMode.ToPerCallInlinedParams:
                return(ObjectInstantiationMode.PerCallInlinedParams);

            case OverrideObjectInstantiationMode.None:
                return(src);
            }
            Debug.Assert(false, "Unknown OverrideObjectInstantiationMode: " + overrideMod.ToString());
            throw new AssociationIoCException("Unknown OverrideObjectInstantiationMode: " + overrideMod.ToString());
        }