<?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>Jason Mitchell &#187; Uncategorized</title>
	<atom:link href="http://jason-mitchell.com/index.php/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://jason-mitchell.com</link>
	<description></description>
	<lastBuildDate>Thu, 26 Jan 2012 22:31:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Data Access Using a Generic Repository in C#</title>
		<link>http://jason-mitchell.com/uncategorized/data-access-using-a-generic-repository-in-c/</link>
		<comments>http://jason-mitchell.com/uncategorized/data-access-using-a-generic-repository-in-c/#comments</comments>
		<pubDate>Sun, 01 Jan 2012 21:30:40 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[data access]]></category>
		<category><![CDATA[repository]]></category>

		<guid isPermaLink="false">http://jason-mitchell.com/?p=407</guid>
		<description><![CDATA[The repository pattern is an abstraction layer which provides a well-organised approach to maintaining a separation between an applications data access and business logic layers.  This gives us the important advantages of making code more maintainable and readable and improving the testability of our code.  It also works great with dependency injection! When I started [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">The repository pattern is an abstraction layer which provides a well-organised approach to maintaining a separation between an applications data access and business logic layers.  This gives us the important advantages of making code more maintainable and readable and improving the testability of our code.  It also works great with dependency injection!</p>
<p style="text-align: justify;">When I started looking at the repository pattern I found that a lot of the samples on the internet used explicitly typed repositories such as ICustomerRepository and IOrderRepository.  However for a website I&#8217;m currently working on all of my CRUD operations were pretty much the same and I wanted to reduce the amount of code I needed to write so I implemented a basic generic repository which would work with any of my data model classes.</p>
<p style="text-align: justify;"><span id="more-407"></span>The following code snippet  is the interface for the basic repository I&#8217;ve been using:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> IRepository
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">void</span> Save<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    IQueryable Query<span style="color: #008000;">&#40;</span>Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>T, <span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&gt;&gt;</span> filter <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span><span style="color: #008000;">;</span>
    T SingleOrDefault<span style="color: #008000;">&#40;</span>Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>T, <span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&gt;&gt;</span> predicate<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">void</span> Insert<span style="color: #008000;">&#40;</span>T entity<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">void</span> Update<span style="color: #008000;">&#40;</span>T entity<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">void</span> Delete<span style="color: #008000;">&#40;</span>T entity<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p style="text-align: justify;">This interface only defines the most basic data access operations and should probably be expanded on to provide support for beginning, committing and rolling back transactions.</p>
<p style="text-align: justify;">The following class is an implementation of IRepository which has been implemented to abstract access to an Entity Framework Code First data context:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> EntityFrameworkRepository <span style="color: #008000;">:</span> IRepository
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> SiteDataContext dataContext<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> EntityFrameworkRepository<span style="color: #008000;">&#40;</span>SiteDataContext dataContext<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">dataContext</span> <span style="color: #008000;">=</span> dataContext<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Save<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        dataContext<span style="color: #008000;">.</span><span style="color: #0000FF;">SaveChanges</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> IQueryable Query<span style="color: #008000;">&#40;</span>Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>T, <span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&gt;&gt;</span> filter <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span>
    <span style="color: #008000;">&#123;</span>
        IQueryable query <span style="color: #008000;">=</span> dataContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Set</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>filter <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
            query <span style="color: #008000;">=</span> query<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Where</span><span style="color: #008000;">&#40;</span>filter<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">return</span> query<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> T SingleOrDefault<span style="color: #008000;">&#40;</span>Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>T, <span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&gt;&gt;</span> predicate<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> dataContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Set</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SingleOrDefault</span><span style="color: #008000;">&#40;</span>predicate<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Insert<span style="color: #008000;">&#40;</span>T entity<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span>
    <span style="color: #008000;">&#123;</span>
        dataContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Set</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>entity<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Update<span style="color: #008000;">&#40;</span>T entity<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span>
    <span style="color: #008000;">&#123;</span>
        DbEntityEntry entityEntry <span style="color: #008000;">=</span> dataContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Entry</span><span style="color: #008000;">&#40;</span>entity<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>entityEntry<span style="color: #008000;">.</span><span style="color: #0000FF;">State</span> <span style="color: #008000;">==</span> EntityState<span style="color: #008000;">.</span><span style="color: #0000FF;">Detached</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            dataContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Set</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Attach</span><span style="color: #008000;">&#40;</span>entity<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            entityEntry<span style="color: #008000;">.</span><span style="color: #0000FF;">State</span> <span style="color: #008000;">=</span> EntityState<span style="color: #008000;">.</span><span style="color: #0000FF;">Modified</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Delete<span style="color: #008000;">&#40;</span>T entity<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span>
    <span style="color: #008000;">&#123;</span>
        dataContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Set</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span>entity<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p style="text-align: justify;">There&#8217;s nothing particularly complicated about this class so  I&#8217;m just going to give a quick example on how to use it and leave it at that!  If you have the following class defined in an Entity Framework Code First data context:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Customer
<span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">&#91;</span>Key<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> ID <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>Required<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> FirstName <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>Required<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> LastName <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p style="text-align: justify;">You could query your customer data really easily using a LINQ expression:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var customers <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">from</span> c <span style="color: #0600FF; font-weight: bold;">in</span> repository<span style="color: #008000;">.</span><span style="color: #0000FF;">Query</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
                <span style="color: #0600FF; font-weight: bold;">where</span> c<span style="color: #008000;">.</span><span style="color: #0000FF;">FirstName</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;Jason&quot;</span>
                <span style="color: #0600FF; font-weight: bold;">select</span> c<span style="color: #008000;">;</span></pre></div></div>

<p style="text-align: justify;">or a lambda expression:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var customer <span style="color: #008000;">=</span> repository<span style="color: #008000;">.</span><span style="color: #0000FF;">Query</span><span style="color: #008000;">&#40;</span>c <span style="color: #008000;">=&gt;</span> c<span style="color: #008000;">.</span><span style="color: #0000FF;">FirstName</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;Jason&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p lang="csharp">For more information about the repository pattern see: <a href="http://msdn.microsoft.com/en-us/library/ff649690.aspx">http://msdn.microsoft.com/en-us/library/ff649690.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/uncategorized/data-access-using-a-generic-repository-in-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>WordPress Stats Plug-in Not Counting</title>
		<link>http://jason-mitchell.com/uncategorized/wordpress-stats-plug-in-not-counting/</link>
		<comments>http://jason-mitchell.com/uncategorized/wordpress-stats-plug-in-not-counting/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 13:47:17 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.jason-mitchell.com/index.php/2009/09/27/wordpress-stats-plug-in-not-counting/</guid>
		<description><![CDATA[I have various WordPress plug-ins installed for my blog but my favourite has got to be the WordPress Stats plug-in.  I like to be able to keep an eye on the amount of traffic my blog gets and in particular what the most popular articles are.  At the minute, it seems to be my article [...]]]></description>
			<content:encoded><![CDATA[<p>I have various WordPress plug-ins installed for my blog but my favourite has got to be the WordPress Stats plug-in.  I like to be able to keep an eye on the amount of traffic my blog gets and in particular what the most popular articles are.  At the minute, it seems to be <a href="http://www.jason-mitchell.com/index.php/2009/08/27/xna-first-person-camera/" target="_blank">my article on a first person camera</a> in <a href="http://en.wikipedia.org/wiki/Microsoft_XNA" target="_blank">XNA</a> and <a href="http://www.jason-mitchell.com/index.php/2009/04/07/iphone-application-development/" target="_blank">my article about iPhone development</a>.  However, for the past month or so the plug-in hasn&#8217;t been counting visits at all.  Which was a little annoying, I figured that I was either hugely unpopular or there was something up with the plug-in.  Not wanting to resign to the possible truth that no one cares what I have to say, I did a little bit of searching for people having similar problems.  it wasn&#8217;t difficult to turn up some results, but the answer came from the <a href="http://wordpress.org/extend/plugins/stats/faq/" target="_blank">plug-in&#8217;s FAQ page on WordPress Extend</a> as I had hoped for.  Basically this plug-in depends on the wp_footer PHP function being called in your theme.  Turns out, my current theme didn&#8217;t call this.  All that I needed to do was add &lt;?php wp_footer(); ?&gt; just before the &lt;/body&gt; tag and the plug-in began counting stats straight away.  Easy fix!</p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/uncategorized/wordpress-stats-plug-in-not-counting/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

