Unity and ASP.NET



Using Dependency Injection in a website can get a little dodgy, but in my ASP.NET site use the same base code as my WPF app, I needed a little dependency injection to resolve references at runtime when the application type is known. Now in your ASP.NET page just like in your WPF application you need a little extra bit to get it all going. Finding this for WPF is easy, not so much in ASP.

Imports System.Web
Imports System.Web.UI
Imports Microsoft.Practices.Unity
''' <summary>
''' C# version and source
''' http://blogs.msdn.com/mpuleio/archive/2008/07/17/proof-of-concept-a-simple-di-solution-for-asp-net-webforms.aspx
''' </summary>
''' <remarks></remarks>
Public Class UnityHttpModule
    Implements IHttpModule

    Public Sub Dispose() Implements System.Web.IHttpModule.Dispose

    End Sub

    Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
        AddHandler context.PreRequestHandlerExecute, AddressOf OnPreRequestHandlerExecute
    End Sub

    Private Sub OnPreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs)
        Dim handler As IHttpHandler = HttpContext.Current.Handler
        If TypeOf handler Is Page Then
            My.Unity.Container.BuildUp(handler.GetType(), handler)

            ' User Controls are ready to be built up after the page initialization is complete
            Dim page As Page = handler
            If Not page Is Nothing Then
                AddHandler page.InitComplete, AddressOf OnPageInitComplete
            End If
        End If
    End Sub

    Private Sub OnPageInitComplete(ByVal sender As Object, ByVal e As EventArgs)
        Dim page As Web.UI.Page = sender
        For Each c In BuildControlTree(page)
                My.Unity.Container.BuildUp(c.GetType(), c)
            Catch ex As Exception
                ' TODO: Some sort of error handling if important
                WebPortalTrace.Verbose(WebPortalTraceType.Unity, "Unity unable to build up {0}", c.GetType)
            End Try
    End Sub

    ' Get the controls in the page's control tree excluding the page itself
    Private Function BuildControlTree(ByVal root As Control) As List(Of Control)
        Dim ct As New List(Of Control)
        For Each c In root.Controls
        Return ct
    End Function

End Class


All you need is to put a reference into your config file:

      <add name="UnityHttpModule" type="Company.Product.UnityHttpModule, Company.Product"/>


And off you go, before you know it you will have dependency injection coming out of your ears.

One of the advantages to using dependency injection is that you could change a piece of functionality without having to recompile and redeploy your site! How about this…

          <type type="Company.Product.ViewModels.IRecentItemsViewModel, Company.Product" mapTo="Company.Product.ViewModels.RecentItemsViewModel, Company.Product" />


The business then decide that they have to have the order of the recent items list changed but that it needs to go into production immediately, so your testing cycle is extremely tight. No problem… fire up a new solution and create a new class that inherits from IRecentItemsViewModel and implement the new functionality. Then compile it as “Company.Product.Hotfix1”, drop it into your test site bin folder and change line above to:

          <type type="Company.Product.ViewModels.IRecentItemsViewModel, Company.Product" mapTo="Company.Product.Hotfix1.ViewModels.RecentItemsViewModel, Company.Product.Hotfix1" />


The site will then load your new code and you can test the only functionality that you have changed, before deploying to production. Now this may not seam like much, but if your system is made up of thousands of views then you may just need this functionality. And it is so easy to achieve that even for small projects it is fantastic.


P.S. Works with MVC… shhhh…

Create a conversation around this article

Share on Facebook
Share on Twitter
Share on Linkdin

We dont have any dates for public classes right now. sign-up to be the first to know, or contact us for discounts or private training.

Read more
Martin Hinshelwood
As we progress deeper into the dynamic landscape of the 21st century, our long-established organisations, born of the Industrial Age and infused with a DNA of strict command and control, stand on shaky ground. These organisations strut with command-and-control bravado, erecting clear hierarchies in their stable inert markets where bureaucracy …
Martin Hinshelwood
In organizational development and team dynamics, Agile (as the Agile Manifesto delineates) and Scrum (as the Scrum Guide outlines) guide teams not by solving their problems but by illuminating the issues that demand attention. These frameworks aim to identify and spotlight the challenges within a team or organization’s processes, effectively …
Martin Hinshelwood
This week, I participated in a Scrum.org Webinar hosted by Sabrina Love (Scrum.org Product Owner) as well as my colleagues, Joanna Płaskonka, Ph.D. and Alex Ballarin to discuss the state of learning and how immersive learning is the future of training. You can watch the video below to hear what …
Martin Hinshelwood
For a long time now I have been searching for that perfect domain that epitomised the vision, the why, of what I am trying to achieve with my customers and the industry at large. Now I have found it in http://nkdagility.com