Castle.MicroKernel.ModelBuilder.Inspectors.LifestyleModelInspector.ReadLifestyleFromConfiguration C# (CSharp) Method

ReadLifestyleFromConfiguration() protected method

Reads the attribute "lifestyle" associated with the component configuration and tries to convert to LifestyleType enum type.
protected ReadLifestyleFromConfiguration ( ComponentModel model ) : bool
model Castle.Core.ComponentModel
return bool
		protected virtual bool ReadLifestyleFromConfiguration(ComponentModel model)
		{
			if (model.Configuration == null)
			{
				return false;
			}

			var lifestyleRaw = model.Configuration.Attributes["lifestyle"];
			if (lifestyleRaw != null)
			{
				var lifestyleType = converter.PerformConversion<LifestyleType>(lifestyleRaw);
				model.LifestyleType = lifestyleType;
				switch (lifestyleType)
				{
					case LifestyleType.Singleton:
					case LifestyleType.Transient:
#if !(SILVERLIGHT || CLIENTPROFILE)
					case LifestyleType.PerWebRequest:
#endif
					case LifestyleType.Thread:
						return true;
					case LifestyleType.Pooled:
						ExtractPoolConfig(model);
						return true;
					case LifestyleType.Custom:
						var lifestyle = GetMandatoryTypeFromAttribute(model, "customLifestyleType", lifestyleType);
						ValidateTypeFromAttribute(lifestyle, typeof(ILifestyleManager), "customLifestyleType");
						model.CustomLifestyle = lifestyle;

						return true;
					case LifestyleType.Scoped:
						var scopeAccessorType = GetTypeFromAttribute(model, "scopeAccessorType");
						if (scopeAccessorType != null)
						{
							ValidateTypeFromAttribute(scopeAccessorType, typeof(IScopeAccessor), "scopeAccessorType");
							model.ExtendedProperties[Constants.ScopeAccessorType] = scopeAccessorType;
						}
						return true;
					case LifestyleType.Bound:
						var binderType = GetTypeFromAttribute(model, "scopeRootBinderType");
						if (binderType != null)
						{
							var binder = ExtractBinder(binderType, model.Name);
							model.ExtendedProperties[Constants.ScopeRootSelector] = binder;
						}
						return true;
					default:
						throw new InvalidOperationException(string.Format("Component {0} has {1} lifestyle. This is not a valid value.", model.Name, lifestyleType));
				}
			}
			else
			{
				// type was not present, but we might figure out the lifestyle based on presence of some attributes related to some lifestyles
				var binderType = GetTypeFromAttribute(model, "scopeRootBinderType");
				if (binderType != null)
				{
					var binder = ExtractBinder(binderType, model.Name);
					model.ExtendedProperties[Constants.ScopeRootSelector] = binder;
					model.LifestyleType = LifestyleType.Bound;
					return true;
				}
				var scopeAccessorType = GetTypeFromAttribute(model, "scopeAccessorType");
				if (scopeAccessorType != null)
				{
					ValidateTypeFromAttribute(scopeAccessorType, typeof(IScopeAccessor), "scopeAccessorType");
					model.ExtendedProperties[Constants.ScopeAccessorType] = scopeAccessorType;
					model.LifestyleType = LifestyleType.Scoped;
					return true;
				}
				var customLifestyleType = GetTypeFromAttribute(model, "customLifestyleType");
				if (customLifestyleType != null)
				{
					ValidateTypeFromAttribute(customLifestyleType, typeof(ILifestyleManager), "customLifestyleType");
					model.CustomLifestyle = customLifestyleType;
					model.LifestyleType = LifestyleType.Custom;
					return true;
				}
			}
			return false;
		}