Unity LifetimeManagers + ASP.NET

So recently I’ve really been looking into Unity and how to use it to handle IoC/DI for a future project of mine, I compeltely gave up on Web Client Software Factory (WCSF) something happened to that project where it just doesn’t work right anymore after having to deal with “Invalid owner for DynamicMethod” over and over again from ObjectBuilder(1.0 that’s in WCSF) I finally scrapped it and started looking at ways to use Unity to handle the MVP pattern instead.

This really had me scratching me on head on the implications of the page life cycle and correctly resolving instances, I’ve seen some examples around that go as far as creating HttpHandlers to intercept the BuildUp event to handle the injection which while I could go that route it definitely adds some complexity to the project. Which sadly I’ve learned especially when using new technology the code needs to be as simple as possible to avoid getting pushback from other developers but simple is good so I’d rather end up with a simple elegant solution vs a complex elegant solution.

Today I ran across a blog post (it’s a little old from May 08) regarding all of this Unity – lifetime management in a web application

I also did alot of referencing back and forth with the MSDN – Using Lifetime Managers

After doing some implemenation of all this I started to see why it makes sense to use the HttpModule to handle processing the build up. I used the HttpModule / Container wrapper extension method that Michael Puleio wrote on his blog Proof of Concept: a simple DI solution for ASP.NET WebForms.

Pretty soon I will be deploying a new solution to Assembla with links on here that will have an Unity driven DI model using the Model View Presenter (MVP) design pattern with a NHibernate back end (since LINQ-TO-SQL is on the death march) over the Microsoft Northwind db sample.

C# ASP.NET Response.Redirect Open Into New Window

UPDATED: 11/04/2008 (see bottom of post)

So I came across the day the desire to have a button open up a new window instead of just using an anchor tag, as usual I run the idea through google and mostly come back with responses saying it’s not possible that way, or to add javascript to the page to capture events. Not quite the solution I was looking for after doing some more searching I came across a javascript solution that would work but wasn’t elegant at all which led me to ponder on adapting it to be cleaner and this is what I came up with.

<asp:Button ID="btnNewEntry" runat="Server" CssClass="button" Text="New Entry"

OnClick="btnNewEntry_Click" OnClientClick="aspnetForm.target ='_blank';"/>

protected void btnNewEntry_Click(object sender, EventArgs e)

It even works using Server.Transfer. There you have it a simple unobtrusive solution to handle Response.Redirect into a new window without needing to add any javascript tags and only take advantage of the OnClientClick event on the button itself.

Update 10/22/2008:

I’d like to thank one of my commenters, Dan, for his solution for accessing the form inside a master page on the child page level.



This code will dig down into the controls collection and find your form and allow you to get access to the client id so you can be sure you have proper naming of it for the javascript to function correctly. Make sure you replace “form1″ with whatever you have your parent form id=”name” set to inside your Master’s page markup.

Update 11/04/2008:

If you have multiple buttons on a single page and only want a specific button to launch into a new windows and want the rest to stay on the current form make sure you decorate your other buttons like

<asp:Button ID="btnStayOnPage" runat="Server" CssClass="button" Text="Stay here"

OnClick="btnStayOnPage_Click" OnClientClick="aspnetForm.target ='_self';"/>

protected void btnStayOnPage_Click(object sender, EventArgs e)
   //Do normal code for postback


kick it on DotNetKicks.com