<?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>James Gregory &#187; Alt.Net</title>
	<atom:link href="http://jagregory.com/writings/category/altnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://jagregory.com</link>
	<description>Monkeying with the code</description>
	<lastBuildDate>Tue, 22 Jun 2010 11:07:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>If it hasn&#8217;t already been done, you don&#8217;t need it</title>
		<link>http://jagregory.com/writings/if-it-hasnt-already-been-done-you-dont-need-it/</link>
		<comments>http://jagregory.com/writings/if-it-hasnt-already-been-done-you-dont-need-it/#comments</comments>
		<pubDate>Tue, 06 May 2008 19:39:52 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Alt.Net]]></category>
		<category><![CDATA[Opinion]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/2008/05/06/if-it-hasnt-already-been-done-you-dont-need-it/</guid>
		<description><![CDATA[If you have a really good idea, and nobody has done it before, chances are there&#8217;s a reason for that. It could be that it was technically impossible last time looked at it, but more than likely it&#8217;s because you&#8217;re trying to solve something that is only present because you&#8217;re doing something wrong.
Consider alternatives always [...]]]></description>
			<content:encoded><![CDATA[<p>If you have a really good idea, and nobody has done it before, chances are there&#8217;s a reason for that. It could be that it was technically impossible last time looked at it, but more than likely it&#8217;s because you&#8217;re trying to solve something that is only present because you&#8217;re doing something wrong.</p>
<p>Consider alternatives always before embarking on writing something yourself. In an ever maturing software ecosystem, and the constant catch-up .Net plays with Java, if a solution to your problem was really needed, somebody should have attempted it before.</p>
<p>That&#8217;s not to say don&#8217;t innovate, but don&#8217;t recreate either, and certainly don&#8217;t provide solutions to non-existent problems.</p>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/if-it-hasnt-already-been-done-you-dont-need-it/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>All about dependencies</title>
		<link>http://jagregory.com/writings/all-about-dependencies/</link>
		<comments>http://jagregory.com/writings/all-about-dependencies/#comments</comments>
		<pubDate>Sat, 05 Apr 2008 16:24:05 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Alt.Net]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/2008/04/05/all-about-dependencies/</guid>
		<description><![CDATA[This article serves as an introduction to the concept of Dependency Injection, and why you&#8217;d want to use it. It is not a getting started guide for using containers. If you&#8217;re interested in those, my personal preference is Castle Windsor and you can find their getting started guide here.
What are dependencies? Also referred to as [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>This article serves as an introduction to the concept of <a href="http://martinfowler.com/articles/injection.html">Dependency Injection</a>, and why you&#8217;d want to use it. It is not a getting started guide for using containers. If you&#8217;re interested in those, my personal preference is <a href="http://www.castleproject.org/container/">Castle Windsor</a> and you can find their <a href="http://www.castleproject.org/container/gettingstarted/index.html">getting started guide here</a>.</p></blockquote>
<p>What are dependencies? Also referred to as couplings, dependencies are other modules that one module requires to fulfill it&#8217;s purpose.</p>
<p>Are dependencies bad? No, of course they&#8217;re not, otherwise we wouldn&#8217;t be able to create anything. However, highly coupled code can cause a lot of problems for your application.</p>
<p>If your code requires knowledge of how a dependency works, then your code is highly coupled. If your code is tied explicitly to an implementation, then your code is also highly coupled.</p>
<p>Take the following example:</p>
<pre name="code" class="c-sharp">
public class ProductUpdater
{
	public void StoreChanges(Product product)
	{
	  SqlDataStore dataStore = new SqlDataStore();

	  dataStore.CreateConnection();
	  dataStore.OpenConnection();

	  dataStore.Update(product);

	  dataStore.CloseConnection();
	}
}
</pre>
<p>The above example is highly coupled to the <code>SqlDataStore</code>. Firstly, it directly creates an instance, which means there&#8217;s no way for us to replace that instance if we need to (I&#8217;ll come to why you&#8217;d want to do that in a bit). Secondly, it relies on a great deal of knowledge of <code>SqlDataStore</code>&#8217;s implementation. In this code we can see that you need to create a connection and open it before you can update the record; that&#8217;s quite a bit of implementation knowledge. If the <code>SqlDataStore</code> was to change so that the <code>OpenConnection</code> method created a connection if one didn&#8217;t already exist, then we&#8217;d need to change every caller of  that code to remove the <code>CreateConnection</code> call; in large system situations like that can quickly lead to a fear of change and refactoring.</p>
<p>I mentioned directly creating an instance stops us from replacing it if need be.  Well when would you actually want to do this? For those unfamiliar with unit testing, you probably haven&#8217;t encountered <a href="http://www.martinfowler.com/bliki/TestDouble.html">test doubles</a>; there are different types of test doubles, but for the purposes of this example they&#8217;re interchangeable.</p>
<p>A test double serves as a swap-in replacement for one of your dependencies. These allow you to execute a piece of code under test, without having to worry about whether things are being put in your database, or e-mails sent for example. If your code is creating instances within methods, those instances cannot be replaced by a test double. Without that ability, testing becomes considerably more difficult.</p>
<p>Tightly tying your code to an instance of a class reduces the flexibility and reuse of your code. Take the above example, that same code could be used to update the product in a cache instead of the database; similarly, you could use an in-memory storage instead of a database for when you&#8217;re running in a test or demo environment. If only your method wasn&#8217;t so tightly tied to the implementation.</p>
<p>This is solved by using <a href="http://martinfowler.com/articles/injection.html">Dependency Injection</a>, which is a part of the <a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle"Dependency Inversion Principal</a> and <a href="http://en.wikipedia.org/wiki/Inversion_of_Control">Inversion of Control</a>.</p>
<blockquote><p>[DIP] seeks to &#8220;invert&#8221; the conventional notion that high level modules in software should depend upon the lower level modules. The principle states that high level or low level modules should not depend upon each other, instead they should depend upon abstractions. &#8212; Wikipedia</p></blockquote>
<p>Essentially, dependency injection allows you to stop instantiating your dependencies. Instead they&#8217;re &#8220;injected&#8221; into your object when it is instantiated itself. This allows the dependencies to be swapped out like we mentioned above.</p>
<p>So taking the original example, here&#8217;s a version of it that&#8217;s been updated to use dependency injection.</p>
<pre name="code" class="c-sharp">
public class ProductUpdater
{
	private SqlDataStore dataStore;

	public ProductUpdater(SqlDataStore dataStore)
	{
		this.dataStore = dataStore;
	}

	public void StoreChanges(Product product)
	{
	  dataStore.CreateConnection();
	  dataStore.OpenConnection();

	  dataStore.Update(product);

	  dataStore.CloseConnection();
	}
}
</pre>
<p>The above implementation now allows us to create a test double, and replace the <code>SqlDataStore</code> in our tests. As I mentioned before, we could now easily push in an in-memory implementation without any changes to the code required.</p>
<p>We can take this further though, because we&#8217;re still tied to a concrete class. Lets make <code>SqlDataStore</code> implement an interface, so we can create other implementations.</p>
<pre name="code" class="c-sharp">
public interface IDataStore
{
	void CreateConnection();
	void OpenConnection();
	void Update(Product product);
	void CloseConnection();
}

public class ProductUpdater
{
	private IDataStore dataStore;

	public ProductUpdater(IDataStore dataStore)
	{
		this.dataStore = dataStore;
	}

	public void StoreChanges(Product product)
	{
	  dataStore.CreateConnection();
	  dataStore.OpenConnection();

	  dataStore.Update(product);

	  dataStore.CloseConnection();
	}
}
</pre>
<p>Now our example is no longer specifically tied to a <code>SqlDataStore</code>, so we could quite easily pass it a <code>FileSystemDataStore</code>, or an <code>InMemoryDataStore</code>, or anything else that implements <code>IDataStore</code>. All that without having to touch a single line within the <code>ProductUpdater</code>.</p>
<p>That&#8217;s the power of dependency injection, and why you should stop hard-coding your dependencies.</p>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/all-about-dependencies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Percieved value and developer education</title>
		<link>http://jagregory.com/writings/percieved-value-and-developer-education/</link>
		<comments>http://jagregory.com/writings/percieved-value-and-developer-education/#comments</comments>
		<pubDate>Wed, 12 Dec 2007 22:24:44 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Alt.Net]]></category>
		<category><![CDATA[Opinion]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/2007/12/12/percieved-value-and-developer-education/</guid>
		<description><![CDATA[A comment on a post at Jeremy D. Miller&#8217;s blog caught my eye. To paraphrase, Jeff Tucker says that part of the problem of the lack of perceived value of ORM/TDD/IOC etc is down to the developers in question not having experienced the reason these tools exist.
He makes a fair point really, we use tools [...]]]></description>
			<content:encoded><![CDATA[<p>A <a href="http://codebetter.com/blogs/jeremy.miller/archive/2007/12/12/my-pick-for-altnetconf-quote-of-the-day.aspx#171928">comment</a> on a <a href="http://codebetter.com/blogs/jeremy.miller/archive/2007/12/12/my-pick-for-altnetconf-quote-of-the-day.aspx">post at Jeremy D. Miller&#8217;s blog</a> caught my eye. To paraphrase, Jeff Tucker says that part of the problem of the lack of perceived value of ORM/TDD/IOC etc is down to the developers in question not having experienced the reason these tools exist.</p>
<p>He makes a fair point really, we use tools because they solve a problem for us, if we haven&#8217;t actually experienced what they&#8217;re solving then their value is appears to be much less than what it is. Similarly, the value of such tools and methodologies is nil when there is a lack of recognition of a problem even existing.</p>
<p>However, I don&#8217;t think the solution is to have developers learn &#8220;the hard way&#8221;. To discover the problem before using the solution is a problem in its-self. If we were to all learn from our own mistakes, and not from each-others, then we&#8217;re not going to make much progress as we&#8217;re all going to be solving the same problems.</p>
<p>Most people will have struggled with whatever problem has necessitated the creation of these tools/methodologies, so I think it&#8217;s more a case of helping the developers connect the dots; &#8220;You remember such and such a problem? Well that&#8217;s easily solved using methodology/technology x!&#8221;, rather than making them retrace the steps of every developer before them.</p>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/percieved-value-and-developer-education/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
