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.
1Imports System.Web
2Imports System.Web.UI
3Imports Microsoft.Practices.Unity
4''' <summary>
5''' C# version and source
6''' http://blogs.msdn.com/mpuleio/archive/2008/07/17/proof-of-concept-a-simple-di-solution-for-asp-net-webforms.aspx
7''' </summary>
8''' <remarks></remarks>
9Public Class UnityHttpModule
10 Implements IHttpModule
11
12 Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
13
14 End Sub
15
16 Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
17 AddHandler context.PreRequestHandlerExecute, AddressOf OnPreRequestHandlerExecute
18 End Sub
19
20 Private Sub OnPreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs)
21 Dim handler As IHttpHandler = HttpContext.Current.Handler
22 If TypeOf handler Is Page Then
23 My.Unity.Container.BuildUp(handler.GetType(), handler)
24
25 ' User Controls are ready to be built up after the page initialization is complete
26 Dim page As Page = handler
27 If Not page Is Nothing Then
28 AddHandler page.InitComplete, AddressOf OnPageInitComplete
29 End If
30 End If
31 End Sub
32
33 Private Sub OnPageInitComplete(ByVal sender As Object, ByVal e As EventArgs)
34 Dim page As Web.UI.Page = sender
35 For Each c In BuildControlTree(page)
36 Try
37 My.Unity.Container.BuildUp(c.GetType(), c)
38 Catch ex As Exception
39 ' TODO: Some sort of error handling if important
40 WebPortalTrace.Verbose(WebPortalTraceType.Unity, "Unity unable to build up {0}", c.GetType)
41 End Try
42 Next
43 End Sub
44
45 ' Get the controls in the page's control tree excluding the page itself
46 Private Function BuildControlTree(ByVal root As Control) As List(Of Control)
47 Dim ct As New List(Of Control)
48 For Each c In root.Controls
49 ct.Add(c)
50 ct.AddRange(BuildControlTree(c))
51 Next
52 Return ct
53 End Function
54
55End Class
All you need is to put a reference into your config file:
1 <system.web>
2 <httpModules>
3 <add name="UnityHttpModule" type="Company.Product.UnityHttpModule, Company.Product"/>
4 </httpModules>
5 </system.web>
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…
1<unity>
2 <containers>
3 <container>
4 <types>
5 <type type="Company.Product.ViewModels.IRecentItemsViewModel, Company.Product" mapTo="Company.Product.ViewModels.RecentItemsViewModel, Company.Product" />
6 </types>
7 </container>
8 </containers>
9</unity>
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:
1<unity>
2 <containers>
3 <container>
4 <types>
5 <type type="Company.Product.ViewModels.IRecentItemsViewModel, Company.Product" mapTo="Company.Product.Hotfix1.ViewModels.RecentItemsViewModel, Company.Product.Hotfix1" />
6 </types>
7 </container>
8 </containers>
9</unity>
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…
Technorati Tags: Software Development .NET CodeProject WPF
No related videos found.
If you've made it this far, it's worth connecting with our principal consultant and coach, Martin Hinshelwood, for a 30-minute 'ask me anything' call.
We partner with businesses across diverse industries, including finance, insurance, healthcare, pharmaceuticals, technology, engineering, transportation, hospitality, entertainment, legal, government, and military sectors.
NIT A/S
CR2
NIT A/S