<?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>Software Rockstar &#187; OOA/OOD</title>
	<atom:link href="http://www.softwarerockstar.com/tag/ooaood/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.softwarerockstar.com</link>
	<description>Coaching and mentoring on a journey from a Developer to an IT Leader</description>
	<lastBuildDate>Tue, 13 Sep 2011 17:18:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>POCO in ADO.NET Entity Framework 4</title>
		<link>http://www.softwarerockstar.com/2011/01/poco-in-ado-net-entity-framework-4/</link>
		<comments>http://www.softwarerockstar.com/2011/01/poco-in-ado-net-entity-framework-4/#comments</comments>
		<pubDate>Tue, 18 Jan 2011 22:45:42 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[ADO.NET]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Change]]></category>
		<category><![CDATA[data access technologies]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[entity models]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[loading]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[object relational mapping]]></category>
		<category><![CDATA[OOA/OOD]]></category>
		<category><![CDATA[OR/M]]></category>
		<category><![CDATA[oriented domain]]></category>
		<category><![CDATA[POCO]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[trivial data]]></category>

		<guid isPermaLink="false">http://www.softwarerockstar.com/?p=625</guid>
		<description><![CDATA[ADO.NET Entity Framework means many things to many people.  At it’s core, however, it allows for Object Relational Mapping (ORM) by providing a layer of abstraction between relational databases and Object Oriented domain objects. The first version of Entity Framework was introduced with .NET Framework 3.5 SP1 and Visual Studio 2008 SP1.  While the intention [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/en-us/library/aa697427(v=vs.80).aspx">ADO.NET Entity Framework</a> means many things to many people.  At it’s core, however, it allows for Object Relational Mapping (ORM) by providing a layer of abstraction between relational databases and Object Oriented domain objects.</p>
<p>The first version of Entity Framework was introduced with .NET Framework 3.5 SP1 and Visual Studio 2008 SP1.  While the intention was great, most of everyone that I know, including my own self who have ever tried using EFv1 for anything except trivial data access have developed a love-and-hate relationship with it.  The first version looked great at first, but turned out to be an immature product at best with severe restrictions as well as quite a few bugs.</p>
<p>With .NET 4 Microsoft released a new version of Entity Framework that has addressed many of the issues and made it a much more stable technology.  Amongst other things, Microsoft heard developer community at large and provided POCO support in EFv4.</p>
<p><strong>POCO Support &#8211; At Last!</strong></p>
<p>The biggest problem with EFv1 was that the domain classes had to be tightly integrated with EF.  Essentially these classes were generated from entity models and developers had no control over code generation.  This limitation didn’t fly very well with most developers, since we like to keep our domain classes generic enough to where we can easily migrate them to any underlying data access technology.  This is especially true since the data access technologies tend to change every so often.</p>
<p>In EFv4 Microsoft has gone to great lengths to support Plain Old CLR Objects (POCO), which is just a fancy name for simple .NET classes with properties that can be used as Data Transfer Objects (DTO) or simply Domain Objects.</p>
<p><a href="http://en.wikipedia.org/wiki/Plain_Old_CLR_Object">POCO</a> classes can be handwritten and easily hooked in to EFv4.  Better yet, EFv4 allows us to generate these classes based on our entity models.  Also we have complete control over code generation since EFv4 uses <a href="http://msdn.microsoft.com/en-us/library/bb126445.aspx">T4 templates</a>.  Speaking of <a href="http://msdn.microsoft.com/en-us/library/bb126445.aspx">T4 templates</a>, they can also be used to generate <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.aspx">ObjectContext</a> classes for our entity models.</p>
<p><strong>Lazy Loading, Explicit Loading, and Eager Loading</strong></p>
<p>In EFv1 the only way to automatically load related entities was using eager loading.  Essentially eager loading is a <a href="http://www.google.com/url?sa=t&amp;source=web&amp;cd=1&amp;ved=0CCgQFjAA&amp;url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Fnetframework%2Faa904594&amp;ei=JcY0Tb32HsqWhQfy69S9Cw&amp;usg=AFQjCNFXPCT1aJ1K461wVDeLDLlRwCB7Vw&amp;sig2=yZcOmHzhHUottOaC-SApQA">LINQ</a> technique that allows loading related entities in one shot using the Include method on the LINQ query.</p>
<p>EFv4 allows for lazy loading on POCO’s.  The only requirement for this added functionality is that navigation properties on POCO’s be marked as virtual and <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontextoptions.lazyloadingenabled.aspx">LazyLoadingEnabled</a> property of <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontextoptions.aspx">ObjectContextOptions</a> be set to true.  The way EFv4 does this is very cool.  Essentially it generates dynamic proxies overriding the virtual properties on POCO’s and uses those proxies instead of the POCO classes at runtime.</p>
<p>In addition to eager and lazy loading, EFv4 allows explicit loading of related entities.  Explicit loading allows us to explicitly load related entities on demand whenever the need arises.  Using explicit loading is as easy as calling the <a href="http://msdn.microsoft.com/en-us/library/dd382880.aspx">LoadProperty</a> method of the <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.aspx">ObjectContext</a> using  the parent entity as well as the navigation property name as parameters.</p>
<p><strong>Change Tracking</strong></p>
<p>EFv4 supports two different types of change tracking which are as follows:</p>
<p><strong>Snapshot Change Tracking</strong>.  Using this kind of change tracking is more efficient in most cases but it might require a few additional lines of code.  When using snapshot change tracking (default behavior in EFv4), the developer must make explicit call to <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.aspx">ObjectContext</a>’s <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.detectchanges.aspx">DetectChanges</a> method in order for the EF change tracker to notice any data that has been changed on the entity since it was first retrieved.  Also calling the <a href="http://www.google.com/url?sa=t&amp;source=web&amp;cd=1&amp;sqi=2&amp;ved=0CBMQFjAA&amp;url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fbb336792.aspx&amp;ei=Kcc0TcoZg8yEB7fe0M8L&amp;usg=AFQjCNE7rfcpXzJatP4e27jk4gjOGewMGg&amp;sig2=xpf8b9OofvMUSm3GB8arTQ">SaveChanges</a> method of the context automatically detects any changes to entities involved.</p>
<p><strong>Real-time Change Tracking</strong>.  Using real-time change tracking the tracker keeps track of all data changes to entities in real-time.  For real-time change tracking to work for POCO’s, all properties of POCO’s should be marked as virtual.  Also any navigation properties should have return types of <a href="http://www.google.com/url?sa=t&amp;source=web&amp;cd=1&amp;ved=0CBYQFjAA&amp;url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2F92t2ye13.aspx&amp;ei=vcc0TeGPL4uwhQfRl728Cw&amp;usg=AFQjCNH9Rx-AertMm5Z70BoXDKVKjEf7zg&amp;sig2=qZlGJ7KMoxVlnZnGtwxeLw">ICollection&lt;T&gt;</a>.  Once these two conditions are met, EFv4 automatically generates dynamic proxies and uses them instead of the POCO’s and tracks changes made to data contained within those entities in real time.</p>
<p><strong>Design Techniques</strong></p>
<p>EFv4 allows a couple of different design techniques that ought to satisfy most situations.</p>
<p><strong>Model First</strong>.  For new projects where that database design is not already set in stone or is non-existent, model-first approach is more natural to most developers and makes a lot of sense.  What this entails is that the developer creates an entity model using the <a href="http://www.google.com/url?sa=t&amp;source=web&amp;cd=1&amp;ved=0CBoQFjAA&amp;url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fcc716685.aspx&amp;ei=K8g0TYjSF4GGhQebm-CYCw&amp;usg=AFQjCNEisyxXXpKQ55JXj6LiDlN9upsjXg&amp;sig2=jiah8MV9pRlpkhyEbdcjLA">Entity Model Designer</a> for the domain objects involved.  This model can be easily changed during the design phase or the early development stages.  Once the architect/developer is happy with the entity design, he/she can use EFv4 to generate the data model using EFv4’s Data Definition Language (DDL) generation capabilities.  Not only that this is a more natural way of designing data access and domain model for a given solution, it’s more agile in that the model can easily be modified using a GUI tool.  Also as long as the underlying database is supported by EFv4, the developer need not worry about the exact syntax of the DDL as it can automatically be generated.  Once again the generation of DDL can be influenced using <a href="http://msdn.microsoft.com/en-us/library/bb126445.aspx">T4 templates</a>.</p>
<p><strong>Database First</strong>.  This technique is more suitable for when a project involves an existing database.  In such cases EF can easily generate domain models based on database schema.  This design technique is not new to EFv4 as it has been available since the first version of EF and this is probably what most of us are most familiar with.</p>
<p><strong>Code Only</strong>.  EFv4 introduced another design technique which requires no models to be created.  Essentially there are only code classes and EF infers the model from them.  Entire database including all required tables can be generated by EF at run-time.</p>
<p>There’s a lot more to EFv4.  What I have covered in this article only addresses a bit of POCO support in EFv4 since that has been the number 1 request of the development community since EFv1 was released.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2011/01/poco-in-ado-net-entity-framework-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Go &#8211; A New Programming Language</title>
		<link>http://www.softwarerockstar.com/2009/11/google-go-a-new-programming-language/</link>
		<comments>http://www.softwarerockstar.com/2009/11/google-go-a-new-programming-language/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 18:55:36 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[OOA/OOD]]></category>
		<category><![CDATA[Personal Development]]></category>

		<guid isPermaLink="false">http://www.softwarerockstar.com/?p=549</guid>
		<description><![CDATA[Google just released Go as a new open source programming language that aims to simplify the complex software development model of modern programming languages, especially in the areas of dependency management and multi-threading. In the works at Google for the last two years, Go was originally developed as a systems programming language but Google would [...]]]></description>
			<content:encoded><![CDATA[<div class="imageleft"><img src="http://softwarerockstar.com/wp-content/uploads/2009/11/go-logo-black1.png" width="195" /></div>
<p>Google just released <a href="http://golang.org/">Go</a> as a new open source programming language that aims to simplify the complex software development model of modern programming languages, especially in the areas of dependency management and multi-threading.<span id="more-549"></span></p>
<p>In the works at Google for the last two years, Go was originally developed as a systems programming language but Google would like to see it branched into other areas as well.  By releasing it into the open source world, Google is counting on the huge open source community at large to develop libraries and other utilities for Go that would make it appealing to a much wider audience.</p>
<p>Go is advertised to be:</p>
<p><strong>Fast</strong><br />
Typical builds take fraction of a second</p>
<p><strong>Safe</strong><br />
Memory is garbage collected</p>
<p><strong>Concurrent</strong><br />
Run thousands of lightweight communicating processes without worrying about stack overflows</p>
<p>You can download Go and get more information on it at <a href="http://golang.org/">http://golang.org/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2009/11/google-go-a-new-programming-language/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The Castle Project</title>
		<link>http://www.softwarerockstar.com/2007/08/the-castle-project/</link>
		<comments>http://www.softwarerockstar.com/2007/08/the-castle-project/#comments</comments>
		<pubDate>Fri, 17 Aug 2007 20:37:00 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[article series]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[castle project]]></category>
		<category><![CDATA[Design Principles]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[inversion of control]]></category>
		<category><![CDATA[ioc]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[OOA/OOD]]></category>

		<guid isPermaLink="false">http://mharoon.wordpress.com/2007/08/17/the-castle-project/</guid>
		<description><![CDATA[In the last couple of years I have noticed a steadily growing interest in the .NET community around the&#160;Inversion of Control (IoC) and Dependency Injection (DI) patterns.&#160; This is partly due to the fact that a number of excellent frameworks have emerged and gained popularity that allow us to use these designs in our applications [...]]]></description>
			<content:encoded><![CDATA[<div class="imageleft"><img src="http://softwarerockstar.com/wp-content/uploads/2007/08/castleinabox1.gif" alt="Castle Project" title="Castle Project" width="191" height="108" /></div>
<p>In the last couple of years I have noticed a steadily growing interest in the .NET community around the&nbsp;<a href="http://en.wikipedia.org/wiki/Inversion_of_control">Inversion of Control (IoC)</a> and <a href="http://en.wikipedia.org/wiki/Dependency_injection">Dependency Injection (DI)</a> patterns.&nbsp; This is partly due to the fact that a number of excellent frameworks have emerged and gained popularity that allow us to use these designs in our applications relatively painlessly.&nbsp; One such framework is the open source <a href="http://www.castleproject.org/">Castle Project</a>.&nbsp; In my opinion <a href="http://www.castleproject.org/">Castle</a> is the best implementation of IoC and DI among many others that I have come across including <a href="http://www.springframework.net/">Spring.NET</a>, <a href="http://ninject.org/">NInject</a>, and&nbsp;<a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=ObjectBuilder">ObjectBuilder</a>.&nbsp; This is however my opinion; which framework you use largely depends upon your requirements and your preferences.</p>
<p>If you are just getting started with Castle or are curious about why the heck one should use IoC and DI, I strongly suggest you check out the <a href="http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart1.aspx">3-part article series</a> by&nbsp;<a href="http://dotnetslackers.com/community/blogs/simoneb">Simone Busoli</a>.&nbsp; I just came across <a href="http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart1.aspx">these articles</a> and I can&#8217;t stress enough how well they are written.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2007/08/the-castle-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ISP: Interface Segregation Principle</title>
		<link>http://www.softwarerockstar.com/2006/08/isp-interface-segregation-principle/</link>
		<comments>http://www.softwarerockstar.com/2006/08/isp-interface-segregation-principle/#comments</comments>
		<pubDate>Tue, 29 Aug 2006 00:10:00 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Agile]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Design Principles]]></category>
		<category><![CDATA[OOA/OOD]]></category>

		<guid isPermaLink="false">http://mharoon.wordpress.com/2006/08/28/isp-interface-segregation-principle/</guid>
		<description><![CDATA[The Interface Segregation Principle (ISP) deals with cohesion and is closely related to the Single Responsibility Principle (SRP). In Uncle Bob&#8217;s terms, the ISP states that: Clients should not be forced to depend on methods that they do not use. In other words, this principle states that if a class performs multiple functions that may [...]]]></description>
			<content:encoded><![CDATA[<p>The Interface Segregation Principle (ISP) deals with <a href="http://en.wikipedia.org/wiki/Cohesion_(computer_science)">cohesion</a> and is closely related to the <a href="http://mfharoon.blogspot.com/2006/08/srp-single-responsibility-principle.html">Single Responsibility Principle (SRP)</a>. In <a href="http://www.objectmentor.com/resources/articles/isp.pdf">Uncle Bob&#8217;s terms</a>, the ISP states that:</p>
<blockquote><p>Clients should not be forced to depend on methods that they do not use.</p></blockquote>
<p>In other words, this principle states that if a class performs multiple functions that may be used by different client classes, then those functions should be seperated out into different interfaces according to their usage.</p>
<p>The idea behind this principle is to confine the aount of rework to only clients that use a certain interface, should the interface of a depended-upon class changes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2006/08/isp-interface-segregation-principle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DIP: Dependency Inversion Principle</title>
		<link>http://www.softwarerockstar.com/2006/08/dip-dependency-inversion-principle/</link>
		<comments>http://www.softwarerockstar.com/2006/08/dip-dependency-inversion-principle/#comments</comments>
		<pubDate>Sun, 27 Aug 2006 13:58:00 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Agile]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[business entities]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[default implementation]]></category>
		<category><![CDATA[definition]]></category>
		<category><![CDATA[Design Principles]]></category>
		<category><![CDATA[domino effect]]></category>
		<category><![CDATA[level modules]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[OOA/OOD]]></category>
		<category><![CDATA[Principle]]></category>
		<category><![CDATA[principle deals]]></category>
		<category><![CDATA[Uncle]]></category>
		<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://mharoon.wordpress.com/2006/08/27/dip-dependency-inversion-principle/</guid>
		<description><![CDATA[The Dependency Inversion Principle deals with how to correctly design classes such that their dependency on one another causes the least amount of work in case of a change. Uncle Bob&#8217;s definition of DIP states: High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend on details. Details [...]]]></description>
			<content:encoded><![CDATA[<p>The Dependency Inversion Principle deals with how to correctly design classes such that their dependency on one another causes the least amount of work in case of a change. <a href="http://www.objectmentor.com/resources/articles/dip.pdf">Uncle Bob&#8217;s definition</a> of DIP states:</p>
<blockquote>
<ul>
<li>High-level modules should not depend on low-level modules. Both should depend on abstractions.</li>
<li>Abstractions should not depend on details. Details should depend on abstractions.</li>
</ul>
</blockquote>
<p>Essentially what this principle means is that in a tiered design, higher level modules and lower level modules should not directly depend on each other; instead they should only depend on <a href="http://en.wikipedia.org/wiki/Abstraction">abstractions</a>. Moreover, abstractions should not have any knowledge of details (other classes in the system).</p>
<p>This principle closely relates to <a href="http://mfharoon.blogspot.com/search?label=Design+Principles">some of the other design principles</a> I discussed previously in that it yields a design that is highly decoupled ensuring that minor changes in one part of the application do not cause a domino effect in other parts of the application. Moreover such a design is extensible &#8212; as new business entities are added, usually such as design scales well by requiring only new code to be added rather than existing code to be modified.  ASP.NET 2.0&#8242;s <a href="http://aspnet.4guysfromrolla.com/articles/101905-1.aspx">Provider Model</a> is a great example of such a design that provides a default implementation for various sub-systems such as Membership, Personalization, Navigation, etc. but also allows developers to modify the default behavior by extending certain classes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2006/08/dip-dependency-inversion-principle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LSP: Liskov Substitution Principle</title>
		<link>http://www.softwarerockstar.com/2006/08/lsp-liskov-substitution-principle/</link>
		<comments>http://www.softwarerockstar.com/2006/08/lsp-liskov-substitution-principle/#comments</comments>
		<pubDate>Thu, 24 Aug 2006 00:23:00 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Agile]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Design Principles]]></category>
		<category><![CDATA[liskov substitution principle]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[mortgage account]]></category>
		<category><![CDATA[mortgage business]]></category>
		<category><![CDATA[new mortgage]]></category>
		<category><![CDATA[OOA/OOD]]></category>
		<category><![CDATA[public abstract void]]></category>
		<category><![CDATA[teller]]></category>
		<category><![CDATA[void]]></category>

		<guid isPermaLink="false">http://mharoon.wordpress.com/2006/08/23/lsp-liskov-substitution-principle/</guid>
		<description><![CDATA[The Liskov Substitution Principle dictates when and where it is correct to derive a subtype from an existing supertype. As defined by Barbara Liskov and Jeannette Wing, it essentially states that: Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where [...]]]></description>
			<content:encoded><![CDATA[<p>The Liskov Substitution Principle dictates when and where it is correct to derive a <a href="http://en.wikipedia.org/wiki/Subtype">subtype</a> from an existing supertype.  As defined by <a href="http://www.pmg.csail.mit.edu/~liskov/">Barbara Liskov</a> and Jeannette Wing, it essentially states that:</p>
<blockquote><p>Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is a subtype of T.</p></blockquote>
<p>What it means is that if S is a subtype of T, then a function q(x) must behave in the same manner irrespective of the type of x whether it be S or T.  In other words, if a piece of code behaves differently for a subtype than a supertype, then that code violates the LSP (and consequently the OCP).</p>
<p>Let&#8217;s say that we work for a bank and that we have a simple teller application.  Currently the teller application works with checking and savings account types.</p>
<pre class="brush: csharp">
public abstract class Account
{
    public abstract double CurrentBalance { get; }
    public abstract void Deposit(double amount);
}

public class CheckingAccount : Account
{
    private double _currentBalance;

    public override double CurrentBalance
    {
        get { return _currentBalance; }
    }

    public override void Deposit(double amount)
    {
        _currentBalance += amount;
    }
}

public class SavingsAccount : Account
{
    /* Savings account implementation */
}
</pre>
<p>Our bank just entered the mortgage business so we need to modify our teller application to support this new account type.  During the short analysis phase we decide that since mortgage account is type of an account (IS-A relationship), we can simply create a new mortgage class deriving from the Account abstract class, override a method and a property, and we should be in business.  We add a new class as follows:</p>
<pre class="brush: csharp">
public class MortgageAccount : Account
{
    private double _currentBalance;

    public override double CurrentBalance
    {
        get { return _currentBalance; }
    }

    public override void Deposit(double amount)
    {
        _currentBalance -= amount;
    }
}
</pre>
<p>Everything sounds logical, that is until we come across this code in the teller application:</p>
<pre class="brush: csharp">
public class Bank
{
    public void ReceiveMoney(Account account, double amount)
    {
        double oldBalance = account.CurrentBalance;
        account.Deposit(amount);
        Debug.Assert(account.CurrentBalance = oldBalance + amount);
    }
}
</pre>
<p>Based on our pre-existing code, the ReceiveMoney() method is making a reasonable assumption that the new balance should be equal to the old balance plus the newly deposited amount.  This assumption is, however, violated if we pass an instance of the Mortgage class to this method.  This is a clear violation of the LSP.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2006/08/lsp-liskov-substitution-principle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OCP Open-Closed Principle</title>
		<link>http://www.softwarerockstar.com/2006/08/ocp-open-closed-principle/</link>
		<comments>http://www.softwarerockstar.com/2006/08/ocp-open-closed-principle/#comments</comments>
		<pubDate>Tue, 22 Aug 2006 00:04:00 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Agile]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[CheckingAccount]]></category>
		<category><![CDATA[Closed]]></category>
		<category><![CDATA[Design Principles]]></category>
		<category><![CDATA[example sourcecode]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[object oriented software]]></category>
		<category><![CDATA[object oriented software design]]></category>
		<category><![CDATA[OOA/OOD]]></category>
		<category><![CDATA[Principle]]></category>
		<category><![CDATA[public abstract void]]></category>
		<category><![CDATA[ReceiveMoney]]></category>
		<category><![CDATA[software entities]]></category>
		<category><![CDATA[void]]></category>

		<guid isPermaLink="false">http://mharoon.wordpress.com/2006/08/21/ocp-open-closed-principle/</guid>
		<description><![CDATA[Another fundemental principle of object-oriented software design is the Open-Closed Principle. According to Uncle Bob (Robert Martin), this principle states that: Software entities (classes, modules, functions, etc.) should beopen for extension, but closed for modification. Essentially, what this means is that software&#8217;s design should be such that it&#8217;s behavior can be modified by extending the [...]]]></description>
			<content:encoded><![CDATA[<p>Another fundemental principle of object-oriented software design is the Open-Closed Principle. According to <a href="http://www.objectmentor.com/publications/ocp.pdf#search=%22Object%20mentor%20OCP%22">Uncle Bob (Robert Martin)</a>, this principle states that:</p>
<blockquote><p>Software entities (classes, modules, functions, etc.) should be<br />open for extension, but closed for modification.</p></blockquote>
<p>Essentially, what this means is that software&#8217;s design should be such that it&#8217;s behavior can be modified by <em>extending</em> the existing source code rather than <em>modifying</em> it. Consider the following example:</p>
<pre class="brush: csharp">
public class CheckingAccount
{
    private double _currentBalance;

    public void ProcessDeposit(double amount)
    {
        _currentBalance += amount;
    }
}

public class MortgageAccount
{
    private double _currentBalance;

    public void ProcessPayment(double amount)
    {
        _currentBalance -= amount;
    }
}

public class Bank
{
    public void ReceiveMoney(object account, double amount)
    {
        if (account.GetType() == typeof(CheckingAccount))
        {
            CheckingAccount checkingAccount = (CheckingAccount)account;
            checkingAccount.ProcessDeposit(amount);
        }
        else if (account.GetType() == typeof(MortgageAccount))
        {
            MortgageAccount mortgageAccount = (MortgageAccount)account;
            mortgageAccount.ProcessPayment(amount);
        }
    }
}
</pre>
<p>Notice that the ReceiveMoney() method of the Bank class has to determine the type of account passed in. Only based on that information it can perform the appropriate business logic. Later if we add another account type then we may need to modify this code in order for it to work properly. The above code is, hence, in violation of the Open-Closed principle.</p>
<p>So how can we revise our code to conform to the Open-Closed Principle? Abstraction is the answer. Let&#8217;s take a look:</p>
<pre class="brush: php">
public abstract class Account
{
    public abstract void Deposit(double amount);
}

public class CheckingAccount : Account
{
    private double _currentBalance;

    public override void Deposit(double amount)
    {
        _currentBalance += amount;
    }
}

public class MortgageAccount : Account
{
    private double _currentBalance;

    public override void Deposit(double amount)
    {
        _currentBalance -= amount;
    }
}

public class Bank
{
    public void ReceiveMoney(Account account, double amount)
    {
        account.Deposit(amount);
    }
}
</pre>
<p>Notice how we got rid of all conditional logic in the ReceiveMoney() method. Also notice that if we later add a new account type, the ReceiveMoney() method will work without any modifications. The revised version of our code above, hence, conforms to the Open-Closed Principle.</p>
<p>Let&#8217;s now consider what happens to our code if a new business rule is added that requires an e-mail notification to be sent to the account holder whenever a deposit is made. Obviously, we&#8217;ll have to modify the ReceiveMoney() method to perform that logic. Since we can&#8217;t account for all possible scenarios, we can never acheive <em>perfect closure</em>. With that in mind, design the simplest software that will do the job, and conform to OCP or other such design principles where you see the need.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2006/08/ocp-open-closed-principle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SRP: Single Responsibility Principle</title>
		<link>http://www.softwarerockstar.com/2006/08/srp-single-responsibility-principle/</link>
		<comments>http://www.softwarerockstar.com/2006/08/srp-single-responsibility-principle/#comments</comments>
		<pubDate>Sun, 20 Aug 2006 13:42:00 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Agile]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Design Principles]]></category>
		<category><![CDATA[OOA/OOD]]></category>

		<guid isPermaLink="false">http://mharoon.wordpress.com/2006/08/20/srp-single-responsibility-principle/</guid>
		<description><![CDATA[SRP is one of the fundamental object oriented software design principles, first introduced by Tom DeMarco in 1979. In simple terms, SRP states that every class should have a single well defined goal, and that all members of that class (properties, methods, etc.) should work together to achieve that goal. Robert Martin puts it in [...]]]></description>
			<content:encoded><![CDATA[<p>SRP is one of the fundamental object oriented software design principles, first introduced by <a href="http://www.systemsguild.com/GuildSite/TDM/Tom_DeMarco.html">Tom DeMarco</a> in 1979.</p>
<p>In simple terms, SRP states that every class should have a single well defined goal, and that all members of that class (properties, methods, etc.) should work together to achieve that goal. <a href="http://www.objectmentor.com/resources/articles/srp">Robert Martin</a> puts it in slightly different terms:</p>
<blockquote><p>There should never be more than one reason for a class to change.</p></blockquote>
<p>Essentially SRP enforces the concept of high <a href="http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29">cohesion</a> that should not only be applied to classes but also internals of a class, such as methods.</p>
<p>So why is SRP such an important design principle? It is because a class with multiple responsibilities is more difficult to change and more difficult to reuse (design smells: <a href="http://blogs.netindonesia.net/zeddy/archive/2006/07/31/10428.aspx">fragility and immobility</a>).</p>
<p>While SRP is quite easy to understand, it&#8217;s not <em>always</em> logical to implement. Consider the following class:</p>
<p><img style="display:block;cursor:hand;text-align:center;margin:0 auto 10px;" alt="" src="http://photos1.blogger.com/blogger2/5599/106541226764272/400/SRP-Image1.jpg" border="0" /></p>
<p>Given the convenience of having a customer class that is smart enough to know how to load and save itself, would you rather break it out like this?:</p>
<p><img style="display:block;width:400px;cursor:hand;text-align:center;margin:0 auto 10px;" alt="" src="http://photos1.blogger.com/blogger2/5599/106541226764272/400/SRP-Image2.jpg" border="0" />
<p>According to SRP, you should, because the combined customer class shares multiple responsibilities: customer business logic, and customer persistence logic. In fact if you use <a href="http://en.wikipedia.org/wiki/Test_driven_development">TDD</a>, you may even be <em>forced</em> to separate these two responsibilities.</p>
<p>In practicality, however, while we should keep this principle in mind and try to apply it wherever it makes sense, we should never get so carried away that we start introducing other design smells in our architecture, such as <a href="http://blogs.netindonesia.net/zeddy/archive/2006/07/31/10428.aspx">Rigidity, Viscosity, and Needless Complexity</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2006/08/srp-single-responsibility-principle/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

