/// <summary> /// Releases all dependencies of the instance (which is being released) /// </summary> internal static void ReleaseDependencies(object control) { IWindsorContainer resolver = WindsorContainerAdapter.GetWindsorContainer(); PropertyInfo[] props = GetInjectableProperties(control); foreach (PropertyInfo propertyInfo in props) { object dependencyInstance = propertyInfo.GetValue(control); Type enumerableType = GetEnumerableType(propertyInfo.PropertyType); if ((enumerableType != null) && (dependencyInstance != null)) { IEnumerable dependencyInstanceEnumerable = (IEnumerable)dependencyInstance; foreach (var dependencyInstanceItem in dependencyInstanceEnumerable) { resolver.Release(dependencyInstanceItem); } } else if (dependencyInstance != null) { resolver.Release(dependencyInstance); } propertyInfo.SetValue(control, null); } }
/// <summary> /// Initializes the instance (Only the instance itself without child controls!). /// </summary> internal static bool InitializeInstance(object control) { IWindsorContainer resolver = WindsorContainerAdapter.GetWindsorContainer(); PropertyInfo[] props = GetInjectableProperties(control); // inject the values to properties foreach (PropertyInfo prop in props) { IEnumerable <InjectOverrideAttribute> overrideAttribs = prop.GetCustomAttributes(typeof(InjectOverrideAttribute), false).Cast <InjectOverrideAttribute>(); Dictionary <string, object> resolvedSubdependencies = overrideAttribs .ToDictionary(x => x.PropertyName, x => resolver.Resolve(x.DependencyKey, x.DependencyServiceType, null)); try { object value; Type enumerableType = GetEnumerableType(prop.PropertyType); if (enumerableType != null) { value = resolvedSubdependencies.Count > 0 ? resolver.ResolveAll(enumerableType, resolvedSubdependencies) : resolver.ResolveAll(enumerableType); } else { value = resolvedSubdependencies.Count > 0 ? resolver.Resolve(prop.PropertyType, resolvedSubdependencies) : resolver.Resolve(prop.PropertyType); } prop.SetValue(control, value, null); } catch (Exception e) { throw new ApplicationException($"Error in resolving dependency {prop.Name}.", e); } } return(props.Length > 0); }
/// <summary> /// Register Castle Windsor Container to HttpRuntime.WebObjectActivator and return its instance. /// If Castle Windsor Container is currently registered, returns the registered Castle Windsor Container instance. /// </summary> public static IWindsorContainer AddWindsorContainer(this HttpApplication application) { // ...(this HttpApplication application) - just to be able to use it as extension method if (application == null) { throw new ArgumentNullException(nameof(application)); } return(WindsorContainerAdapter.AddWindsorContainer()); }
/// <summary> /// Get registered Castle Windsor Container instance (or null if not registered). /// </summary> public static IWindsorContainer GetWindsorContainer() { return(WindsorContainerAdapter.GetWindsorContainer()); }