Unity and ASP.NET

Book an Expert


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.


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


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…


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:


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…

Unity and ASP.NET was last modified: May 8th, 2009 by Martin Hinshelwood

-Every company deserves working software that successfully and consistently meets their customers needs on a regular cadence. We can help you get working software with continuous feedback so that your lean-agile teams can deliver continuous value with Visual Studio ALM, Team Foundation Server & Scrum. We have experts on hand to help improve your process and deliver more value at higher quality.

  • mobile application software deve

    That’s great, I never thought about Unity and ASP.NET like that before.

  • enterprise mobility solution

    I was just thinking about Unity and ASP.NET and you’ve really helped out. Thanks!

  • burning calories

    Thanks a lot for the interesting post man.

  • Casino Boni

    I’ve got a problem when using listview’s insertitem template wrappered in updatepanel, why I can’t check the source code part of “insert,cacel” segment in browser ? I mean if using updatepanel to enable Ajax, hit btnAdditem button, for example, then, a insert item template displays at the bottom of item template, then, maybe I want to add some calendarextender or jquery’s datepicker to some textbox (i.e, bind to the birthday field back in the database), because I can not find the source code of insertitem in browser, I even don’t know how to do next…..

  • automatenspielen

    puh, thanks got you posted that! The dependency injection is a quite cool trick and worked for me