<?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>C.M. Jackson .Net &#187; Programming</title>
	<atom:link href="http://www.cmjackson.net/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cmjackson.net</link>
	<description>Web Design, Programming, Tutorials</description>
	<lastBuildDate>Tue, 13 Dec 2011 17:38:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Cricket.Net: Part 3 &#8211; Unit Testing</title>
		<link>http://www.cmjackson.net/2011/12/13/cricket-net-part-3-unit-testing/</link>
		<comments>http://www.cmjackson.net/2011/12/13/cricket-net-part-3-unit-testing/#comments</comments>
		<pubDate>Tue, 13 Dec 2011 17:33:37 +0000</pubDate>
		<dc:creator>Chris Jackson</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://www.cmjackson.net/?p=490</guid>
		<description><![CDATA[Summary Cricket.Net is a new open source project that I&#8217;ve started on Codeplex. It will be a web application that will be used for tracking software bugs. Cricket.Net will be written in C# using HTML5, ASP.NET MVC 3, Entity Framework 4.1 Code First, StructureMap, and the Onion Architecture. This project will be an example application [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Summary</strong><br />
Cricket.Net is a new open source project that I&#8217;ve started on Codeplex. It will be a web application that will be used for tracking software bugs. Cricket.Net will be written in C# using HTML5, ASP.NET MVC 3, Entity Framework 4.1 Code First, StructureMap, and the Onion Architecture. This project will be an example application to demonstrate how to use these technologies together to build a web application from the ground up.</p>
<p>I’ll also be going through using Mercurial both on my system and pushing changes to the Codeplex site.</p>
<p>Cricket.Net Series:</p>
<ul>
<li><a href="http://www.cmjackson.net/2011/10/12/cricket-net-part-1-setting-up-the-initial-project/">Part 1: Setting up the initial project</a></li>
<li><a href="http://www.cmjackson.net/2011/10/13/cricket-net-part-2-design-discussion/">Part 2: Design Discussion</a></li>
<li>Part 3: Unit Testing</li>
</ul>
<p><strong>Unit Testing Thoughts</strong><br />
The idea behind unit testing is to write a very simple piece of code to test your business logic for one condition.  The test either passes or fails.  As you build your application, you may have hundreds or thousands of these tests.  By keeping them very simple, and not running any time intensive operations such as I/O, these tests will be able to execute very quickly and give you an indication that your program is working correctly.</p>
<p>Unit Tests become most useful when your application has been completed for a number of months and then a required change is needed.  As you modify your code, you can re-run your unit tests to make sure your new modifications have not broken your program in some other area.</p>
<p>The difficult part of unit testing is thinking of all the different angles to test your piece of code.</p>
<p>As you write your tests and develop your application, you will undoubtedly break some of the tests that you had previously written.  These will then need to be revised.  This is the process you will go through to fully flesh out your application and develop it from a user (of the code) perspective.</p>
<p>I usually begin by writing all my tests against the Controllers since that is the entry point to the things my application will do.  I also try to write tests against the requirements of the application only.  If you test all of an application&#8217;s requirements, then you are making sure the application is doing what your customer wants it to do.  There is no need to test 100% of the code you write.  As mentioned above, you will break unit tests as you develop your application and continually have to revise them.  Revising tests that cover 100% of your code could be very time-consuming.</p>
<p><strong>Unit Test Controller Shell</strong><br />
Since we&#8217;ll be testing Requirements, the first thing I do is create a new folder in the Unit Test project called Requirements.  There is currently a Controllers folder that we will remove later on.</p>
<p>Next, create a new Class item called ApplicationControllerRequirements.</p>
<p><a href="http://www.cmjackson.net/wp-content/uploads/2011/12/part3_01.png"><img class="alignnone size-medium wp-image-491" title="part3_01" src="http://www.cmjackson.net/wp-content/uploads/2011/12/part3_01-300x207.png" alt="" width="300" height="207" /></a></p>
<p>The new class will need to be modified for unit testing. Below is the basic shell for a unit testing class.</p>
<pre class="brush: vb;">
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Cricket.UnitTests.Requirements
{
  [TestClass]
  public class ApplicationControllerRequirements
  {
    [TestInitialize]
    public void Setup()
    {
      //This method runs before each test method.
    }

    #region Test Group

    [TestMethod]
    public void unit_test_method()
    {
      //Arrange.

      //Act.

      //Assert.
    }

    #endregion
  }
}
</pre>
<p>There are three attributes being used on this class: TestClass, TestInitialize, and TestMethod.</p>
<ul>
<li><strong>TestClass</strong> &#8211; Signals this is a unit testing class.</li>
<li><strong>TestInitialize</strong> &#8211; This is an initialization method that is executed before each test method.</li>
<li><strong>TestMethod</strong> &#8211; This is a unit test function.</li>
</ul>
<p>A unit test only tests one condition and there may be many conditions we need to test for, so I like to group my tests together using regions. These can be collapsed out of the way within Visual Studio to make it easier to work on other unit tests without the unwanted ones getting in the way.</p>
<p>Within a test method, there are three things that need to be done:</p>
<ul>
<li><strong>Arrange</strong> &#8211; This consists of any mocking or setup tasks.</li>
<li><strong>Act</strong> &#8211; This is the execution of the action you are testing.</li>
<li><strong>Assert</strong> &#8211; This is the verification you received the desired result.</li>
</ul>
<p><strong>Writing The First Unit Test<br />
</strong>First we will start with a very simple test against the Index action of the controller. This is the default action that is called if none is specified in the application&#8217;s URL.</p>
<p><a href="http://www.cmjackson.net/wp-content/uploads/2011/12/part3_02.png"><img class="alignnone size-full wp-image-499" title="part3_02" src="http://www.cmjackson.net/wp-content/uploads/2011/12/part3_02.png" alt="" width="429" height="181" /></a></p>
<p>We&#8217;re going to test if the Index controller returns a ViewResult. Notice the compiler now has an error on the &#8220;controller&#8221; object. This is because we haven&#8217;t created it yet. One nice aspect of TDD (Test Driven Development) is you think about how the interface to your code is used, and then develop the code afterwards.  Now we know we need to create a &#8220;controller&#8221; object and it needs to have a method called &#8220;Index&#8221;.</p>
<p>Since the initialization of the controller will need to be done for nearly all tests in this class, we&#8217;ll add that to the Setup() method.</p>
<p><a href="http://www.cmjackson.net/wp-content/uploads/2011/12/part3_03.png"><img class="alignnone size-full wp-image-500" title="part3_03" src="http://www.cmjackson.net/wp-content/uploads/2011/12/part3_03.png" alt="" width="439" height="549" /></a></p>
<p>Now the compiler is showing errors on the &#8220;ApplicationController&#8221; class.  We need to create the new controller in the main project.</p>
<ul>
<li>Right-click on the Controllers folder in the main project.</li>
<li>Select Add, then Controller.</li>
<li>Type in the Controller Name: ApplicationController and click Add. (Leaving the Template set on Empty Controller)</li>
<li>In your unit test class, add the using statement: <em>using Cricket.Controllers;</em></li>
<li>Under the Unit Test method, add a cast to the &#8220;Act&#8221; line: <em>ViewResult result = (ViewResult)controller.Index();</em></li>
<li>Also, don&#8217;t forget to change the name of your unit test method to something meaningful. The more description you can put in this method name, the better.</li>
</ul>
<p><strong>Running Your Test</strong><br />
Ideally, you want to see your test fail first, to make sure you didn&#8217;t accidentally write a test that can never fail.</p>
<p>We can quickly test this by adding the following line to our Index method on the ApplicationController:</p>
<pre class="brush: vb;">
return RedirectToAction(&quot;Index&quot;);
</pre>
<p>A ViewResult is expected in the test, so now it will fail.</p>
<p><a href="http://www.cmjackson.net/wp-content/uploads/2011/12/part3_03-1.png"><img class="alignnone size-full wp-image-510" title="part3_03-1" src="http://www.cmjackson.net/wp-content/uploads/2011/12/part3_03-1.png" alt="" width="538" height="81" /></a></p>
<p>After you have verified the test has failed, change the Index method back to the following:</p>
<pre class="brush: vb;">
        public ActionResult Index()
        {
            return View();
        }
</pre>
<p>Now, your test should pass.<br />
<a href="http://www.cmjackson.net/wp-content/uploads/2011/12/part3_04.png"><img class="alignnone size-full wp-image-503" title="part3_04" src="http://www.cmjackson.net/wp-content/uploads/2011/12/part3_04.png" alt="" width="539" height="93" /></a><br />
<strong></strong></p>
<p><strong>Another Test<br />
</strong>Next, we&#8217;ll add another very simple test to verify the correct view is being returned from the Index method.</p>
<pre class="brush: vb;">
        [TestMethod]
        public void index_action_should_return_the_index_view()
        {
            //Arrange.

            //Act.
            ViewResult result = (ViewResult)controller.Index();

            //Assert.
            Assert.AreEqual(&quot;Index&quot;, result.ViewName, &quot;result.ViewName&quot;);
        }
</pre>
<p>Running this test will fail because the Index method is not returning a specific view. To change this, modify the Index method to show:</p>
<pre class="brush: vb;">
        public ActionResult Index()
        {
            return View(&quot;Index&quot;);
        }
</pre>
<p>Now the test will pass because we are specifying a view to display.<br />
<a href="http://www.cmjackson.net/wp-content/uploads/2011/12/part3_05.png"><img class="alignnone size-full wp-image-504" title="part3_05" src="http://www.cmjackson.net/wp-content/uploads/2011/12/part3_05.png" alt="" width="535" height="99" /></a><br />
<strong></strong></p>
<p><strong>Final Thoughts<br />
</strong>Unit Testing does take some time to get the hang of it. It is well worth it in the long run to know you can modify your program to add new features without breaking the current functionality.</p>
<p>As we develop this application, these tests will become more complex, but by focusing on one result and writing multiple tests for each action, we can work through it.</p>
<p>The empty &#8220;Arrange&#8221; comment for each of these tests is a place holder for mocking objects we may need to place there in the future. Typically, Unit Testing developers say to place all setup code in the arrange.  I like to place common setup tasks that will happen for each test in the &#8220;Setup&#8221; method where I only have to code them once.  If there is a specific setup task needed for a single test, then I will place that under the &#8220;Arrange&#8221; comment.</p>
<p>Visit the project site at: <a href="http://cricket.codeplex.com">http://cricket.codeplex.com</a>.<br />
Download the source code from this post at:<br />
<a href="http://cricket.codeplex.com/SourceControl/changeset/changes/df77b3e9d546">http://cricket.codeplex.com/SourceControl/changeset/changes/df77b3e9d546</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.cmjackson.net/2011/12/13/cricket-net-part-3-unit-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cricket.Net: Part 2 &#8211; Design Discussion</title>
		<link>http://www.cmjackson.net/2011/10/13/cricket-net-part-2-design-discussion/</link>
		<comments>http://www.cmjackson.net/2011/10/13/cricket-net-part-2-design-discussion/#comments</comments>
		<pubDate>Thu, 13 Oct 2011 14:03:47 +0000</pubDate>
		<dc:creator>Chris Jackson</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Website Development]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Onion Architecture]]></category>

		<guid isPermaLink="false">http://www.cmjackson.net/?p=479</guid>
		<description><![CDATA[Summary Cricket.Net is a new open source project that I&#8217;ve started on Codeplex. It will be a web application that will be used for tracking software bugs. Cricket.Net will be written in C# using HTML5, ASP.NET MVC 3, Entity Framework 4.1 Code First, StructureMap, and the Onion Architecture. This project will be an example application [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Summary</strong><br />
Cricket.Net is a new open source project that I&#8217;ve started on Codeplex. It will be a web application that will be used for tracking software bugs. Cricket.Net will be written in C# using HTML5, ASP.NET MVC 3, Entity Framework 4.1 Code First, StructureMap, and the Onion Architecture. This project will be an example application to demonstrate how to use these technologies together to build a web application from the ground up.</p>
<p>I&#8217;ll also be going through using Mercurial both on my system and pushing changes to the Codeplex site.</p>
<p>Cricket.Net Series:</p>
<ul>
<li><a href="http://www.cmjackson.net/2011/10/12/cricket-net-part-1-setting-up-the-initial-project/">Part 1: Setting up the initial project</a></li>
<li>Part 2: Design Discussion<br />
<a href="http://www.cmjackson.net/2011/12/13/cricket-net-part-3-unit-testing/">Part 3: Unit Testing</a></li>
</ul>
<p><strong>What will the application do?</strong><br />
I&#8217;m going to take a step back before continuing with the code and look at what this application is suppose to do. I don&#8217;t need to determine everything up front, instead I&#8217;m going to use an Agile approach and lay down some basic requirements for what the system is suppose to do and if those change as the development progresses, that is fine.</p>
<p>Ususally the first thing I do is think about what information the application needs to store. Cricket.Net is a bug tracking program, so it will need to store information on a specific bug and what application the bug occurred in. I&#8217;m also going to want to track dates for when the bug was created and resolved, the version of the application the bug occurred in and maybe a list of one or more assignees who are working on resolving the bug.</p>
<p>Next, I&#8217;m going to open up Visio and separate these ideas into objects on a database model diagram. Since I&#8217;ve already created my solution folder, I&#8217;m going to add a folder within it called !Design. I&#8217;ll place all of my design files here to keep them with the solution when I update the Mercurial repository.</p>
<p>You don&#8217;t necessarily need to create a diagram of the database, you could just write-up the code using Entity Framework: Code First, but it is nice for non-programmers to have something to look at. Also, complex databases with many relationships can be difficult to work with if you don&#8217;t have a diagram to look at.</p>
<p><a href="http://www.cmjackson.net/wp-content/uploads/2011/10/Database-Diagram.jpg"><img class="alignnone size-medium wp-image-481" title="Database Diagram" src="http://www.cmjackson.net/wp-content/uploads/2011/10/Database-Diagram-300x252.jpg" alt="" width="300" height="252" /></a></p>
<p><strong>Onion Architecture and StructureMap</strong><br />
Now I&#8217;d like to talk for a minute about the Onion Architecture.  Structuring an application in this way separates the business logic from the web and infrastructure components in a way that limits their dependencies on each other. Take a look at the following diagram:</p>
<p><a href="http://www.cmjackson.net/wp-content/uploads/2011/10/Onion-Architecture.jpg"><img class="alignnone size-medium wp-image-482" title="Onion Architecture" src="http://www.cmjackson.net/wp-content/uploads/2011/10/Onion-Architecture-300x125.jpg" alt="" width="300" height="125" /></a></p>
<p>The blue boxes represent a project within the solution. The arrows point to their dependencies. Notice how all projects depend on Core. This is where the business logic classes and interfaces are found. Also, notice how the Web project (in this case will be MVC 3) does not depend on Infrastructure (where the database repositories reside). There is an indirect dependency through the DependencyInjection project. This is where StructureMap (or another IoC framework) lives. StructureMap will wire-up the dependencies between the Web and Infrastructure frameworks using the interfaces found in the Core project.</p>
<p>Why go through all this work to setup your project this way? Encapsulation of components that could be replacable in the future makes it easy to upgrade an application to those new componenets. With Onion Architecture you could switch the entire application over to a different database server and only have to modify the database classes found in the Infrastructure project. As long as the interfaces do not change for those classes, the reset of the application will work as it did previously. This can save much time over rewriting the entire application because the db code is embedded everywhere.</p>
<p>The UI component could be changed as well (in this case the Web project) for a console application. The Core and Infrastructure will function the same, but the application would appear totally different.</p>
<p>Projects Summary:</p>
<ul>
<li><strong>UnitTests</strong> &#8211; As the name implies, it contains all unit testing code. The Moq mocking framework will be used here.</li>
<li><strong>Web</strong> &#8211; This is the user interface project. This contains the controllers, views, view models, and wrapper classes around MVC or web specific operations.</li>
<li><strong>Core</strong> &#8211; This contains all interfaces for the application and business logic including: the domain model and service classes.</li>
<li><strong>DependencyInjection</strong> - This contains the classes needed by StructureMap. StructureMap will register all interfaces and concrete classes used in the application.</li>
<li><strong>Infrastructure</strong> &#8211; This contains any IO wrappers or classes using 3rd party projects (including our own common projects).</li>
</ul>
<p><strong>Unit Testing</strong><br />
When starting a project, I try to get in the habbit of coding a Unit Test first, before anything else. This helps me think about how I want to use a method before I worry about how the method works. This tends to lead to an overall better design of the application. I also try to adhere to the SOLID principles, DRY, and YAGNI by only coding what is needed at the time.</p>
<p>Unit Tests should also be very simple. Test for one condition to be met and pass or fail.</p>
<p><strong>Requirements</strong></p>
<ul>
<li>Allow users to manage Applications (including adding different versions for each).</li>
<li>Allow users to manage their issues.</li>
<li>Allow users to be managed by users.</li>
<li>(Managing each of these includes listing, adding, creating, editing, and deleting).</li>
</ul>
<p>Visit the project site at: <a href="http://cricket.codeplex.com">http://cricket.codeplex.com</a>.<br />
Download the source code from this post at: <a href="http://cricket.codeplex.com/SourceControl/changeset/changes/a115ce901573">http://cricket.codeplex.com/SourceControl/changeset/changes/a115ce901573</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.cmjackson.net/2011/10/13/cricket-net-part-2-design-discussion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cricket.Net: Part 1 &#8211; Setting up the initial project</title>
		<link>http://www.cmjackson.net/2011/10/12/cricket-net-part-1-setting-up-the-initial-project/</link>
		<comments>http://www.cmjackson.net/2011/10/12/cricket-net-part-1-setting-up-the-initial-project/#comments</comments>
		<pubDate>Wed, 12 Oct 2011 19:50:32 +0000</pubDate>
		<dc:creator>Chris Jackson</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Website Development]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>

		<guid isPermaLink="false">http://www.cmjackson.net/?p=467</guid>
		<description><![CDATA[Summary Cricket.Net is a new open source project that I&#8217;ve started on Codeplex. It will be a web application that will be used for tracking software bugs. Cricket.Net will be written in C# using HTML5, ASP.NET MVC 3, Entity Framework 4.1 Code First, StructureMap, and the Onion Architecture. This project will be an example application to demonstrate [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Summary</strong><br />
Cricket.Net is a new open source project that I&#8217;ve started on Codeplex. It will be a web application that will be used for tracking software bugs. Cricket.Net will be written in C# using HTML5, ASP.NET MVC 3, Entity Framework 4.1 Code First, StructureMap, and the Onion Architecture. This project will be an example application to demonstrate how to use these technologies together to build a web application from the ground up.</p>
<p>I&#8217;ll also be going through using Mercurial both on my system and pushing changes to the Codeplex site.</p>
<p>Cricket.Net Series:</p>
<ul>
<li>Part 1: Setting up the initial project</li>
<li><a href="http://www.cmjackson.net/2011/10/13/cricket-net-part-2-design-discussion/">Part 2: Design Discussion</a></li>
<li><a href="http://www.cmjackson.net/2011/12/13/cricket-net-part-3-unit-testing/">Part 3: Unit Testing</a></li>
</ul>
<p><strong>Prerequisites</strong><br />
To get started, I&#8217;ll be using Visual Studio 2010 SP1Rel and <a href="http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;id=1491" target="_blank">ASP.NET MVC 3 Tools Update</a>. Later, I&#8217;ll be using <a href="http://www.microsoft.com/download/en/details.aspx?id=26825" target="_blank">Entity Framework 4.1 Update 1</a> so that I can use Code First. You can either download and install Entity Framework, or we&#8217;ll attempt to install this from the Nuget package first.</p>
<p><strong>Creating A New Project</strong><br />
Initially, I&#8217;m going to start with the basic MVC package created by Visual Studio. After this package is created, I&#8217;m going to create the Mercurial repository so that I can send the code to Codeplex.</p>
<ol>
<li>Open Visual Studio 2010.</li>
<li>Click the File menu, then New, Project.</li>
<li>In the New Project window, under Installed Templates, expand Visual C# and choose Web. Select the template called &#8220;ASP.NET MVC 3 Web Application.&#8221; The Name field will be the default name and namespace of your initial project, the Location will be the folder on your C:\ drive where to store your solution, and the Solution Name is the name of your solution folder (if you wish it to be different).<br />
<a href="http://www.cmjackson.net/wp-content/uploads/2011/10/part1_01.png"><img class="alignnone size-medium wp-image-468" title="New Part window" src="http://www.cmjackson.net/wp-content/uploads/2011/10/part1_01-300x207.png" alt="" width="300" height="207" /></a></li>
<li>After clicking OK, the New ASP.NET MVC 3 Project window pops up. I&#8217;m going to choose Internet Application so that the tooling will allow me to go ahead and create a Unit Test project also. I&#8217;m going to change the name of the Unit Test project to Cricket.UnitTests.<br />
<a href="http://www.cmjackson.net/wp-content/uploads/2011/10/part1_02.png"><img class="alignnone size-medium wp-image-469" title="New ASP.NET MVC 3 Project" src="http://www.cmjackson.net/wp-content/uploads/2011/10/part1_02-300x270.png" alt="" width="300" height="270" /></a></li>
<li>After Visual Studio finishes creating the project, it should look like the image below. The Internet Application template that we had chosen in the previous step has added some files that will may or may not use, but for now we&#8217;ll leave these in the solution and move on to creating the Mercurial repository.<br />
<a href="http://www.cmjackson.net/wp-content/uploads/2011/10/part1_03.png"><img class="alignnone size-medium wp-image-470" title="Initial Solution Explorer" src="http://www.cmjackson.net/wp-content/uploads/2011/10/part1_03-285x300.png" alt="" width="285" height="300" /></a></li>
</ol>
<p><strong>Setting up the Mercurial Repository</strong><br />
If you are unfamiliar with Mercurial, it is a source control application that is very fast and pretty easy to use. Before you can continue with this tutorial, you will need to install at least <a href="http://tortoisehg.bitbucket.org/" target="_blank">TortoiseHg</a> to manage the Mercurial repository from Windows Explorer.  If you&#8217;d also like to work with it from within Visual Studio, you will need <a href="http://visualhg.codeplex.com/" target="_blank">VisualHg</a>.  If you&#8217;d like to know more about what Mercurial can do, check out this <a href="http://tekpub.com/codeplex" target="_blank">video from tekpub.com</a>.</p>
<ol>
<li>If you still have your new solution open in Visual Studio, go to the File menu and choose Close Solution.</li>
<li>Next, browse to the folder where your solution folder is located. (In my case, I need to go to the C:\Users\cjackson\Documents\All Projects\ folder)</li>
<li>Right-click on the Cricket.Net folder and expand the TortoiseHg context menu and choose Create Repository Here.</li>
<li>TortoiseHg will display an Init window. Click the Create button to create the repository.</li>
<li>After the repository has been created, a green check mark icon will appear over the Cricket.Net folder icon.</li>
<li>Open the Cricket.Net folder and then edit the .hgignore file. This file will tell the repository to ignore certain files/folders that are used by Visual Studio and do not need to be version controlled. Paste the text below into the file and save it.</li>
</ol>
<pre class="brush: vb;"># use glob syntax
syntax: glob

*.obj
*.pdb
*.user
*.aps
*.pch
*.vspscc
*.vssscc
*_i.c
*_p.c
*.ncb
*.suo
*.tlb
*.tlh
*.bak
*.cache
*.ilk
*.log
*.lib
*.sbr
*.scc
[Bb]in
[Dd]ebug*/
obj/
[Rr]elease*/
_ReSharper*/
[Tt]humbs.db
[Tt]est[Rr]esult*
[Bb]uild[Ll]og.*
*.[Pp]ublish.xml
*.resharper
</pre>
<p>Next, I&#8217;ll save the initial project into the Mercurial repository.</p>
<ol>
<li><span style="font-family: Consolas;">Return to the folder where your solution is stored. (C:\Users\cjackson\Documents\All Projects\ folder)</span></li>
<li>Right-click on the Cricket.Net folder and choose Hg Commit&#8230;</li>
<li>The Cricket.Net commit window will pop up. On the left pane, click the check box at the top to select all files then in the top-right pane, type in a comment for the commit. After you enter whatever comments you wish, click the Commit button to save the current state of the solution.<br />
<a href="http://www.cmjackson.net/wp-content/uploads/2011/10/part1_05.png"><img class="alignnone size-medium wp-image-475" title="Commit" src="http://www.cmjackson.net/wp-content/uploads/2011/10/part1_05-300x190.png" alt="" width="300" height="190" /></a></li>
<li>Next, for new untracked files, a window may pop up asking to add untracked files. Click Add.</li>
</ol>
<p>To save the current repository to Codeplex, follow these instructions:</p>
<ol>
<li>Return to the folder where your solution is stored. (C:\Users\cjackson\Documents\All Projects\ folder)</li>
<li>Right-click on Cricket.Net and expand the TortoiseHG context menu and choose Synchronize.</li>
<li>Under Remote Repository, change the drop down to HTTPS and enter the name of the repository so the URL looks like this (<a href="http://hg01.codeplex.com/cricket">http://hg01.codeplex.com/cricket</a>)</li>
<li>Click on the <a href="http://www.cmjackson.net/wp-content/uploads/2011/10/part1_06.png"><img class="alignnone size-full wp-image-476" title="Push icon" src="http://www.cmjackson.net/wp-content/uploads/2011/10/part1_06.png" alt="" width="30" height="29" /></a> icon to push changes to Codeplex.</li>
<li>Enter your Codeplex username and password when prompted and the files will be uploaded to Codeplex.</li>
</ol>
<p>Visit the project site at: <a href="http://cricket.codeplex.com">http://cricket.codeplex.com</a>.<br />
Download the source code from this post at: <a href="http://cricket.codeplex.com/SourceControl/changeset/changes/1569d75e695b#">http://cricket.codeplex.com/SourceControl/changeset/changes/1569d75e695b#</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.cmjackson.net/2011/10/12/cricket-net-part-1-setting-up-the-initial-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unknown Exceptions</title>
		<link>http://www.cmjackson.net/2011/03/02/unknown-exceptions/</link>
		<comments>http://www.cmjackson.net/2011/03/02/unknown-exceptions/#comments</comments>
		<pubDate>Wed, 02 Mar 2011 18:03:03 +0000</pubDate>
		<dc:creator>Chris Jackson</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Exceptions]]></category>
		<category><![CDATA[Routing]]></category>

		<guid isPermaLink="false">http://www.cmjackson.net/?p=438</guid>
		<description><![CDATA[When I build an ASP.NET MVC application, I like to add my own error handler and serve up some nicer error messages to the users of the systems.  I allow for unknown exceptions to be emailed to me along with a stack trace so I can attempt to track them down and resolve unhandled issues. [...]]]></description>
			<content:encoded><![CDATA[<p>When I build an ASP.NET MVC application, I like to add my own error handler and serve up some nicer error messages to the users of the systems.  I allow for unknown exceptions to be emailed to me along with a stack trace so I can attempt to track them down and resolve unhandled issues.</p>
<p>Today I came across something new.  There were two different files that the client browsers from certain users were trying to access on the server.</p>
<p>- /_vti_bin/owssvr.dll<br />
- /MSOffice/cltreq.asp</p>
<p>It appears that these are requests from a browser to look for web discussions on the site being accessed.  While this may be fine for a normal user&#8217;s browser, it is causing two exceptions to fire each time my application is being accessed.</p>
<p>To resolve this issue using ASP.NET MVC, I added the following code to the Global.asax.vb file at the beginning of the RegisterRoutes method:</p>
<pre class="brush: vb;">
routes.IgnoreRoute(&quot;_vti_bin/owssvr.dll&quot;)
routes.IgnoreRoute(&quot;MSOffice/cltreq.asp&quot;)
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.cmjackson.net/2011/03/02/unknown-exceptions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using the Visual Studio 2010 Remote Debugger with ASP.NET MVC</title>
		<link>http://www.cmjackson.net/2010/11/04/using-the-visual-studio-2010-remote-debugger-with-asp-net-mvc/</link>
		<comments>http://www.cmjackson.net/2010/11/04/using-the-visual-studio-2010-remote-debugger-with-asp-net-mvc/#comments</comments>
		<pubDate>Thu, 04 Nov 2010 19:20:53 +0000</pubDate>
		<dc:creator>Chris Jackson</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Remote Debugger]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">http://www.cmjackson.net/?p=405</guid>
		<description><![CDATA[Visual Studio Remote Debugging Monitor To use the remote debugger with ASP.NET MVC applications, you first must run the Visual Studio Remote Debugger Monitor on the server or remote computer. There are two ways you can do this: - Copy the monitor&#8217;s files to the server or remote computer - Create a shared folder on [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Visual Studio Remote Debugging Monitor</strong></p>
<p>To use the remote debugger with ASP.NET MVC applications, you first must run the Visual Studio Remote Debugger Monitor on the server or remote computer.</p>
<p>There are two ways you can do this:</p>
<p>- Copy the monitor&#8217;s files to the server or remote computer<br />
- Create a shared folder on your developer client and run the monitor on the server from the share.</p>
<p>I will go through the shared folder method. Share out this folder:<br />
C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\Remote Debugger</p>
<p>If you server is running a 64-bit OS, use the x64 folder.  For the 32-bit OS, use the x86 folder.</p>
<p><strong>Setting up the server</strong></p>
<p>- Log into the server and navigate to the shared folder on your developer PC. <br />
- Go into the correct folder for your server&#8217;s OS (x86 or x64).<br />
- Run the msvsmon.exe file.  If your server&#8217;s OS is Windows 2008, you will need to right-click on the file and run as Administrator.<br />
- Open Tools -&gt; Permissions.  Make sure that your developer client user is listed here.  If not, add the user and click OK.  Click Yes to allow the user to debug.<br />
- Copy the name of the server (listed on the first line of the Visual Studio Remote Debugging Monitor).<br />
- Return to your developer client.</p>
<p><strong>Debugging remotely</strong></p>
<p>- Make sure the latest version of your application is published to the server (so your source code will be the same as the executed code).<br />
- From Visual Studio 2010, click Debug -&gt; Attach to process.<br />
- Change the value of the Qualifier field to the name of the server on the Visual Studio Remote Debugging Monitor and click Refresh.<br />
- Under the Available Processes, select &#8220;w3wp.exe&#8221; and click Attach.  If you do not see this process listed, then open a web browser and run your application.  After your application has loaded, click the Refresh button and you should see the process.</p>
<p>* If you receive any error messages, you may need to:<br />
- Click Build -&gt; Clean Solution<br />
- Click Build -&gt; Rebuild Solution<br />
- Republish your application to the server<br />
- Click Debug -&gt; Attach To Process</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cmjackson.net/2010/11/04/using-the-visual-studio-2010-remote-debugger-with-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Setting up Structure Map</title>
		<link>http://www.cmjackson.net/2010/05/14/setting-up-structure-map/</link>
		<comments>http://www.cmjackson.net/2010/05/14/setting-up-structure-map/#comments</comments>
		<pubDate>Fri, 14 May 2010 14:34:20 +0000</pubDate>
		<dc:creator>Chris Jackson</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[StructureMap]]></category>

		<guid isPermaLink="false">http://www.cmjackson.net/?p=388</guid>
		<description><![CDATA[StructureMap If you haven&#8217;t heard about dependency injection, it is something you should definitely look into. It can save tons of time when coding. StructureMap is a dependency injection framework that makes it easy to create new instances of objects and even cache an instance of an object so various references to the object use the [...]]]></description>
			<content:encoded><![CDATA[<p><strong>StructureMap<br />
</strong>If you haven&#8217;t heard about dependency injection, it is something you should definitely look into. It can save tons of time when coding.</p>
<p>StructureMap is a dependency injection framework that makes it easy to create new instances of objects and even cache an instance of an object so various references to the object use the same instance. This is very useful with objects such as database connections.</p>
<p>You can download the library from <a href="http://structuremap.github.com/structuremap/index.html">the StructureMap Home Page</a>. The example used in this post is using StructureMap 2.6.1.  I&#8217;m also using Visual Basic.  I had a tough time finding information on how to use StructureMap with Visual Basic, so hopefully, this will help someone else.</p>
<p><strong>Setting Up MVC<br />
</strong>When using StructureMap with ASP.NET MVC 1.0, I&#8217;ve found it works best if a folder is created in each project; Main, Data, and Service; where the StructureMap files are stored. This keeps them organized and makes them easy to find as you need to update them. A reference to the StructureMap DLL will need to be added to each project as well.</p>
<p>In the main project&#8217;s StructureMap folder, the following files are needed:</p>
<p><strong>StructureMapControllerFactory</strong></p>
<pre class="brush: vb;">
Imports System.Web.Mvc
Imports StructureMap

Public Class StructureMapControllerFactory
    Inherits DefaultControllerFactory

    Protected Overrides Function GetControllerInstance(ByVal controllerType As System.Type) As System.Web.Mvc.IController

        Return ObjectFactory.GetInstance(controllerType)

    End Function

End Class</pre>
<p>This class sets up a factory class to get an instance of the StructureMap factory. It will be used to replace MVC&#8217;s default controller factory so that StructureMap will be in charge of creating our controllers.</p>
<p><strong>BootStrapper</strong></p>
<pre class="brush: vb;">
Imports StructureMap
Imports YourProject.Data
Imports YourProject.Services

Public Class BootStrapper

    Public Shared Sub ConfigureStructureMap()

        ObjectFactory.Initialize(AddressOf StructureMapRegistry)

    End Sub

    Private Shared Sub StructureMapRegistry(ByVal x As IInitializationExpression)

        x.AddRegistry(New MainRegistry())
        x.AddRegistry(New DataRegistry( _ ConfigurationManager.ConnectionStrings(&quot;iSeries&quot;).ConnectionString, _ ConfigurationManager.ConnectionStrings(&quot;Interbase&quot;).ConnectionString))
        x.AddRegistry(New ServiceRegistry())
        x.Scan(AddressOf StructureMapScanner)

    End Sub

    Private Shared Sub StructureMapScanner(ByVal scanner As StructureMap.Graph.IAssemblyScanner)

        scanner.Assembly(&quot;YourProjectNamespace&quot;)
        scanner.Assembly(&quot;YourProjectNamespace.Data&quot;)
        scanner.Assembly(&quot;YourProjectNamespace.Services&quot;)
        scanner.WithDefaultConventions()

    End Sub
End Class</pre>
<p>This class is where StructureMap is configured. Within the StructureMapRegistry method, each AddRegistry call is made to include each of our project&#8217;s registry files, which have yet to be created. We will get to that in a moment, but notice that the DataRegistry is accepting values for connection strings. This is how information can be passed into a class being created by StructureMap when you would like to keep the configuration settings within the Web.config file.</p>
<p>Also, within the StructureMapScanner method, an entry needs to be made to scan each project&#8217;s namespace.</p>
<p><strong>MainRegistry</strong></p>
<pre class="brush: vb;">
Imports DCS.Data
Imports StructureMap.Configuration.DSL

Public Class MainRegistry
    Inherits Registry

    Public Sub New()

        [For](Of IWebContext)() _
            .Use(Of WebContext)()

    End Sub

End Class</pre>
<p>Each project will have its own registry file where you can specify that Interface-A should create an instance of Class-A. In the example above, I want StructureMap to create an instance of WebContext (from the main project) when it comes across a reference to IWebContext. </p>
<p>In the Data project&#8217;s StructureMap folder you will need:</p>
<p><strong>DataRegistry</strong></p>
<pre class="brush: vb;">
Imports StructureMap.Configuration.DSL
Imports Spartan.DAO.Database

Public Class DataRegistry
    Inherits Registry

    Public Sub New(ByVal iSeriesConnectionString As String, ByVal interbaseConnectionString As String)

    'Data Connections.
    [For](Of YourDataContext)() _
        .HybridHttpOrThreadLocalScoped _
        .Use(Function() New YourDataContext())

    [For](Of DB2Connection)() _
        .HybridHttpOrThreadLocalScoped _
        .Use(Function() New DB2Connection(iSeriesConnectionString))

    [For](Of InterbaseConnection)() _
        .HybridHttpOrThreadLocalScoped _
        .Use(Function() New InterbaseConnection(interbaseConnectionString))

    'Repositories.
    [For](Of IShiftRepository)() _
        .Use(Of ShiftRepository)()

    End Sub

End Class</pre>
<p>The DataRegistry is interesting because we are passing in two connection strings from the Web.config file. The first data connection is for Microsoft SQL, the second for DB2 and the third for Interbase. The second and third are using the DB2Connection  and InterbaseConnection classes which are home grown database connection class that I wrote.  All three of these database connections are being cached by using the .HybridHttpOrThreadLocalScoped property. Using this will make the database cached instance work for both your live program and your unit testing.</p>
<p>The entry under repositories is what all remaining entries would look like within the registry class. Since the data project contains our repositories, they would all be listed here.</p>
<p>And, in the Service project&#8217;s StructureMap folder you will need:</p>
<p><strong>ServiceRegistry</strong></p>
<pre class="brush: vb;">
Imports StructureMap.Configuration.DSL

Public Class ServiceRegistry
    Inherits Registry

    Public Sub New()

        [For](Of IShiftService)() _
        .Use(Of ShiftService)()

    End Sub

End Class</pre>
<p>The service registry is similar to that of the main project. All service classes found in the service project would be listed here.</p>
<p><strong>Changes to the Global.asax</strong></p>
<p>The last thing to do is setup the Global.asax file to load StructureMap.  Make the following changes to the Application_Start method:</p>
<pre class="brush: vb;">
Sub Application_Start()

    RegisterRoutes(RouteTable.Routes)

    'StructureMap
    BootStrapper.ConfigureStructureMap()
    ControllerBuilder.Current.SetControllerFactory(New StructureMapControllerFactory())

End Sub</pre>
<p>Now, the default controller factory used by MVC will be overridden by our StructureMapControllerFactory. When a controller is created, it will now use StructureMap.</p>
<p>So, how do you use this when creating a class? This is the easy part.</p>
<p>StructureMap will use the greediest constructor from your controller, meaning, if you have two constructors, the one with the most parameters will be used. Each parameter references an Interface and sets a class variable for the object.  StructureMap uses the registry classes that we setup to map an interface to a real object.</p>
<p><strong>Example Controller</strong></p>
<pre class="brush: vb;">
Imports YourProject.Data
Imports YourProject.Services
Imports StructureMap

Public Class ExampleController
    Inherits System.Web.Mvc.Controller

    Private _shiftService As IShiftService
    Private _webContext As IWebContext

    Public Sub New( _
        ByVal shiftService As IShiftService, _
        ByVal webContext As IWebContext)

        _shiftService = shiftService
        _webContext = webContext

        'Initialize service classes.
        _shiftSummaryService.Initialize(New ModelStateWrapper(Me.ModelState))

    End Sub

    ...</pre>
<p>All that you need to do is create a constructor, pass in all the interface references you need for the controller, then store them in a class variable. If you need to use the ModelState for validation within your service class, the best way I&#8217;ve found to do this is use an Initialize function in your service class to pass in the ModelState. This is because the ModelState is not yet created by MVC when the service classes are created by StructureMap.</p>
<p>When StructureMap creates the instance of the ShiftService, it will look at the greediest constructor from that class to create instances for any required parameters. This continues for all classes that are referenced.</p>
<p>Once the solution has been configured to use StructureMap, the only thing you&#8217;ll need to maintain will be the registry files as you add new classes and interfaces to your project. Don&#8217;t forget to add a registry entry or you will recieve an exception.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cmjackson.net/2010/05/14/setting-up-structure-map/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Using the Linq IQueryable Toolkit</title>
		<link>http://www.cmjackson.net/2010/02/10/using-the-linq-iqueryable-toolkit/</link>
		<comments>http://www.cmjackson.net/2010/02/10/using-the-linq-iqueryable-toolkit/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 19:14:45 +0000</pubDate>
		<dc:creator>Chris Jackson</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[IQToolkit]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.cmjackson.net/?p=385</guid>
		<description><![CDATA[The IQToolkit provides you with a framework for building your own LINQ providers. The toolkit comes with providers for Microsoft SQL, Microsoft SQL CE, Access, MySQL, and SQLite. This post will show you how to use the toolkit to talk to Microsoft SQL. Later, I&#8217;ll be attempting to write my own providers for DB2 and [...]]]></description>
			<content:encoded><![CDATA[<p>The IQToolkit provides you with a framework for building your own LINQ providers. The toolkit comes with providers for Microsoft SQL, Microsoft SQL CE, Access, MySQL, and SQLite.</p>
<p>This post will show you how to use the toolkit to talk to Microsoft SQL. Later, I&#8217;ll be attempting to write my own providers for DB2 and Interbase 7.1.</p>
<p><strong>Download the toolkit</strong><br />
First thing to do is <a href="http://www.codeplex.com/IQToolkit">download the IQToolkit</a> (version 0.16a as of this posting).</p>
<p><strong>Reference the toolkit</strong><br />
I setup a console project to test the toolkit. Within your project, create a reference to the IQToolkit.Data.dll and the IQToolkit.dll. Then, add a reference to the dll for the provider you wish to use. In this case, we are using the IQToolkit.Data.SqlClient.dll for Microsoft SQL.</p>
<p><strong>Table objects</strong><br />
Next, you will need to create a class object for each table you need to work with in the database. I have a table that has two fields: PLCID and SummaryProdMode. Create a class object to hold these properties and then use the Column attribute to bind the property to the table column.</p>
<pre class="brush: vb;">
Imports IQToolkit.Data.Mapping

Public Class Machine

    Private _id As Integer
    Private _mode As Boolean

    &lt;Column(IsPrimaryKey:=True)&gt; _
    Public Property PLCID() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    &lt;Column()&gt; _
    Public Property SummaryProdMode() As Boolean
        Get
            Return _mode
        End Get
        Set(ByVal value As Boolean)
            _mode = value
        End Set
    End Property

End Class
</pre>
<p><strong>DataContext</strong><br />
The data context contains read only properties for each database table being used. In this case, I&#8217;m dealing with the DCM_Machines table.</p>
<pre class="brush: vb;">
Imports IQToolkit
Imports IQToolkit.Data.Mapping
Imports System.Linq

Public Class MachineDataContext

    Private _provider As IEntityProvider

    Public Property EntityProvider() As IEntityProvider
        Get
            Return _provider
        End Get
        Set(ByVal value As IEntityProvider)
            _provider = value
        End Set
    End Property

    &lt;Table()&gt; _
    Public ReadOnly Property Machines() As IEntityTable(Of Machine)
        Get
            Return EntityProvider.GetTable(Of Machine)(&quot;DCM_Machines&quot;)
        End Get
    End Property

    Public Sub New(ByVal provider As IEntityProvider)

        _provider = provider

    End Sub

End Class
</pre>
<p><strong>Using the provider</strong><br />
Use the following code to setup and use the provider with LINQ.</p>
<pre class="brush: vb;">
Imports IQToolkit.Data

Module Module1

    Sub Main()

        Dim provider = DbEntityProvider.From( _
            &quot;IQToolkit.Data.SqlClient&quot;, _
            &quot;YOUR_CONNECTION_STRING&quot;, _
            &quot;Machine&quot;)
        Dim db = New MachineDataContext(provider)

        Dim machs = (From m In db.Machines _
                    Select m).ToList()

        For Each m In machs

            Console.WriteLine(&quot;id=&quot; &amp; m.PLCID)

        Next

        Console.ReadLine()

    End Sub

End Module
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.cmjackson.net/2010/02/10/using-the-linq-iqueryable-toolkit/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC &#8211; Organizing your solution</title>
		<link>http://www.cmjackson.net/2010/02/04/asp-net-mvc-organizing-your-solution/</link>
		<comments>http://www.cmjackson.net/2010/02/04/asp-net-mvc-organizing-your-solution/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 14:16:25 +0000</pubDate>
		<dc:creator>Chris Jackson</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[organization]]></category>

		<guid isPermaLink="false">http://www.cmjackson.net/?p=379</guid>
		<description><![CDATA[Keeping your project files organized will help you navigate through your projects and help you find what you are looking for to make changes faster. Here are some tips that I use to help keep my ASP.NET MVC solutions organized: Use multiple projects to separate sections of the program. Use folders to group interfaces, classes, and factories [...]]]></description>
			<content:encoded><![CDATA[<p>Keeping your project files organized will help you navigate through your projects and help you find what you are looking for to make changes faster.</p>
<p>Here are some tips that I use to help keep my ASP.NET MVC solutions organized:</p>
<ol>
<li>Use multiple projects to separate sections of the program.</li>
<li>Use folders to group interfaces, classes, and factories that deal with an object.</li>
<li>If objects can be used by other programs, put them in their own class library project.</li>
</ol>
<p><strong>Option 1</strong><br />
When beginning a new program, I usually start by creating an ASP.NET MVC solution with 4 projects:</p>
<ul>
<li><span style="text-decoration: underline;">The main project</span> &#8211; which contains the MVC components of the application such as Controllers, Views, CSS, and JavaScript files.</li>
<li><span style="text-decoration: underline;">The unit testing project</span> &#8211; which contains all of the requirements for the application along with any fake repositories used for testing.</li>
<li><span style="text-decoration: underline;">The data project</span> &#8211; which contains the repositories and domain objects used by the application.</li>
<li><span style="text-decoration: underline;">The services project</span> &#8211; which contains the service objects that are the middle-man between the Controller and the Repository.</li>
</ul>
<p><strong>Option 2</strong><br />
Folders are used within the projects to further organize the code files within. The data project could have a domain object called Operation.  Domain objects are placed in a folder with the same name as the object.  Interfaces, Repositories, and Factories that deal with the object are also placed in the domain object folder.</p>
<p>The service project is organized similar to the data project.</p>
<p>The unit test project is slightly modified from the default to create a folder which contains all fake repository objects and another folder to contain requirement tests. Depending on the detail of unit testing you wish to utilize, you may have unit tests that verify application requirements and/or unit tests that further verify other parts your code.</p>
<p>In my opinion, you can write application requirements that detail exactly what the program should do and as long as you unit test these requirements, you don&#8217;t need to waste time trying to unit test 100% of your program. If a bug is found later, it will most likely be because it was not an application requirement. Then, add a new requirement, write a unit test, and make the application pass the new test.</p>
<p><strong>Option 3</strong><br />
Where I work, we have various databases that we need to communicate with. We wanted a way to use these databases the same way every time without having to remember all of the different objects and names that needed to be used by each. So, I developed a parent wrapper class around the basic database functionality and then individual wrappers around each database object that inherited from the parent wrapper. Now, we can call up any of the database connections and work with them in exactly the same way, the only difference between them is the call to the factory when creating the instance.</p>
<p>These database wrappers were added to their own separate project so that in future applications, we could continue to reuse them.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cmjackson.net/2010/02/04/asp-net-mvc-organizing-your-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Selecting the last item of a collection using Linq-to-sql</title>
		<link>http://www.cmjackson.net/2010/01/29/selecting-the-last-item-of-a-collection-using-linq-to-sql/</link>
		<comments>http://www.cmjackson.net/2010/01/29/selecting-the-last-item-of-a-collection-using-linq-to-sql/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 14:00:00 +0000</pubDate>
		<dc:creator>Chris Jackson</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Linq To SQL]]></category>

		<guid isPermaLink="false">http://www.cmjackson.net/?p=367</guid>
		<description><![CDATA[Today I had some problems with Linq-To-SQL. I needed to get the last item from a collection and only return items where the date was older than two weeks from now. Data Tables The main items that I want to return from Linq are Revision records. A Revision has a collection of Document_History records that [...]]]></description>
			<content:encoded><![CDATA[<p>Today I had some problems with Linq-To-SQL. I needed to get the last item from a collection and only return items where the date was older than two weeks from now.</p>
<p><strong>Data Tables</strong><br />
The main items that I want to return from Linq are Revision records. A Revision has a collection of Document_History records that keep track of updates to the revision. The Document_History record has a Date field that is the date and time when the history entry was inserted, or when the revision was modified.</p>
<p><strong>The Problem</strong><br />
When using the data objects that Linq generates, you are allowed to perform some functions, such as Last and Reverse on the collection of items.</p>
<pre class="brush: plain;">Revision.Document_History.Last.Date</pre>
<p>The above code would return the most recent date, or the last time a revision was updated.</p>
<p>But, when you try to use the Last method within a Linq statement, Visual Studio throws an error.</p>
<p><strong>The Fix</strong><br />
To get around this error, I figured out that you can select the latest date using a sub query and the Max method. Below is an example of the Linq-To-SQL code that worked for me.</p>
<pre class="brush: plain;">
Dim revs As List(Of Revision) = From r In dbcontext.Revisions _
                      Where (From h In r.Document_History _
                             Select h.Date).Max &lt;= cutoffDate _
                             Select r
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.cmjackson.net/2010/01/29/selecting-the-last-item-of-a-collection-using-linq-to-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dependency Injection</title>
		<link>http://www.cmjackson.net/2010/01/27/dependency-injection/</link>
		<comments>http://www.cmjackson.net/2010/01/27/dependency-injection/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 14:33:10 +0000</pubDate>
		<dc:creator>Chris Jackson</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Object Oriented Programming]]></category>
		<category><![CDATA[patterns]]></category>

		<guid isPermaLink="false">http://www.cmjackson.net/?p=358</guid>
		<description><![CDATA[Dependency Injection is a design pattern that is used in Object Oriented Programming to create a relationship between two objects without object A depending on object B. Instead, through the use of an Interface, a place-holder for object B can be referenced in object A. Then, object B can be passed into object A where it is [...]]]></description>
			<content:encoded><![CDATA[<p>Dependency Injection is a design pattern that is used in Object Oriented Programming to create a relationship between two objects without object A depending on object B. Instead, through the use of an Interface, a place-holder for object B can be referenced in object A. Then, object B can be passed into object A where it is utilized. If there is an object C that uses the same interface as object B, it may also be passed into object A.</p>
<p><strong>A Tightly Coupled Relationship</strong><br />
Below is an example of two classes that are tightly coupled, meaning that if we want to replace object B with another class, we must change the code of object A. Object A is dependent on object B to execute.</p>
<pre class="brush: vb;">
Public Class ObjectA

  Private _obj As ObjectB

  Public Sub New(ByVal obj As ObjectB)

    _obj = obj

  End Sub

  Public Sub RunB()

    _obj.Run()

  End Sub

End Class
</pre>
<p> </p>
<pre class="brush: vb;">
Public Class ObjectB

  Public Sub Run()

    Console.WriteLine(&quot;Run from objectB&quot;)

  End Sub

End Class
</pre>
<p><strong>A Loosely Coupled Relationship</strong><br />
Now, let&#8217;s look at a better approach that will allow us to replace ObjectB with another class using the same interface or replace it with a child class of ObjectB.</p>
<p>Below, the ObjectB class now implements the IObjectB interface and the references to ObjectB in ObjectA have been replaced with a reference to the interface IObjectB instead.</p>
<pre class="brush: vb;">
Public Interface IObjectB

  Sub Run()

End Interface
</pre>
<pre class="brush: vb;">
Public Class ObjectA

  Private _obj As IObjectB

  Public Sub New(ByVal obj As IObjectB)

    _obj = obj

  End Sub

  Public Sub RunB()

    _obj.Run()

  End Sub

End Class
</pre>
<pre class="brush: vb;">
Public Class ObjectB
  Implements IObjectB

  Public Sub Run() Implements IObjectB.Run

    Console.WriteLine(&quot;Run from objectB&quot;)

  End Sub

End Class
</pre>
<p>Now, ObjectA can execute no matter if we pass into it a copy of ObjectB or another class that implements the IObjectB interface. The Interface ensures that all objects that implement it will have a subroutine called Run. That is all that ObjectA needs to know. This breaks ObjectA&#8217;s dependency on ObjectB.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cmjackson.net/2010/01/27/dependency-injection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

