/// <summary> /// Intercepts the given <see cref="ControlCollection"/> by dynamically deriving /// the original type and let it implement <see cref="ISupportsWebDependencyInjection"/>. /// </summary> /// <param name="defaultApplicationContext">the ApplicationContext to be set on the collection instance.</param> /// <param name="ctlAccessor">a wrapper around the owner control instance.</param> /// <param name="ctlColAccessor">a wrapper around the collection instance.</param> /// <returns><value>true</value>, if interception was successful. <value>false</value> otherwise</returns> public bool Intercept(IApplicationContext defaultApplicationContext, ControlAccessor ctlAccessor, ControlCollectionAccessor ctlColAccessor) { Type collectionType = ctlColAccessor.GetTargetType(); if (collectionType.IsSealed || !ReflectionUtils.IsTypeVisible(collectionType, DynamicProxyManager.ASSEMBLY_NAME)) // || (null == collectionType.GetConstructor(new Type[] {typeof (Control)})) { return(false); } // this will enhance the collection's type and create a new instance of this type with fields copied from original collection try { ControlCollection childControls = InterceptCollection(ctlAccessor.GetTarget(), ctlColAccessor.GetTarget()); ((ISupportsWebDependencyInjection)childControls).DefaultApplicationContext = defaultApplicationContext; ctlAccessor.Controls = childControls; } catch { // this may happen, if the ControlCollection doesn't contain a standard-ctor ControlCollection( Control owner) return(false); } return(true); }
/// <summary> /// Intercepts the given <see cref="ControlCollection"/> by dynamically deriving /// the original type and let it implement <see cref="ISupportsWebDependencyInjection"/>. /// </summary> /// <param name="defaultApplicationContext">the ApplicationContext to be set on the collection instance.</param> /// <param name="ctlAccessor">a wrapper around the owner control instance.</param> /// <param name="ctlColAccessor">a wrapper around the collection instance.</param> /// <returns><value>true</value>, if interception was successful. <value>false</value> otherwise</returns> public bool Intercept(IApplicationContext defaultApplicationContext, ControlAccessor ctlAccessor, ControlCollectionAccessor ctlColAccessor) { Type collectionType = ctlColAccessor.GetTargetType(); if (collectionType.IsSealed || !ReflectionUtils.IsTypeVisible(collectionType, DynamicProxyManager.ASSEMBLY_NAME)) // || (null == collectionType.GetConstructor(new Type[] {typeof (Control)})) { return false; } // this will enhance the collection's type and create a new instance of this type with fields copied from original collection try { ControlCollection childControls = InterceptCollection(ctlAccessor.GetTarget(), ctlColAccessor.GetTarget() ); ((ISupportsWebDependencyInjection)childControls).DefaultApplicationContext = defaultApplicationContext; ctlAccessor.Controls = childControls; } catch { // this may happen, if the ControlCollection doesn't contain a standard-ctor ControlCollection( Control owner) return false; } return true; }
private static Control InjectDependenciesRecursiveInternal(IApplicationContext appContext, Control control) { if (control is LiteralControl) { return(control); // nothing to do } ISupportsWebDependencyInjection diControl = control as ISupportsWebDependencyInjection; if (diControl != null && diControl.DefaultApplicationContext != null) { return(control); // nothing to do anymore - control cares for itself and its children } // "intercept" Control to make it DI-aware ControlInterceptor.EnsureControlIntercepted(appContext, control); // if the control is a UserControl, use ApplicationContext from it's physical location IApplicationContext appContextToUse = appContext; UserControl userControl = control as UserControl; if (userControl != null) { appContextToUse = GetControlApplicationContext(appContext, userControl); } // set ApplicationContext instance if (control is IApplicationContextAware) { ((IApplicationContextAware)control).ApplicationContext = appContextToUse; } // inject dependencies using control's context control = (Control)appContextToUse.ConfigureObject(control, control.GetType().FullName); // and now go for control's children if (control.HasControls()) { ControlCollection childControls = control.Controls; int childCount = childControls.Count; for (int i = 0; i < childCount; i++) { Control c = childControls[i]; if (c is LiteralControl) { continue; } Control configuredControl = InjectDependenciesRecursiveInternal(appContext, c); if (configuredControl != c) { ControlAccessor ac = new ControlAccessor(c.Parent); ac.SetControlAt(configuredControl, i); } } } return(control); }
public bool Intercept(IApplicationContext defaultApplicationContext, ControlAccessor ctlAccessor, ControlCollectionAccessor ctlColAccessor) { Control target = ctlAccessor.GetTarget(); ctlColAccessor.Owner = target is INamingContainer ? new NamingContainerSupportsWebDependencyInjectionOwnerProxy(defaultApplicationContext, target) : new SupportsWebDependencyInjectionOwnerProxy(defaultApplicationContext, target); return true; }
public bool Intercept(IApplicationContext defaultApplicationContext, ControlAccessor ctlAccessor, ControlCollectionAccessor ctlColAccessor) { Control target = ctlAccessor.GetTarget(); ctlColAccessor.Owner = target is INamingContainer ? new NamingContainerSupportsWebDependencyInjectionOwnerProxy(defaultApplicationContext, target) : new SupportsWebDependencyInjectionOwnerProxy(defaultApplicationContext, target); return(true); }
public bool Intercept(IApplicationContext defaultApplicationContext, ControlAccessor ctlAccessor, ControlCollectionAccessor ctlColAccessor) { return(true); }
private static void EnsureControlCollectionIntercepted(IApplicationContext defaultApplicationContext, Control control) { // check the collection ControlAccessor ctlAccessor = new ControlAccessor(control); ControlCollection childControls = ctlAccessor.Controls; if (IsDependencyInjectionAware(defaultApplicationContext, childControls)) { return; // nothing more to do } // check, if the collection's owner has already been intercepted ControlCollectionAccessor ctlColAccessor = new ControlCollectionAccessor(childControls); if (IsDependencyInjectionAware(defaultApplicationContext, ctlColAccessor.Owner)) { return; // nothing more to do } // lookup strategy in cache IInterceptionStrategy strategy = null; lock (s_cachedInterceptionStrategies) { strategy = (IInterceptionStrategy)s_cachedInterceptionStrategies[control.GetType()]; } if (strategy != null) { strategy.Intercept(defaultApplicationContext, ctlAccessor, ctlColAccessor); } else { // nothing in cache - try well-known strategies for owner resp. child collection type strategy = (IInterceptionStrategy)s_knownInterceptionStrategies[control.GetType()]; if (strategy == null) { strategy = (IInterceptionStrategy)s_knownInterceptionStrategies[childControls.GetType()]; } // try intercept using well-known strategy if (strategy != null) { bool bOk = strategy.Intercept(defaultApplicationContext, ctlAccessor, ctlColAccessor); if (!bOk) { strategy = null; } } // not well-known or didn't work out if (strategy == null) { // probe for a strategy bool bOk = false; for (int i = 0; i < s_availableInterceptionStrategies.Length; i++) { strategy = s_availableInterceptionStrategies[i]; bOk = strategy.Intercept(defaultApplicationContext, ctlAccessor, ctlColAccessor); if (bOk) { break; } } if (!bOk) { LogManager.GetLogger(typeof(ControlInterceptor)).Warn(string.Format("dependency injection not supported for control type {0}", ctlAccessor.GetTarget().GetType())); strategy = s_noopInterceptionStrategy; } } lock (s_cachedInterceptionStrategies) { s_cachedInterceptionStrategies[control.GetType()] = strategy; } } }
public bool Intercept( IApplicationContext defaultApplicationContext, ControlAccessor ctlAccessor, ControlCollectionAccessor ctlColAccessor ) { return true; }
private static void EnsureControlCollectionIntercepted( IApplicationContext defaultApplicationContext, Control control ) { // check the collection ControlAccessor ctlAccessor = new ControlAccessor( control ); ControlCollection childControls = ctlAccessor.Controls; if (IsDependencyInjectionAware( defaultApplicationContext, childControls )) { return; // nothing more to do } // check, if the collection's owner has already been intercepted ControlCollectionAccessor ctlColAccessor = new ControlCollectionAccessor( childControls ); if (IsDependencyInjectionAware( defaultApplicationContext, ctlColAccessor.Owner )) { return; // nothing more to do } // lookup strategy in cache IInterceptionStrategy strategy = null; lock (s_cachedInterceptionStrategies) { strategy = (IInterceptionStrategy)s_cachedInterceptionStrategies[control.GetType()]; } if (strategy != null) { strategy.Intercept( defaultApplicationContext, ctlAccessor, ctlColAccessor ); } else { // nothing in cache - try well-known strategies for owner resp. child collection type strategy = (IInterceptionStrategy)s_knownInterceptionStrategies[control.GetType()]; if (strategy == null) { strategy = (IInterceptionStrategy)s_knownInterceptionStrategies[childControls.GetType()]; } // try intercept using well-known strategy if (strategy != null) { bool bOk = strategy.Intercept( defaultApplicationContext, ctlAccessor, ctlColAccessor ); if (!bOk) { strategy = null; } } // not well-known or didn't work out if (strategy == null) { // probe for a strategy bool bOk = false; for (int i = 0; i < s_availableInterceptionStrategies.Length; i++) { strategy = s_availableInterceptionStrategies[i]; bOk = strategy.Intercept( defaultApplicationContext, ctlAccessor, ctlColAccessor ); if (bOk) break; } if (!bOk) { LogManager.GetLogger( typeof( ControlInterceptor ) ).Warn( string.Format( "dependency injection not supported for control type {0}", ctlAccessor.GetTarget().GetType() ) ); strategy = s_noopInterceptionStrategy; } } lock (s_cachedInterceptionStrategies) { s_cachedInterceptionStrategies[control.GetType()] = strategy; } } }
/// <summary> /// Wraps a control to make it DI aware /// </summary> /// <param name="defaultApplicationContext"></param> /// <param name="targetControl"></param> public SupportsWebDependencyInjectionOwnerProxy(IApplicationContext defaultApplicationContext, Control targetControl) { _defaultApplicationContext = defaultApplicationContext; _targetControl = new ControlAccessor(targetControl); }
private static Control InjectDependenciesRecursiveInternal(IApplicationContext appContext, Control control) { if (control is LiteralControl) return control; // nothing to do ISupportsWebDependencyInjection diControl = control as ISupportsWebDependencyInjection; if (diControl != null && diControl.DefaultApplicationContext != null) { return control; // nothing to do anymore - control cares for itself and its children } // "intercept" Control to make it DI-aware ControlInterceptor.EnsureControlIntercepted(appContext, control); // if the control is a UserControl, use ApplicationContext from it's physical location IApplicationContext appContextToUse = appContext; UserControl userControl = control as UserControl; if (userControl != null) { appContextToUse = GetControlApplicationContext(appContext, userControl); } // set ApplicationContext instance if (control is IApplicationContextAware) { ((IApplicationContextAware)control).ApplicationContext = appContextToUse; } // inject dependencies using control's context control = (Control)appContextToUse.ConfigureObject(control, control.GetType().FullName); // and now go for control's children if (control.HasControls()) { ControlCollection childControls = control.Controls; int childCount = childControls.Count; for (int i = 0; i < childCount; i++) { Control c = childControls[i]; if (c is LiteralControl) continue; Control configuredControl = InjectDependenciesRecursiveInternal(appContext, c); if (configuredControl != c) { ControlAccessor ac = new ControlAccessor(c.Parent); ac.SetControlAt(configuredControl, i); } } } return control; }