<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Billy McCafferty : Contests, ASP.NET</title><link>http://devlicio.us/blogs/billy_mccafferty/archive/tags/Contests/ASP.NET/default.aspx</link><description>Tags: Contests, ASP.NET</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Integrate It! Coding Contest using Castle Project</title><link>http://devlicio.us/blogs/billy_mccafferty/archive/2007/05/02/integrate-it-coding-contest-w-castle-project.aspx</link><pubDate>Wed, 02 May 2007 23:08:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:24285</guid><dc:creator>Billy McCafferty</dc:creator><slash:comments>9</slash:comments><description>&lt;P&gt;[Updated May 9, 2007: Clarified contest&amp;nbsp;rules to&amp;nbsp;downgrade some "requirements" to be "suggestions" instead.&amp;nbsp; I.e., there's now a little more wiggle room in the implementation details.]&lt;/P&gt;
&lt;P&gt;A contest guaranteed to challenge you!&amp;nbsp; This contest involves integrating Castle Project facilities into an existing project that's pre-wired for &lt;A class="" href="http://www.castleproject.org/container/index.html"&gt;Castle Project Windsor&lt;/A&gt;.&amp;nbsp; Have you wanted to try out some aspect oriented programming?&amp;nbsp; This just&amp;nbsp;&lt;EM&gt;may&lt;/EM&gt; be a good place to use it.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Background &amp;amp; Problem&lt;/STRONG&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the article, &lt;A class="" href="http://www.codeproject.com/aspnet/NHibernateBestPractices.asp"&gt;NHibernate Best Practices with ASP.NET&lt;/A&gt;, the &lt;A class="" href="http://www.hibernate.org/43.html"&gt;open-session-in-view&lt;/A&gt;&amp;nbsp;technique is employed to begin a transaction at the beginning of each HTTP request, and then to commit the transaction at the end of the request.&amp;nbsp; There are a&amp;nbsp;few drawbacks associated with this:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;If you don't need a transaction, or if you don't even need to talk to the database, you've wasted a bit of&amp;nbsp;useless transactional&amp;nbsp;overhead.&lt;/LI&gt;
&lt;LI&gt;Having transactions automatically started in an HTTP module can unintentionally&amp;nbsp;begin transactions for&amp;nbsp;unintended items such as images and CSS files&amp;nbsp;depending on IIS settings.&amp;nbsp; (This can also be coded around in the HTTP module, but it's still less than ideal IMO.)&lt;/LI&gt;
&lt;LI&gt;Since the transaction encompasses the entire HTTP request from start to finish, it's difficult to&amp;nbsp;center&amp;nbsp;the transaction to&amp;nbsp;only a&amp;nbsp;specific section of code.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;On the flip side, using an HTTP module greatly simplifies the process of managing the transaction.&amp;nbsp; A few challenges when not using an HTTP module include:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;How can a transaction be started from a project layer without talking to an NHibernate manager class directly?&lt;/LI&gt;
&lt;LI&gt;Assume a page communicates with two databases at the same time.&amp;nbsp; How can a transaction be cleanly started for one database communication but not the other?&lt;/LI&gt;
&lt;LI&gt;How can the transaction be managed to begin at the start of a particular method and committed at the end of the method, again, without talking to an NHibernate manager class directly?&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;The &lt;A class="" href="http://wiki.castleproject.org/index.php/Facility:Automatic_Transaction_Management"&gt;Castle Project Automatic Transaction Management facility&lt;/A&gt; is about the cleanest approach to transaction management that I have found.&amp;nbsp; With this facility, in conjunction with the &lt;A class="" href="http://wiki.castleproject.org/index.php/Services:Transaction"&gt;Castle Project Transaction service&lt;/A&gt;, it's simple to mark a class as transactional and a particular method as using a transaction, as demonstrated in the following sample code:&lt;/P&gt;
&lt;P&gt;&lt;TEXTAREA class=c#:nogutter name=code rows=4 cols=50&gt;[Transactional]
public class EditCustomerPresenter {
  [Transaction(TransactionMode.Requires)]
  public void Update(long customerId) {
    // Do stuff with the DB
  }
}&lt;/TEXTAREA&gt; &lt;/P&gt;
&lt;P&gt;This contest involves brining&amp;nbsp;the benefits of the Automatic Transaction Management facility to the "enterprise" NHibernate sample code included in &lt;A class="" href="http://www.codeproject.com/aspnet/NHibernateBestPractices.asp"&gt;NHibernate Best Practices with ASP.NET&lt;/A&gt;.&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Your Incentive&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Up for grabs with this contest is the following book.&lt;/P&gt;
&lt;P&gt;&lt;IMG alt="Design Patterns: Elements of Reusable Object-Oriented Software" src="http://www.awprofessional.com/ShowCover.asp?isbn=0321399838&amp;amp;type=c" width=125&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.awprofessional.com/bookstore/product.asp?isbn=0321399838&amp;amp;rl=1"&gt;Essential Windows Workflow Foundation&lt;/A&gt;&lt;BR&gt;By Dharma Shukla, Bob Schmidt.&lt;/P&gt;
&lt;P&gt;The prize winning code will also be integrated into the NHibernate Best Practices article's "enterprise" sample project with plenty of accolades for the author in the&amp;nbsp;explanatory comments.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Instructions to Enter&lt;/STRONG&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Make sure you have NUnit installed; available at &lt;A href="http://www.nunit.org/" target=_blank&gt;http://www.nunit.org/&lt;/A&gt;.&lt;/LI&gt;
&lt;LI&gt;Download the NHibernate Best Practice article's &lt;A class="" href="http://www.codeproject.com/aspnet/NHibernateBestPractices/EnterpriseSample.zip"&gt;"enterprise" sample code&lt;/A&gt;&amp;nbsp;from CodeProject.&lt;/LI&gt;
&lt;LI&gt;Follow the &lt;A class="" href="http://www.codeproject.com/aspnet/NHibernateBestPractices.asp#EXTENDING_BASICS"&gt;steps described in the article&lt;/A&gt; to get the sample code up and running.&lt;/LI&gt;
&lt;LI&gt;Open NUnit, go to File/Open, then open &amp;lt;solution directory&amp;gt;/EnterpriseSample.Tests/bin/Debug/EnterpriseSample.Tests.dll. Click run and make sure all the tests pass.&amp;nbsp; (You may need to add an assembly redirect for your version of NUnit; an &lt;A class="" href="http://www.codeproject.com/aspnet/NHibernateBestPractices.asp?df=100&amp;amp;forumid=278860&amp;amp;noise=5&amp;amp;exp=0&amp;amp;mpp=50&amp;amp;select=2017728#xx2017728xx"&gt;example assembly redirect is described in the article's comments&lt;/A&gt;.)&lt;/LI&gt;
&lt;LI&gt;Complete the&amp;nbsp;integration requirements&amp;nbsp;as described below. Be sure to follow the style of the code that is already in place: classes, methods and public properties in PascalCase, private members in camelCase, etc. Following the style that's already there is always a good rule of thumb when modifying legacy code.&lt;/LI&gt;
&lt;LI&gt;When finished with the integration, make sure all the unit tests are still passing and that no test is being ignored, zip up your solution and send to &lt;A href="mailto:challenge4@emccafferty.com"&gt;challenge4@emccafferty.com&lt;/A&gt;. If you want to submit your code, but don't want to be considered for the prize, please&amp;nbsp;let me know.&amp;nbsp; The due date for your submission is&amp;nbsp;&lt;SPAN style="FONT-WEIGHT:bold;COLOR:red;"&gt;Monday morning&amp;nbsp;at 8 AM, May 14, US mountain time&lt;/SPAN&gt;.&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;The first correct solution which, in my completely subjective opinion, integrates Castle Project in&amp;nbsp;the cleanest and most simple (to use) manner, will win the contest.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Now Integrate It!&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Below is a listing of requirements and/or suggestions for your solution.&amp;nbsp; "Requirements" need to be included in your solution.&amp;nbsp; "Suggestions" need not be&amp;nbsp;implemented exactly as described&amp;nbsp;if you find a better way, in your opinion, to fulfill the stated goals.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;EM&gt;Required&lt;/EM&gt;:&amp;nbsp; The EnterpriseSample.Presenters.EditCustomerPresenter.Update method must be wrapped within a transaction.&amp;nbsp; This is the ONLY method which needs to have a transaction applied to it.&lt;/LI&gt;
&lt;LI&gt;&lt;EM&gt;Required&lt;/EM&gt;:&amp;nbsp; The transaction&amp;nbsp;may not be started and stopped within the ICustomerDao data-access object.&amp;nbsp; E.g., adding NHibernateSessionManager.BeginTransaction &amp;amp; CommitTransaction to the DAO is not permitted.&lt;/LI&gt;
&lt;LI&gt;&lt;EM&gt;Required&lt;/EM&gt;:&amp;nbsp; The EnterpriseSample.Presenters and EnterpriseSample.Web&amp;nbsp;projects must remain ignorant of NHibernateSessionManager.&lt;/LI&gt;
&lt;LI&gt;&lt;EM&gt;Required&lt;/EM&gt;:&amp;nbsp; The transaction may not be &lt;EM&gt;started&lt;/EM&gt; in any&amp;nbsp;IHttpModule, such as&amp;nbsp;ProjectBase.Data.NHibernateSessionModule, the HTTP module which commits any open transactions.&amp;nbsp; But it's OK if this HTTP module is still used to commit any transactions.&amp;nbsp; (You'll note, in the sample code, that the code which would usually start the transaction has been commented out.)&lt;/LI&gt;
&lt;LI&gt;&lt;EM&gt;Required&lt;/EM&gt;:&amp;nbsp; As the EnterpriseSample code is configured to work with multiple databases, your solution must be able to support multiple databases as well.&amp;nbsp; An example would be [Transaction("TheDbsUniqueIdentifier")].&amp;nbsp; This example is only a suggestion and is not required.&amp;nbsp; What &lt;EM&gt;is&lt;/EM&gt; required is that an easy way exists to have the transaction started for one database/session factory&amp;nbsp;and not for another.&lt;/LI&gt;
&lt;LI&gt;&lt;EM&gt;Required&lt;/EM&gt;:&amp;nbsp; Unit tests must be included to test your solution.&lt;EM&gt;&amp;nbsp;&lt;/EM&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;EM&gt;Suggestion&lt;/EM&gt;:&amp;nbsp; Beginning a transaction may be done using an attribute such as [Transaction] or by using aspect oriented programming to intercept the method and begin the transaction.&amp;nbsp; Although not required, the attribute approach would be preferred for the purposes of &lt;A class="" href="http://www.martinfowler.com/ieeeSoftware/explicit.pdf"&gt;being explicit&lt;/A&gt;.&amp;nbsp; If you use an AOP approach, leverage &lt;A class="" href="http://www.castleproject.org/aspectsharp/index.html"&gt;Castle Project's Aspect#&lt;/A&gt;&lt;EM&gt;.&lt;/EM&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;EM&gt;Suggestion&lt;/EM&gt;:&amp;nbsp; The transaction&amp;nbsp;may ultimately be started and stopped via the existing ProjectBase.Data.NHibernateSessionManager class.&amp;nbsp; If your solution requires working around the NHibernateSessionManager to support multiple databases, then that's alright.&lt;/LI&gt;
&lt;LI&gt;&lt;EM&gt;Suggestion&lt;/EM&gt;:&amp;nbsp; Castle Project's Automatic Transaction Management facility, or a customized variant, &lt;STRONG&gt;&lt;EM&gt;may&lt;/EM&gt; be leveraged&lt;/STRONG&gt; but is not required.&amp;nbsp; You're certainly welcome to write your own handler code from the ground up.&lt;/LI&gt;
&lt;LI&gt;&lt;EM&gt;Suggestion&lt;/EM&gt;:&amp;nbsp; Your transaction management code for handling the attributes should be placed into the project ProjectBase.Data in a new folder called TransactionMgmt.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;As a bonus, and added brownie points for&amp;nbsp;getting the prize,&amp;nbsp;develop your solution to commit the transaction immediately after&amp;nbsp;the method which has the transaction attribute declared on top of it.&amp;nbsp; So the transaction will begin at the top of&amp;nbsp;the method and&amp;nbsp;commit as soon as the method completes.&amp;nbsp; Your welcome to use various approaches to this including, but not limited to, attributes to commit the transaction and/or aspect oriented programming (AOP) to commit the transaction when the method completes.&amp;nbsp; Again, if you use an AOP approach, leverage &lt;A class="" href="http://www.castleproject.org/aspectsharp/index.html"&gt;Castle Project's Aspect#&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Why?&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;This challenge has been incredibly fun for me to work on.&amp;nbsp; It's&amp;nbsp;far from trivial&amp;nbsp;and requires you to use a variety of techniques to integrate successfully.&amp;nbsp; Furthermore, Castle Project is a phenomenal grouping of utilities which needs to get greater attention around the development community.&amp;nbsp; Not only can they be used out of the box, but they&amp;nbsp;can also be extended to other purposes...which is&amp;nbsp;one of the challenges here. &amp;nbsp;Consequently, I hope this challenge will give you something fun to work on while learning a few things in the process.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Where to go for Help&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;The following resources should help explain the code you'll be working with better along with possible solution paths for you to take:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;NHibernate Best Practices article:&amp;nbsp; &lt;A href="http://www.codeproject.com/aspnet/NHibernateBestPractices.asp"&gt;http://www.codeproject.com/aspnet/NHibernateBestPractices.asp&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;Castle Project Facilities:Automatic Transaction Management:&amp;nbsp; &lt;A href="http://wiki.castleproject.org/index.php/Facility:Automatic_Transaction_Management"&gt;http://wiki.castleproject.org/index.php/Facility:Automatic_Transaction_Management&lt;/A&gt;&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;Castle Project Services:Transaction:&amp;nbsp; &lt;A href="http://wiki.castleproject.org/index.php/Services:Transaction"&gt;http://wiki.castleproject.org/index.php/Services:Transaction&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;Castle Project Aspect#:&amp;nbsp; &lt;A href="http://www.castleproject.org/aspectsharp/index.html"&gt;http://www.castleproject.org/aspectsharp/index.html&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;"The joys of Castle.Services.Transaction":&amp;nbsp; &lt;A href="http://jroller.com/page/hammett?entry=the_joys_of_castle_services"&gt;http://jroller.com/page/hammett?entry=the_joys_of_castle_services&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;Castle Project Forums:&amp;nbsp; &lt;A href="http://forum.castleproject.org/index.php"&gt;http://forum.castleproject.org/index.php&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;NHibernate Forums:&amp;nbsp; &lt;A href="http://forum.hibernate.org/viewforum.php?f=25"&gt;http://forum.hibernate.org/viewforum.php?f=25&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Good luck and let me know if you have any questions!&lt;/P&gt;
&lt;P&gt;Billy McCafferty&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=24285" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/billy_mccafferty/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://devlicio.us/blogs/billy_mccafferty/archive/tags/Contests/default.aspx">Contests</category><category domain="http://devlicio.us/blogs/billy_mccafferty/archive/tags/NHibernate/default.aspx">NHibernate</category><category domain="http://devlicio.us/blogs/billy_mccafferty/archive/tags/Castle+Project/default.aspx">Castle Project</category></item></channel></rss>