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.
UPDATED: 11/04/2008 (see bottom of post)
<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)
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.
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