<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Brian Hartsock&#039;s Blog &#187; Design Patterns</title>
	<atom:link href="http://blog.brianhartsock.com/category/programming/design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.brianhartsock.com</link>
	<description>The exciting life of a software developer and nerd</description>
	<lastBuildDate>Thu, 29 Jul 2010 02:40:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Policy Inject Me</title>
		<link>http://blog.brianhartsock.com/2008/05/01/policy-inject-me/</link>
		<comments>http://blog.brianhartsock.com/2008/05/01/policy-inject-me/#comments</comments>
		<pubDate>Thu, 01 May 2008 17:00:35 +0000</pubDate>
		<dc:creator>bhartsock</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.brianhartsock.com/?p=95</guid>
		<description><![CDATA[I just started using the Policy Injection Application Block from Microsoft&#8217;s Enterprise library. At first, I thought it was worthless and overly complex. I was wrong. It is a very powerful programming tool that allows me to develop a lot faster and cleaner. Policy Injection is similar to aspect-oriented programming in that it helps to [...]]]></description>
			<content:encoded><![CDATA[<p>I just started using the Policy Injection Application Block from Microsoft&#8217;s <a href="http://www.codeplex.com/entlib">Enterprise library</a>.  At first, I thought it was worthless and overly complex.  I was wrong.  It is a very powerful programming tool that allows me to develop a lot faster and cleaner.</p>
<p>Policy Injection is similar to <a href="http://en.wikipedia.org/wiki/Aspect_%28computer_science%29">aspect-oriented programming</a> in that it helps to break away <a href="http://en.wikipedia.org/wiki/Cross-cutting_concern">cross-cutting concerns</a> (authorization, logging) from the actual business logic.  And yes, I basically copied that from the documentation.</p>
<p>Here is what policy injection means for me.  I can build business logic, which performs only what I want it to.  So I can have a function called <em>AddUser</em> and all that is done is the actual operation of adding that user.  I can then attach policies to that function, which specify how to authorize and log it.  This has a couple effects.  One, it reduces the amount of code which in turn cleans up the code.  Second, it allows me to customize cross-cutting concerns for different projects.  If I have an administrative application, I need to authorize differently than a user application.  Policy injection makes this possible without dirtying up the code.</p>
<p>After the initial few hours of getting used to using it, I am a huge proponent.  I definitely recommend checking it out.</p>
<p align="left"><a target="_blank" class="tt" href="http://twitter.com/home/?status=@brianhartsock+Policy+Inject+Me+http://bit.ly/xd6uu" title="Post to Twitter"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://blog.brianhartsock.com/2008/05/01/policy-inject-me/&amp;title=Policy+Inject+Me" title="Post to Digg"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://blog.brianhartsock.com/2008/05/01/policy-inject-me/&amp;t=Policy+Inject+Me" title="Post to Facebook"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-facebook-micro3.png" alt="Post to Facebook" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://blog.brianhartsock.com/2008/05/01/policy-inject-me/&amp;title=Policy+Inject+Me" title="Post to Reddit"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-reddit-micro3.png" alt="Post to Reddit" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.brianhartsock.com/2008/05/01/policy-inject-me/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NHibernate O/R Mapper: Part 1</title>
		<link>http://blog.brianhartsock.com/2007/12/10/nhibernate-or-mapper-part-1/</link>
		<comments>http://blog.brianhartsock.com/2007/12/10/nhibernate-or-mapper-part-1/#comments</comments>
		<pubDate>Mon, 10 Dec 2007 21:13:01 +0000</pubDate>
		<dc:creator>bhartsock</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://blog.brianhartsock.com/2007/12/10/nhibernate-or-mapper-part-1/</guid>
		<description><![CDATA[As most of you know, the data access layer is my favorite application layer to work with. For a while, I worked on my own active record implementation in PHP. After switching to .NET, my outlook has strayed away from the active record pattern though. I like using business entity objects that have no coupling [...]]]></description>
			<content:encoded><![CDATA[<p>As most of you know, the data access layer is my favorite application layer to work with.  For a while, I worked on my own active record implementation in PHP.  After switching to .NET, my outlook has strayed away from the active record pattern though.  I like using business entity objects that have no coupling to a database.  Instead, I use an object/relational mapper to translate from business entity to persisted database object.</p>
<p>At first, I started working on my own implementation again, which provided a simple, in code, mapping technique.  I quickly realized that mapping business entity relationships wasn&#8217;t easy though.  After a couple hours of staring at my monitor hoping I could come up with an elegant solution, I decided to look at some 3rd party mappers.  I had heard about <a href="http://www.nhibernate.org">NHibernate</a> before and decided to give it a shot.</p>
<p>I spent about two days prototyping part of my database in NHibernate and my first impression is wow.  Everything I could think of adding in an o/r mapper exists in NHibernate.  Lazy loading, relationships, inheritance, component mapping, and so much more is possible.  Over the past few weeks, my entire data access layer has been converted to NHibernate with overwhelming success.  Like any library, it has its own quarks, but for the most part it speeds up development time while adding lots of power to my applications.  I would recommend it to anyone.</p>
<p>NHibernate uses XML documents or .NET attributes to map an entity class to a database.  I never thought I would use XML again after painful VB6 experiences, but I decided to give it another shot.  To my surprise, I loved it.  It allows for really clean separation of the database and code, which basically means cleaner code and a happier Brian.  The only really annoying part of NHibernate is session management, but I&#8217;ll save that for part 2.</p>
<p align="left"><a target="_blank" class="tt" href="http://twitter.com/home/?status=@brianhartsock+NHibernate+O%2FR+Mapper%3A+Part+1+http://bit.ly/SHTd9" title="Post to Twitter"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://blog.brianhartsock.com/2007/12/10/nhibernate-or-mapper-part-1/&amp;title=NHibernate+O%2FR+Mapper%3A+Part+1" title="Post to Digg"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://blog.brianhartsock.com/2007/12/10/nhibernate-or-mapper-part-1/&amp;t=NHibernate+O%2FR+Mapper%3A+Part+1" title="Post to Facebook"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-facebook-micro3.png" alt="Post to Facebook" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://blog.brianhartsock.com/2007/12/10/nhibernate-or-mapper-part-1/&amp;title=NHibernate+O%2FR+Mapper%3A+Part+1" title="Post to Reddit"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-reddit-micro3.png" alt="Post to Reddit" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.brianhartsock.com/2007/12/10/nhibernate-or-mapper-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ADO.NET</title>
		<link>http://blog.brianhartsock.com/2007/09/06/adonet/</link>
		<comments>http://blog.brianhartsock.com/2007/09/06/adonet/#comments</comments>
		<pubDate>Thu, 06 Sep 2007 16:08:15 +0000</pubDate>
		<dc:creator>bhartsock</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://blog.brianhartsock.com/2007/09/06/adonet/</guid>
		<description><![CDATA[I recently started a new project here at Webmail.us which will be a .NET based web application. One of the core pieces of .NET web apps is, not surprisingly, ADO.NET. It is Microsoft&#8217;s upgrade from ADO, but it is more of a rewrite. Almost everything has been refactored, with many new abstractions and paradigms for [...]]]></description>
			<content:encoded><![CDATA[<p>I recently started a new project here at Webmail.us which will be a .NET based web application.  One of the core pieces of .NET web apps is, not surprisingly, ADO.NET.  It is Microsoft&#8217;s upgrade from ADO, but it is more of a rewrite.  Almost everything has been refactored, with many new abstractions and paradigms for how to access data.  When I first read about ADO.NET, I was very skeptical.  I didn&#8217;t understand the need for these complex, hard to understand abstractions like DataTable&#8217;s and DataRelation&#8217;s and DataSet&#8217;s.  It all seemed foreign to me for a multitude of reasons.  Unfortunately, Microsoft seems to be pushing DataSet&#8217;s so loudly, its hard to see what else is out there.</p>
<p>After a few days of digging though, I found a few great articles (<a href="http://www.informit.com/articles/article.aspx?p=31099">Choosing Data Containers for .NET</a>, <a href="http://msdn2.microsoft.com/En-US/library/aa479317.aspx">On the Way to Mastering ASP.NET: Introducing Custom Entity Classes</a>) on why not to use DataSets, as well as why you should.  DataSets were designed for rapid application development, and seems to be Microsoft&#8217;s answer to the Rails ActiveRecord pattern.  It is a quick and dirty way to build the data access layer for web applications.  To be more specific, by quick I mean it allows version 1.0 of a product to be released quickly, because a data abstraction already exists.  By dirty, I mean it isn&#8217;t an abstraction created for your application, it is a data abstraction created for any application.  </p>
<h4>Pro&#8217;s of ADO.net</h4>
<ul>
<li>Fast development time</li>
<li>In place architecture.<br />
Developing custom data access solutions can be very powerful, and very risky.  A bad design can lead to bad performance and is inherently error prone</li>
<li>Feature rich
<ul>
<li>Disconnected data</li>
<li>Concurrency</li>
<li>Many more&#8230;</li>
</ul>
</li>
<li>Easily serializable</li>
</ul>
<h4>Con&#8217;s of DataSets</h4>
<ul>
<li>Large memory footprint</li>
<li>Not object oriented.<br />
DataSet&#8217;s represent relation data, just like a database.  But I don&#8217;t want an in-memory representation of the Employee table, I want Employee objects.  <a href="http://www.hanselman.com/blog/PermaLink.aspx?guid=d88f7539-10d8-4697-8c6e-1badb08bb3f5">Scott Hanselman</a> sums it up pretty nicely:</p>
<blockquote><p>A DataSet is an object, right?  But it&#8217;s not a Domain Object, it&#8217;s not an &#8220;Apple&#8221; or &#8220;Orange&#8221; &#8211; it&#8217;s an object of type &#8220;DataSet.&#8221;  A DataSet is a bowl (one that knows about the backing Data Store).  A DataSet is an object that knows how to HOLD Rows and Columns.  It&#8217;s an object that knows a LOT about the Database.  But I don&#8217;t want to return bowls.  I want to return Domain Objects, like &#8220;Apples.&#8221;
</p></blockquote>
</li>
<li>Not abstracted.<br />
Directly connected with database data and structure.  So, any change to the database schema, could trickle down to the presentation layer.</li>
</ul>
<h4>Conclusion</h4>
<p>ADO.NET is a huge improvement from old ADO, but don&#8217;t buy into the entire framework.  Microsoft is trying to push DataSets but they might not always be the best solution.  For enterprise applications, I don&#8217;t think DataSet&#8217;s are the answer.  On the other hand, for smaller web application, it might be a perfect fit. </p>
<p align="left"><a target="_blank" class="tt" href="http://twitter.com/home/?status=@brianhartsock+ADO.NET+http://bit.ly/2lsN65" title="Post to Twitter"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://blog.brianhartsock.com/2007/09/06/adonet/&amp;title=ADO.NET" title="Post to Digg"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://blog.brianhartsock.com/2007/09/06/adonet/&amp;t=ADO.NET" title="Post to Facebook"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-facebook-micro3.png" alt="Post to Facebook" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://blog.brianhartsock.com/2007/09/06/adonet/&amp;title=ADO.NET" title="Post to Reddit"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-reddit-micro3.png" alt="Post to Reddit" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.brianhartsock.com/2007/09/06/adonet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend Framework 1.0</title>
		<link>http://blog.brianhartsock.com/2007/07/13/zend-framework-10/</link>
		<comments>http://blog.brianhartsock.com/2007/07/13/zend-framework-10/#comments</comments>
		<pubDate>Fri, 13 Jul 2007 16:33:37 +0000</pubDate>
		<dc:creator>bhartsock</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://blog.brianhartsock.com/2007/07/13/zend-framework-10/</guid>
		<description><![CDATA[A while back, I posted a review of some existing Active Record implementations. One of those happened to be the Zend frameworks DB portion. I believe that was about the time of version 0.7, and I recently looked on their website and discovered 1.0 had been released. I initially had low expectations, expecting 1.0 to [...]]]></description>
			<content:encoded><![CDATA[<p>A while back, I<a href="http://blog.brianhartsock.com/2007/02/19/active-record/"> posted a review</a> of some existing Active Record implementations.  One of those happened to be the Zend frameworks DB portion.  I believe that was about the time of version 0.7, and I recently looked on their <a href="http://framework.zend.com/home">website</a> and discovered 1.0 had been released.</p>
<p>I initially had low expectations, expecting 1.0 to just clean up 0.7 code, with minimal feature additions.  After I downloaded it, I found out how wrong I was.  The 1.0 release fixed every negative comment I had about 0.7.  Although it&#8217;s not perfect, it&#8217;s a lot better than it used to be.</p>
<h4>The Good..</h4>
<ul>
<li>Variables for defining which row and rowset classes to use in Zend_Db_Table.  This enables custom row and rowset classes which I believe is a must-have.</li>
<li>Support for compound primary keys</li>
<li>Ability to cache the table meta data and prevent unnecessary database access</li>
</ul>
<h4>The Bad&#8230;</h4>
<ul>
<li>Application level support for foreign keys, namely cascading deletes.  This feature is awesome since a lot of the tables I work with are myisam and don&#8217;t support cascading deletes.  Too bad it doesn&#8217;t truly cascade.  If you delete a <strong>single</strong> record, it will cascade that delete to all related tables, but no cascading after that.  Basically,  a worthless feature that causes more confusion than it helps.  Just use MySQL&#8217;s Innodb storage engine and clean up the code</li>
<li>Complexity.  Lots of classes and lots going on.</li>
</ul>
<p align="left"><a target="_blank" class="tt" href="http://twitter.com/home/?status=@brianhartsock+Zend+Framework+1.0+http://bit.ly/ZuDrd" title="Post to Twitter"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://blog.brianhartsock.com/2007/07/13/zend-framework-10/&amp;title=Zend+Framework+1.0" title="Post to Digg"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://blog.brianhartsock.com/2007/07/13/zend-framework-10/&amp;t=Zend+Framework+1.0" title="Post to Facebook"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-facebook-micro3.png" alt="Post to Facebook" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://blog.brianhartsock.com/2007/07/13/zend-framework-10/&amp;title=Zend+Framework+1.0" title="Post to Reddit"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-reddit-micro3.png" alt="Post to Reddit" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.brianhartsock.com/2007/07/13/zend-framework-10/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More Active Record Features</title>
		<link>http://blog.brianhartsock.com/2007/05/23/more-active-record-features/</link>
		<comments>http://blog.brianhartsock.com/2007/05/23/more-active-record-features/#comments</comments>
		<pubDate>Wed, 23 May 2007 19:46:00 +0000</pubDate>
		<dc:creator>bhartsock</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.brianhartsock.com/2007/05/23/more-active-record-features/</guid>
		<description><![CDATA[It has been a while since I have written about, or coded, my Active Record implementation. Basically, I have had too many other things to work on to enhance the functionality of an already working, albeit very beta, design. But, that doesn&#8217;t mean I haven&#8217;t been thinking of all the features I want to add [...]]]></description>
			<content:encoded><![CDATA[<p>It has been a while since I have written about, or coded, my Active Record implementation.  Basically, I have had too many other things to work on to enhance the functionality of an already working, albeit very beta, design.  But, that doesn&#8217;t mean I haven&#8217;t been thinking of all the features I want to add to it.</p>
<p>For many applications, the date created and date modified information for data is very useful, but annoying to set.  Since Active Record abstracts all access to a database, these fields can be set easily and transparently.  Many other implementations of the Active Record object already support this, so its not a new feature, but definitely a needed feature.  The main issue is what timezone the server is in since NOW() doesn&#8217;t return GMT.  It&#8217;s obvious that GMT is the preferred storage format of this data, so maybe passing a GMT date string from GMT is best.</p>
<p>Another feature that I just thought of is soft deletes.  Basically a soft delete is where the data isn&#8217;t actually deleted, but a flag is set to show that it is deleted.  This is very useful if you need to keep old data around for restores, or if you just want to have a record of everything.  One of our new hires MikeT brought it up to me since his project has a soft delete requirement.</p>
<p>Hopefully I can get these added into my implementation fairly quickly since they are so simple.  Even though features like this are just the tip of the iceberg, they are crucial for creating a viable Active Record design.</p>
<p align="left"><a target="_blank" class="tt" href="http://twitter.com/home/?status=@brianhartsock+More+Active+Record+Features+http://bit.ly/lNRQv" title="Post to Twitter"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://blog.brianhartsock.com/2007/05/23/more-active-record-features/&amp;title=More+Active+Record+Features" title="Post to Digg"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://blog.brianhartsock.com/2007/05/23/more-active-record-features/&amp;t=More+Active+Record+Features" title="Post to Facebook"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-facebook-micro3.png" alt="Post to Facebook" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://blog.brianhartsock.com/2007/05/23/more-active-record-features/&amp;title=More+Active+Record+Features" title="Post to Reddit"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-reddit-micro3.png" alt="Post to Reddit" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.brianhartsock.com/2007/05/23/more-active-record-features/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A better web framework &#8211; Part 1</title>
		<link>http://blog.brianhartsock.com/2007/04/09/a-better-web-framework-part-1/</link>
		<comments>http://blog.brianhartsock.com/2007/04/09/a-better-web-framework-part-1/#comments</comments>
		<pubDate>Tue, 10 Apr 2007 01:27:35 +0000</pubDate>
		<dc:creator>bhartsock</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.brianhartsock.com/?p=20</guid>
		<description><![CDATA[As I have mentioned before, design patterns are one of my favorite areas of computer science. Well, design patterns have a close association with web frameworks, so web frameworks are definitely another favorite area of mine. They both help speed up the development process by providing a structure for applications. Since Ruby on Rails took [...]]]></description>
			<content:encoded><![CDATA[<p>As I have mentioned before, design patterns are one of my favorite areas of computer science.   Well, design patterns have a close association with web frameworks, so web frameworks are definitely another favorite area of mine.  They both help speed up the development process by providing a structure for applications.  Since Ruby on Rails took off a couple years ago, other web frameworks have sprung up in many other languages.  None of these have met my needs at Webmail.us.</p>
<p>The webmail application I work on is very different than many other Web 2.0 applications, which is why most of the existing frameworks don&#8217;t work for us.</p>
<ul>
<li>There are no page refreshes and therefore:
<ul>
<li>Most drawing is done by Javascript</li>
<li>All data transfered is JSON encoded</li>
</ul>
</li>
<li>Email, Calendar, Tasks, Contacts, Settings are all combined into 1 application which means:
<ul>
<li>Large memory footprint</li>
<li>Javascript performance very important</li>
<li>On the fly loading of HTML, Javascript, and CSS is a must</li>
</ul>
</li>
</ul>
<p>For the past few months, I have been thinking about what I really need in a web framework.  Just like Rails, an MVC framework would need to be the base.  In Part 1, I will just talk about the MVC foundation for this framework.</p>
<h3>Model</h3>
<p>An ActiveRecord type object would provide the MySQL data abstraction.  I have mentioned that I have been working on my own implementation for a while, so I won&#8217;t go into too much detail on this.  Basically, for a complicated web application, most existing model abstractions don&#8217;t work very well.  With complicated JOIN&#8217;s, WHERE&#8217;s, and subquerys, something different must be used.</p>
<h3>View</h3>
<p>A templating mechanism must exist to have a complete web framework.  Current view abstractions won&#8217;t work in webmail.  Since webmail never refreshes, going to the server every time we need to populate a template is troublesome.  The templating mechanism in the better web framework would allow a template to be used in PHP or in Javascript.</p>
<p>If a template is used in Javascript, once it is downloaded once, it wouldn&#8217;t have to be downloaded again.  The hard part about a Javascript template is the inability to have loops and the need for slow regular expression parsing.</p>
<p>If a template request in Javascript has to make a server request every time a template needs to be populated, speed becomes an issue.  This is fine for large templates, but what about small widget templates?  Making a server request for a small element increases 1. latency and 2. server load which in turn increases latency even more.</p>
<h3>Controller</h3>
<p>Controllers are the backbone of a web framework.  I like how Rail works with a controller class where each data request is mapped to a function call.  This seems effective to me and needs little modification.</p>
<h3>Conclusion</h3>
<p>I know what you must be thinking: Your MVC design isn&#8217;t that much different than Rails.  Well, that&#8217;s true, but remember this is part 1.  The MVC design is just the foundation of a better web framework.  Rails is popular because it is useful, so it is definitely a good starting point for developing a new framework.</p>
<p>Shout out to Mike for his work on Javascript templates.</p>
<p align="left"><a target="_blank" class="tt" href="http://twitter.com/home/?status=@brianhartsock+A+better+web+framework+%E2%80%93+Part+1+http://bit.ly/RcbzK" title="Post to Twitter"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://blog.brianhartsock.com/2007/04/09/a-better-web-framework-part-1/&amp;title=A+better+web+framework+%E2%80%93+Part+1" title="Post to Digg"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://blog.brianhartsock.com/2007/04/09/a-better-web-framework-part-1/&amp;t=A+better+web+framework+%E2%80%93+Part+1" title="Post to Facebook"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-facebook-micro3.png" alt="Post to Facebook" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://blog.brianhartsock.com/2007/04/09/a-better-web-framework-part-1/&amp;title=A+better+web+framework+%E2%80%93+Part+1" title="Post to Reddit"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-reddit-micro3.png" alt="Post to Reddit" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.brianhartsock.com/2007/04/09/a-better-web-framework-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Active Record</title>
		<link>http://blog.brianhartsock.com/2007/02/19/active-record/</link>
		<comments>http://blog.brianhartsock.com/2007/02/19/active-record/#comments</comments>
		<pubDate>Tue, 20 Feb 2007 04:00:42 +0000</pubDate>
		<dc:creator>bhartsock</dc:creator>
				<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://blog.brianhartsock.com/?p=6</guid>
		<description><![CDATA[One of my favorite design patterns is the Active Record pattern. For those of you who haven&#8217;t heard of it, this pattern abstracts database access to a very simplistic level. I first starting playing with my own implementations a couple years ago, but it wasn&#8217;t until I used Ruby on Rails that I first saw [...]]]></description>
			<content:encoded><![CDATA[<p>One of my favorite design patterns is the <a href="http://en.wikipedia.org/wiki/ActiveRecord" target="_blank">Active Record</a> pattern.  For those of you who haven&#8217;t heard of it, this pattern abstracts database access to a very simplistic level.  I first starting playing with my own implementations a couple years ago, but it wasn&#8217;t until I used <a href="http://www.rubyonrails.com/" target="_blank">Ruby on Rails</a> that I first saw a fully functional implementation.</p>
<p>At Webmail.us, I have decided to use my side-project hours to work on my own version of the active record pattern.  It has been a slow going process, since I rarely find free hours to work on it, but I have finally created a workable version.  But, before we get to the inner workings of my implementation, I need to give credit where credit is due.</p>
<h4>Rails Active Record:</h4>
<p>Since the first active record pattern I encountered was the Rails implementation, I decided to start there with my first PHP implementation.  It wasn&#8217;t long before I realized  the PHP vs. Ruby differences were going to be a serious problem.</p>
<p>I wanted the <em>find</em> functions to be static, because an object shouldn&#8217;t have to populate itself after it has been instantiated.  Unfortunately, in PHP, static methods aren&#8217;t capable of polymorphism (which makes sense since $this isn&#8217;t applicable).  Therefore, overloading them becomes impossible.</p>
<p><a href="http://cakephp.org/" target="_blank">CakePHP</a> solved this by making all the find functions non-static, but I didn&#8217;t like this solution either, because it doesn&#8217;t make sense for the previously stated reasons.  Then I found &#8230;..</p>
<h4>Zend DB:</h4>
<p>The <a href="http://framework.zend.com" target="_blank">Zend framework</a>, while still very  new, has some powerful features, namely the DB section.  When I first saw this implementation of the active record pattern, I knew this was the starting point for my own version.</p>
<p>Zend DB has three main classes, Zend_Db_Table, Zend_Db_Table_Row, and Zend_Db_Table_Rowset.  This design is much more flexible, and makes more sense to me than Rails&#8217; and CakePHP&#8217;s implementations.   Each database object is abstracted to its own classes, aka tables, rows, and rowsets (or arrays of rows).</p>
<p>At first, I almost decided to use this implementation and forgo the long process of creating my own.  But, it fell short of my requirements for a few different reasons.  The biggest problem was that the Row and RowSet class names are hard-coded in the Table class.  This means overloading them is impossible:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Db_Table_Row<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'db'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_db<span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'table'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'data'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_fetch<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Row'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$where</span><span style="color: #339933;">,</span> <span style="color: #000088;">$order</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>It also uses prepared statements, which I have never used and don&#8217;t really have a desire to.  They are definitely slower than straight MySQL calls and I don&#8217;t have the mysqli extension installed so that is out  (Although I should probably work on getting that extension installed).</p>
<hr />Even though Rails, CakePHP, and Zend DB didn&#8217;t work out for me, they played in important role in leading me towards my current solution.  In the future, I will discuss in more depth my own implementation and keep everyone updated with how it is working out.</p>
<p align="left"><a target="_blank" class="tt" href="http://twitter.com/home/?status=@brianhartsock+Active+Record+http://bit.ly/z7x49" title="Post to Twitter"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://blog.brianhartsock.com/2007/02/19/active-record/&amp;title=Active+Record" title="Post to Digg"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://blog.brianhartsock.com/2007/02/19/active-record/&amp;t=Active+Record" title="Post to Facebook"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-facebook-micro3.png" alt="Post to Facebook" /></a> <a target="_blank" class="tt" href="http://reddit.com/submit?url=http://blog.brianhartsock.com/2007/02/19/active-record/&amp;title=Active+Record" title="Post to Reddit"><img class="nothumb" src="http://blog.brianhartsock.com/wp-content/plugins/tweet-this/icons/tt-reddit-micro3.png" alt="Post to Reddit" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.brianhartsock.com/2007/02/19/active-record/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
