Support Class providing a method to ensure a control has been intercepted
        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);
        }
Beispiel #2
0
 /// <summary>
 /// Apply dependency injection stuff on the handler.
 /// </summary>
 /// <param name="handler">the handler to be intercepted</param>
 /// <param name="applicationContext">the context responsible for configuring this handler</param>
 private static void ApplyDependencyInjectionInfrastructure(IHttpHandler handler, IApplicationContext applicationContext)
 {
     if (handler is Control)
     {
         ControlInterceptor.EnsureControlIntercepted(applicationContext, (Control)handler);
     }
     else
     {
         if ((handler is ISupportsWebDependencyInjection) &&
             (((ISupportsWebDependencyInjection)handler).DefaultApplicationContext == null))
         {
             ((ISupportsWebDependencyInjection)handler).DefaultApplicationContext = applicationContext;
         }
     }
 }