<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Brian David Berman: Technical Blog</title>
	<atom:link href="http://briandberman.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://briandberman.com</link>
	<description></description>
	<lastBuildDate>Thu, 07 May 2009 20:52:57 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='briandberman.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/61d141130f1aa57286013f349f20bbd8?s=96&#038;d=http://s2.wp.com/i/buttonw-com.png</url>
		<title>Brian David Berman: Technical Blog</title>
		<link>http://briandberman.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://briandberman.com/osd.xml" title="Brian David Berman: Technical Blog" />
	<atom:link rel='hub' href='http://briandberman.com/?pushpress=hub'/>
		<item>
		<title>Super Simple Repository Pattern Example</title>
		<link>http://briandberman.com/2009/05/07/super-simple-repository-pattern-example/</link>
		<comments>http://briandberman.com/2009/05/07/super-simple-repository-pattern-example/#comments</comments>
		<pubDate>Thu, 07 May 2009 20:52:57 +0000</pubDate>
		<dc:creator>briandberman</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>

		<guid isPermaLink="false">http://briandberman.wordpress.com/2009/05/07/super-simple-repository-pattern-example/</guid>
		<description><![CDATA[After reading Chapter 1 of Professional ASP.NET MVC 1.0, I got a nice introduction to the “repository” pattern.&#160; Using a repository helps us separate concerns, which is a big part of (but not limited to) ASP.NET MVC.&#160; Let’s start off really simple by looking at a table (called Employees) I created and dragged into the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=briandberman.com&blog=7478844&post=21&subd=briandberman&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>After reading <a href="http://www.tinyurl.com/aspnetmvc" target="_blank">Chapter 1</a> of <em><a href="http://www.amazon.com/Professional-ASP-NET-MVC-1-0-Conery/dp/0470384611" target="_blank">Professional ASP.NET MVC 1.0</a></em>, I got a nice introduction to the “repository” pattern.&#160; Using a repository helps us separate concerns, which is a big part of (but not limited to) ASP.NET MVC.&#160; Let’s start off really simple by looking at a table (called <em>Employees</em>) I created and dragged into the LINQ to SQL Designer in Visual Studio 2008:</p>
<p><a href="http://briandberman.files.wordpress.com/2009/05/image.png"><img title="image" style="display:inline;border-width:0;" height="217" alt="image" src="http://briandberman.files.wordpress.com/2009/05/image_thumb.png?w=244&#038;h=217" width="244" border="0" /></a></p>
<p>Once we save our .dbml file, Visual Studio 2008 creates an “Employee” class that exposes the available properties, allowing us to persist data back to our database.&#160; A simple way to add a new record to this table is as follows:</p>
<div id="codeSnippetWrapper" style="border-right:silver 1px solid;border-top:silver 1px solid;font-size:8pt;overflow:auto;border-left:silver 1px solid;width:97.5%;cursor:text;direction:ltr;max-height:200px;line-height:12pt;border-bottom:silver 1px solid;font-family:&#39;height:220px;background-color:#f4f4f4;text-align:left;margin:20px 0 10px;padding:4px;">
<div id="codeSnippet" style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> CompanyDataContext companyDataContext = <span style="color:#0000ff;">new</span> CompanyDataContext();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>&#160; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> Employee employee = <span style="color:#0000ff;">new</span> Employee();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>&#160; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> employee.LastName = <span style="color:#006080;">&quot;Berman&quot;</span>;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span> employee.FirstName = <span style="color:#006080;">&quot;Brian&quot;</span>;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> employee.JobTitle = <span style="color:#006080;">&quot;Software Engineer&quot;</span>;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span> employee.Extension = <span style="color:#006080;">&quot;1234&quot;</span>;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span> employee.HireDate = DateTime.Parse(<span style="color:#006080;">&quot;01/01/2006&quot;</span>);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>&#160; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span> companyDataContext.Employees.InsertOnSubmit(employee);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span> companyDataContext.SubmitChanges();</pre>
</p></div>
</div>
<p>The above produces the following result in the database:</p>
<p><a href="http://briandberman.files.wordpress.com/2009/05/image1.png"><img title="image" style="display:inline;border-width:0;" height="69" alt="image" src="http://briandberman.files.wordpress.com/2009/05/image_thumb1.png?w=710&#038;h=69" width="710" border="0" /></a> </p>
<p>While this may be a fine way to update a table, it is difficult to test and requires mention of the data context, as well as data storage implementation (LINQ to SQL) methods.&#160; To fix this, we can add a “repository” layer:</p>
<div id="codeSnippetWrapper" style="border-right:silver 1px solid;border-top:silver 1px solid;font-size:8pt;overflow:auto;border-left:silver 1px solid;width:97.5%;cursor:text;direction:ltr;max-height:200px;line-height:12pt;border-bottom:silver 1px solid;font-family:&#39;height:243px;background-color:#f4f4f4;text-align:left;margin:20px 0 10px;padding:4px;">
<div id="codeSnippet" style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> EmployeeRepository</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     CompanyDataContext companyDataContext = <span style="color:#0000ff;">new</span> CompanyDataContext();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>&#160; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> Add(Employee employee)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>         companyDataContext.Employees.InsertOnSubmit(employee);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>     }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>&#160; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> Save()</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>         companyDataContext.SubmitChanges();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>     }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span> }</pre>
</p></div>
</div>
<p>The repository class contains the data storage implementation activity, including the data context and methods.&#160; We can then change our original code to the following:</p>
<div id="codeSnippetWrapper" style="border-right:silver 1px solid;border-top:silver 1px solid;font-size:8pt;overflow:auto;border-left:silver 1px solid;width:97.5%;cursor:text;direction:ltr;max-height:200px;line-height:12pt;border-bottom:silver 1px solid;font-family:&#39;background-color:#f4f4f4;text-align:left;margin:20px 0 10px;padding:4px;">
<div id="codeSnippet" style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> EmployeeRepository employeeRepository = <span style="color:#0000ff;">new</span> EmployeeRepository();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>&#160; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> Employee employee = <span style="color:#0000ff;">new</span> Employee();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>&#160; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> employee.LastName = <span style="color:#006080;">&quot;Yandle&quot;</span>;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span> employee.FirstName = <span style="color:#006080;">&quot;Justine&quot;</span>;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> employee.JobTitle = <span style="color:#006080;">&quot;Teacher&quot;</span>;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span> employee.Extension = <span style="color:#006080;">&quot;5678&quot;</span>;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span> employee.HireDate = DateTime.Parse(<span style="color:#006080;">&quot;01/01/2008&quot;</span>);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>&#160; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span> employeeRepository.Add(employee);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span> employeeRepository.Save();  </pre>
</p></div>
</div>
<p>While this example doesn’t use less code, you will notice the “insert” and “save” functionality is handled through the database repository methods Add() and Save().&#160; The result of running this code (along with our original code) produces the following in the database:</p>
<p><a href="http://briandberman.files.wordpress.com/2009/05/image2.png"><img title="image" style="display:inline;border-width:0;" height="92" alt="image" src="http://briandberman.files.wordpress.com/2009/05/image_thumb2.png?w=707&#038;h=92" width="707" border="0" /></a> </p>
<p>This allows our code to be more testable since we could now write unit tests against our objects without a database (mock objects).&#160; Although less likely, we are also able to swap out our data storage implementation (e.g. going from LINQ to SQL to LINQ to Entities) at a later time in a much smoother fashion.&#160; Some other example methods within the EmployeeRepository class are as follows:</p>
<div id="codeSnippetWrapper" style="border-right:silver 1px solid;border-top:silver 1px solid;font-size:8pt;overflow:auto;border-left:silver 1px solid;width:97.5%;cursor:text;direction:ltr;max-height:200px;line-height:12pt;border-bottom:silver 1px solid;font-family:&#39;background-color:#f4f4f4;text-align:left;margin:20px 0 10px;padding:4px;">
<div id="codeSnippet" style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> EmployeeRepository</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>         CompanyDataContext companyDataContext = <span style="color:#0000ff;">new</span> CompanyDataContext();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>&#160; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>         <span style="color:#0000ff;">public</span> IQueryable&lt;Employee&gt; FindAllEmployees()</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>         {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>             <span style="color:#0000ff;">return</span> companyDataContext.Employees;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>         }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>&#160; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>         <span style="color:#0000ff;">public</span> Dinner GetEmployee(<span style="color:#0000ff;">int</span> id)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>         {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>             <span style="color:#0000ff;">return</span> companyDataContext.Employees.SingleOrDefault(e =&gt; e.EmployeeId == id);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>         }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>&#160; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>         <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> Add(Employee employee)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span>         {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span>             companyDataContext.Employees.InsertOnSubmit(employee);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span>         }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span>&#160; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  20:</span>         <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> Save()</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  21:</span>         {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  22:</span>             companyDataContext.SubmitChanges();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  23:</span>         }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  24:</span>     }</pre>
</p></div>
</div>
<p>Hopefully this explains the repository pattern in the simplest way possible.&#160; Please feel free to leave comments.&#160; I am still learning and wouldn’t mind learning more through comments!&#160; <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<br />Posted in ASP.NET, ASP.NET MVC  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/briandberman.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/briandberman.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/briandberman.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/briandberman.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/briandberman.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/briandberman.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/briandberman.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/briandberman.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/briandberman.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/briandberman.wordpress.com/21/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=briandberman.com&blog=7478844&post=21&subd=briandberman&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://briandberman.com/2009/05/07/super-simple-repository-pattern-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/989981e8c41a8df14685a57217a98a46?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Brian Berman</media:title>
		</media:content>

		<media:content url="http://briandberman.files.wordpress.com/2009/05/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://briandberman.files.wordpress.com/2009/05/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://briandberman.files.wordpress.com/2009/05/image_thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Fun with ASP.NET MVC leads to nice oO Design discovery (Updated)</title>
		<link>http://briandberman.com/2009/04/24/fun-with-aspnet-mvc-leads-to-nice-oo-design-discovery/</link>
		<comments>http://briandberman.com/2009/04/24/fun-with-aspnet-mvc-leads-to-nice-oo-design-discovery/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 03:47:42 +0000</pubDate>
		<dc:creator>briandberman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://briandberman.wordpress.com/2009/04/24/fun-with-aspnet-mvc-leads-to-nice-oo-design-discovery/</guid>
		<description><![CDATA[I was working through the first chapter of Professional ASP.NET MVC 1.0 and came across a really slick way to handle business rule violations.&#160; Below is some sample code written by Scott Guthrie for the NerdDinner project.


   1: //
   2: // POST: /Dinners/Edit/2
   3: [AcceptVerbs(HttpVerbs.Post)]
   4: public [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=briandberman.com&blog=7478844&post=7&subd=briandberman&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>I was working through the first chapter of <a href="http://www.amazon.com/gp/product/0470384611?ie=UTF8&amp;tag=diabeticbooks&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0470384611" target="_blank">Professional ASP.NET MVC 1.0</a> and came across a really slick way to handle business rule violations.&#160; Below is some sample code written by <a href="http://weblogs.asp.net/scottgu/" target="_blank">Scott Guthrie</a> for the <a href="http://www.nerddinner.com" target="_blank">NerdDinner</a> project.</p>
<div id="codeSnippetWrapper" style="border-right:silver 1px solid;border-top:silver 1px solid;font-size:8pt;overflow:auto;border-left:silver 1px solid;width:97.5%;cursor:text;direction:ltr;line-height:12pt;border-bottom:silver 1px solid;font-family:&#39;background-color:#f4f4f4;text-align:left;margin:20px 0 10px;padding:4px;">
<div id="codeSnippet" style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#008000;">//</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#008000;">// POST: /Dinners/Edit/2</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> [AcceptVerbs(HttpVerbs.Post)]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> <span style="color:#0000ff;">public</span> ActionResult Edit(<span style="color:#0000ff;">int</span> id, FormCollection formValues)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     Dinner dinner = dinnerRepository.GetDinner(id);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>&#160; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>     <span style="color:#0000ff;">try</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>         UpdateModel(dinner);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>&#160; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>         dinnerRepository.Save();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>&#160; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>         <span style="color:#0000ff;">return</span> RedirectToAction(<span style="color:#006080;">&quot;Details&quot;</span>, <span style="color:#0000ff;">new</span> { id = dinner.DinnerId });</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>&#160; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span>     }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span>&#160; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span>     <span style="color:#0000ff;">catch</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  20:</span>         ModelState.AddRuleViolations(dinner.GetRuleViolations());</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  21:</span>&#160; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  22:</span>         <span style="color:#0000ff;">return</span> View(dinner);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  23:</span>     }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;font-family:&#39;background-color:#f4f4f4;text-align:left;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  24:</span> }</pre>
</p></div>
</div>
<p>What’s going on here is that we are editing a Dinner object.&#160; We pass in the ID of the Dinner we are editing as well as the form values from the form post.&#160; At line 6, we instantiate a new dinner object based on the ID passed in.&#160; We then “try” (lines 8-13) to update the model and save it back to the database.&#160; If all goes well and the posted data is valid, the Dinner is saved and we are redirected (line 12).&#160; If, on the other hand, something goes wrong (incorrect or incomplete data is passed in), it safe to say that a business rule has been violated.&#160; Enter our “catch” (lines 14-19).&#160; Since something went wrong, we update our model state with the violations from the GetRuleViolations method on the Dinner object.&#160; We then return the dinner, showing the user the violations to fix.&#160; I think this is, not only a great ASP.NET MVC example, but also a good OO design example. </p>
<p>(Update – Styles for code have been fixed. <strike>I am aware that I have some formatting issues.&#160; I just opened this blog and haven’t looked at the CSS yet.&#160; Thanks.</strike>)</p>
<br />Posted in Uncategorized  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/briandberman.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/briandberman.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/briandberman.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/briandberman.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/briandberman.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/briandberman.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/briandberman.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/briandberman.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/briandberman.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/briandberman.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=briandberman.com&blog=7478844&post=7&subd=briandberman&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://briandberman.com/2009/04/24/fun-with-aspnet-mvc-leads-to-nice-oo-design-discovery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/989981e8c41a8df14685a57217a98a46?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Brian Berman</media:title>
		</media:content>
	</item>
		<item>
		<title>My First Tech Blog Post</title>
		<link>http://briandberman.com/2009/04/23/my-first-tech-blog-post/</link>
		<comments>http://briandberman.com/2009/04/23/my-first-tech-blog-post/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 20:12:18 +0000</pubDate>
		<dc:creator>briandberman</dc:creator>
				<category><![CDATA[Thoughts]]></category>

		<guid isPermaLink="false">http://briandberman.wordpress.com/2009/04/23/my-first-tech-blog-post/</guid>
		<description><![CDATA[After watching Scott Hanselman’s video on Social Networking for Developers, I’ve decided to take the plunge into technical blogging.&#160; I’ll be posting thoughts, code samples, and anything I think is interesting and cool, when it comes to software development.&#160; Thanks.  
Posted in Thoughts       <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=briandberman.com&blog=7478844&post=4&subd=briandberman&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>After watching Scott Hanselman’s <a href="http://www.hanselman.com/blog/SocialNetworkingForDevelopersConferenceTalkVideo.aspx">video</a> on Social Networking for Developers, I’ve decided to take the plunge into technical blogging.&#160; I’ll be posting thoughts, code samples, and anything I think is interesting and cool, when it comes to software development.&#160; Thanks.  </p>
<br />Posted in Thoughts  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/briandberman.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/briandberman.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/briandberman.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/briandberman.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/briandberman.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/briandberman.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/briandberman.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/briandberman.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/briandberman.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/briandberman.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=briandberman.com&blog=7478844&post=4&subd=briandberman&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://briandberman.com/2009/04/23/my-first-tech-blog-post/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/989981e8c41a8df14685a57217a98a46?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Brian Berman</media:title>
		</media:content>
	</item>
	</channel>
</rss>