MVC 2, Unity, Interception and EventAggregator - code review

Aug 11, 2009 at 5:44 PM
Edited Aug 11, 2009 at 5:45 PM

Being new to ASP.NET MVC2 and Unity Interception I would value a code review on my approach to integrating Unity into MVC2.   The existing/documented approaches no longer work since the interface to CreateController() has changed.   I used Unity Interception for the task while plugging in an EventAggregator (borrowed from PRISM).   blogged details and source code.

Curiously my ControllerCallHandler could not BuildUp the controller on line 20.   I had to resort to the Container setter doing a Buildup(this) and then Buildup(GetType(),this) to buildup both the base class as well as derived class, e.g., HomeController.


    1 public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    2 {
    3     IMethodReturn msg;
    4     try
    5     {
    6         Logger.Log(string.Format("BEFORE::ControllerCallHandler.Invoke for {0}",
    7             input.Target.GetType().Name), 
    8             Category.Debug, Priority.None);
   10         msg = getNext()(input, getNext);
   12         // Create child container for each
   13         IUnityContainer childContainer = Container.CreateChildContainer();
   15         // If the controller is being handled by MVCContrib - implements IControllerBase
   16         // then we'll set the Container.  Attempts to do a BuildUp here do not work as
   17         // expected so the Container Setter is expected to perform a Container.Buildup(this)
   18         // See Base\ControllerBase Container setter for details
   19         if (msg.ReturnValue is IControllerBase)
   20             ((IControllerBase)msg.ReturnValue).Container = childContainer;
   22         Logger.Log(string.Format("AFTER::ControllerCallHandler.Invoke for {0}",
   23             input.Target.GetType().Name),
   24             Category.Debug, Priority.None);
   26     }
   27     catch (Exception ex)
   28     {
   29         msg = input.CreateExceptionMethodReturn(ex);
   30     }
   32     return msg;
   33 }



Jan 17, 2010 at 1:07 AM

This project might help you:

Jun 6, 2010 at 10:39 PM
@Billkrat: Even though this post is a bit old your blog post is still no 1 for searches on mvc 2 and unity. I can only regret this since you're totally wrong and may have caused people to move away from Unity for the wrong reasons. The interface has not changed at all so your solution is totally unnecessary as well as over complicated. You can read my response and attempt to knock you down from number one in seach here: Best regards // Rickard Nilsson