Example #1
0
        /// <summary>
        ///   Creates an implementation of <see cref = "ILifestyleManager" /> based on <see cref = "LifestyleType" /> and invokes <see cref = "ILifestyleManager.Init" /> to initialize the newly created manager.
        /// </summary>
        /// <param name = "model"> </param>
        /// <param name = "activator"> </param>
        /// <returns> </returns>
        public virtual ILifestyleManager CreateLifestyleManager(ComponentModel model, IComponentActivator activator)
        {
            ILifestyleManager manager;
            var type = model.LifestyleType;

            switch (type)
            {
            case LifestyleType.Scoped:
                manager = new ScopedLifestyleManager(CreateScopeAccessor(model));
                break;

            case LifestyleType.Bound:
                manager = new ScopedLifestyleManager(CreateScopeAccessorForBoundLifestyle(model));
                break;

            case LifestyleType.Thread:
                manager = new ScopedLifestyleManager(new ThreadScopeAccessor());
                break;

            case LifestyleType.Transient:
                manager = new TransientLifestyleManager();
                break;

#if !(SILVERLIGHT || CLIENTPROFILE)
            case LifestyleType.PerWebRequest:
                manager = new ScopedLifestyleManager(new WebRequestScopeAccessor());
                break;
#endif
            case LifestyleType.Custom:
                manager = model.CustomLifestyle.CreateInstance <ILifestyleManager>();

                break;

            case LifestyleType.Pooled:
                var initial = ExtendedPropertiesConstants.Pool_Default_InitialPoolSize;
                var maxSize = ExtendedPropertiesConstants.Pool_Default_MaxPoolSize;

                if (model.ExtendedProperties.Contains(ExtendedPropertiesConstants.Pool_InitialPoolSize))
                {
                    initial = (int)model.ExtendedProperties[ExtendedPropertiesConstants.Pool_InitialPoolSize];
                }
                if (model.ExtendedProperties.Contains(ExtendedPropertiesConstants.Pool_MaxPoolSize))
                {
                    maxSize = (int)model.ExtendedProperties[ExtendedPropertiesConstants.Pool_MaxPoolSize];
                }

                manager = new PoolableLifestyleManager(initial, maxSize);
                break;

            default:
                //this includes LifestyleType.Undefined, LifestyleType.Singleton and invalid values
                manager = new SingletonLifestyleManager();
                break;
            }

            manager.Init(activator, this, model);

            return(manager);
        }
 public override ILifestyleManager CreateLifestyleManager(ComponentModel model, IComponentActivator activator)
 {
     if (model.LifestyleType == LifestyleType.PerWebRequest)
     {
         var manager = new ScopedLifestyleManager(new PerRequestScopeAccessor());
         manager.Init(activator, this, model);
         return(manager);
     }
     return(base.CreateLifestyleManager(model, activator));
 }
		/// <summary>
		///   Creates an implementation of
		///   <see cref = "ILifestyleManager" />
		///   based
		///   on
		///   <see cref = "LifestyleType" />
		///   and invokes
		///   <see cref = "ILifestyleManager.Init" />
		///   to initialize the newly created manager.
		/// </summary>
		/// <param name = "activator"></param>
		/// <returns></returns>
		protected virtual ILifestyleManager CreateLifestyleManager(IComponentActivator activator)
		{
			ILifestyleManager manager;
			var type = ComponentModel.LifestyleType;

			switch (type)
			{
				case LifestyleType.Scoped:
					var scopeManager = Kernel.GetSubSystem("scope") as IScopeManager;
					if (scopeManager == null)
					{
						throw new InvalidOperationException("Scope Subsystem not found.  Did you forget to add it?");
					}
					manager = new ScopedLifestyleManager(new CurrentScopeAccessor(scopeManager, ComponentModel));
					break;
				case LifestyleType.Thread:
#if SILVERLIGHT
					manager = new PerThreadThreadStaticLifestyleManager();
#else
					manager = new PerThreadLifestyleManager();
#endif
					break;
				case LifestyleType.Transient:
					manager = new TransientLifestyleManager();
					break;
#if (!SILVERLIGHT && !CLIENTPROFILE)
				case LifestyleType.PerWebRequest:
					manager = new PerWebRequestLifestyleManager();
					break;
#endif
				case LifestyleType.Custom:
					manager = ComponentModel.CustomLifestyle.CreateInstance<ILifestyleManager>();

					break;
				case LifestyleType.Pooled:
				{
					var initial = ExtendedPropertiesConstants.Pool_Default_InitialPoolSize;
					var maxSize = ExtendedPropertiesConstants.Pool_Default_MaxPoolSize;

					if (ComponentModel.ExtendedProperties.Contains(ExtendedPropertiesConstants.Pool_InitialPoolSize))
					{
						initial = (int)ComponentModel.ExtendedProperties[ExtendedPropertiesConstants.Pool_InitialPoolSize];
					}
					if (ComponentModel.ExtendedProperties.Contains(ExtendedPropertiesConstants.Pool_MaxPoolSize))
					{
						maxSize = (int)ComponentModel.ExtendedProperties[ExtendedPropertiesConstants.Pool_MaxPoolSize];
					}

					manager = new PoolableLifestyleManager(initial, maxSize);
				}
					break;
				default:
					//this includes LifestyleType.Undefined, LifestyleType.Singleton and invalid values
					manager = new SingletonLifestyleManager();
					break;
			}

			manager.Init(activator, Kernel, ComponentModel);

			return manager;
		}
        /// <summary>
        ///   Creates an implementation of
        ///   <see cref = "ILifestyleManager" />
        ///   based
        ///   on
        ///   <see cref = "LifestyleType" />
        ///   and invokes
        ///   <see cref = "ILifestyleManager.Init" />
        ///   to initialize the newly created manager.
        /// </summary>
        /// <param name = "activator"></param>
        /// <returns></returns>
        protected virtual ILifestyleManager CreateLifestyleManager(IComponentActivator activator)
        {
            ILifestyleManager manager;
            var type = ComponentModel.LifestyleType;

            switch (type)
            {
            case LifestyleType.Scoped:
                var scopeManager = Kernel.GetSubSystem("scope") as IScopeManager;
                if (scopeManager == null)
                {
                    throw new InvalidOperationException("Scope Subsystem not found.  Did you forget to add it?");
                }
                manager = new ScopedLifestyleManager(new CurrentScopeAccessor(scopeManager, ComponentModel));
                break;

            case LifestyleType.Thread:
#if SILVERLIGHT
                manager = new PerThreadThreadStaticLifestyleManager();
#else
                manager = new PerThreadLifestyleManager();
#endif
                break;

            case LifestyleType.Transient:
                manager = new TransientLifestyleManager();
                break;

#if (!SILVERLIGHT && !CLIENTPROFILE)
            case LifestyleType.PerWebRequest:
                manager = new PerWebRequestLifestyleManager();
                break;
#endif
            case LifestyleType.Custom:
                manager = ComponentModel.CustomLifestyle.CreateInstance <ILifestyleManager>();

                break;

            case LifestyleType.Pooled:
            {
                var initial = ExtendedPropertiesConstants.Pool_Default_InitialPoolSize;
                var maxSize = ExtendedPropertiesConstants.Pool_Default_MaxPoolSize;

                if (ComponentModel.ExtendedProperties.Contains(ExtendedPropertiesConstants.Pool_InitialPoolSize))
                {
                    initial = (int)ComponentModel.ExtendedProperties[ExtendedPropertiesConstants.Pool_InitialPoolSize];
                }
                if (ComponentModel.ExtendedProperties.Contains(ExtendedPropertiesConstants.Pool_MaxPoolSize))
                {
                    maxSize = (int)ComponentModel.ExtendedProperties[ExtendedPropertiesConstants.Pool_MaxPoolSize];
                }

                manager = new PoolableLifestyleManager(initial, maxSize);
            }
            break;

            default:
                //this includes LifestyleType.Undefined, LifestyleType.Singleton and invalid values
                manager = new SingletonLifestyleManager();
                break;
            }

            manager.Init(activator, Kernel, ComponentModel);

            return(manager);
        }