# Thursday, July 29, 2010
Close up blog entry

Visual Studio tip: if the cursor is located after a variable followed by a dot without intellisense showing, press Ctrl + Space Bar to bring up intellisense. One common use of this shortcut is if the wrong property or function is chosen you can quickly select the property or function, delete the selection, and press Ctrl + Space Bar to popup intellisense to pick the correct one.

I created a short demonstration to show the shortcut in action:

I hope this helps!

Share/Bookmark
Thursday, July 29, 2010 10:13:32 PM (Eastern Daylight Time, UTC-04:00)  #    Disclaimer  |  Comments [0]  | 
Close up blog entry

Visual Studio tip: if your cursor is located between the parenthesis in a C# function/method you can use use Ctrl + Shift + Space Bar to bring up the function parameter help popup. 

I created a short video to demonstrate how the shortcut works:

I hope this tip helps!

Share/Bookmark
Thursday, July 29, 2010 9:35:50 PM (Eastern Daylight Time, UTC-04:00)  #    Disclaimer  |  Comments [0]  | 
# Saturday, July 24, 2010
Close up blog entry

I created a screen cast to show how to use CodeRush’s SelectionExpand and SelectionReduce commands. This is one of those “I can’t live without” features that I use when editing code. These keyboard commands allow quick selection of blocks of code which speed up your code editing time.

Use it and code on!

Share/Bookmark
Saturday, July 24, 2010 1:18:15 AM (Eastern Daylight Time, UTC-04:00)  #    Disclaimer  |  Comments [0]  | 
# Thursday, July 22, 2010
Close up blog entry

Using CodeRush from DevExpress you can very quickly change function/method visibility without typing the visibility text :-)

In a code file, if your cursor is anywhere within or on a function definition, you can use the left Alt key plus the up and down arrow keys to cycle through the various visibility modifiers.

I created a short screen cast to show it in action.

I hope this tip helps :-)

Share/Bookmark
Thursday, July 22, 2010 11:35:08 PM (Eastern Daylight Time, UTC-04:00)  #    Disclaimer  |  Comments [0]  | 
# Wednesday, July 21, 2010
Close up blog entry

CodeRush is one of my can’t live without tools :-) I created a short screen cast to demo the unit test runner that comes with CodeRush from DevExpress.

I use it to run my unit tests at home and work :-D

Share/Bookmark
Wednesday, July 21, 2010 11:45:08 PM (Eastern Daylight Time, UTC-04:00)  #    Disclaimer  |  Comments [0]  | 
Close up blog entry

I was assigning the output of Git to a variable in a Windows PowerShell script and it looked as if I was losing the carriage returns and line feeds in the output. What is actually happening is the output of the executable is being broken out into an array of strings.

The following shows an example PowerShell session with an explanation following.

Windows PowerShell Losing Line Feed Example

  1. Running the git program returns output that is delimited by line breaks.
  2. Assign the output of the program to a variable.
  3. Write-Host the variable and the line breaks are gone >:-{
  4. You can see the variable is actually an array!
  5. You can create a string using the .NET String.Join function.
  6. Now Write-Host of the variable has the line breaks.

I created a short screen cast to explain this as well.

I hope this helps. Code on!

Share/Bookmark
Wednesday, July 21, 2010 11:41:03 PM (Eastern Daylight Time, UTC-04:00)  #    Disclaimer  |  Comments [0]  | 
Close up blog entry

Hmm… I want to show the status of my Git repository on my Windows PowerShell prompt. I found a post on Stack Overflow that gives a good example of showing Git repo status on a PowerShell prompt, but I customized it a bit to show data I want to see.

Git status on my PowerShell prompt when the index and working directory are clean:

Git PowerShell Prompt Nothing To Commit Working Directory Clean

When there are differences in the Git index or in the working folder then the prompt will look like the following:

Git PowerShell Prompt With Index And Working Directory Changes

The meaning of the status is documented on the git-status man page for the --s, –short, or --porcelain argument. I’m using git status –porcelain, as it not supposed to change in the future. Please note that I replace the space in the status with a dash.

GitStatusKey

The script for the profile.ps1 file follows:

$Global:CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$UserType = "User"
$CurrentUser.Groups | foreach {
      if ($_.value -eq "S-1-5-32-544")
      {
         $UserType = "Admin"
      }
   }

function prompt
{
   # Fun stuff if using the standard PowerShell prompt; not useful for Console2.
   # This, and the variables above, could be commented out.
   if($UserType -eq "Admin")
   {
      $host.UI.RawUI.WindowTitle = "" + $(get-location) + " : Admin"
      $host.UI.RawUI.ForegroundColor = "white"
   }
   else
   {
      $host.ui.rawui.WindowTitle = $(get-location)
   }

   Write-Host("")
   $statusString = ""
   $symbolicref = git symbolic-ref HEAD
   if($symbolicref -ne $NULL)
   {
      $statusString += "GIT [" + $symbolicref.substring($symbolicref.LastIndexOf("/") +1) + "] "

      $status = git status --porcelain #--untracked-files=all
   
      if ( $status )
      {
         $matches = [regex]::matches([system.string]::join("`n", $status), "(?m)^.{2}")
         
         $statusTotals = @{} # Create hash table
         
         foreach ( $match in $matches )
         {
            if ( ![string]::IsNullOrEmpty($match.Value) )
            {
               $matchValue = $match.Value.Replace(" ", "-")
               
               if ( !$statusTotals.ContainsKey($matchValue) )
               {
                  $statusTotals.Add($matchValue, 1)
               }
               else
               {
                  $statusTotals.Set_Item($matchValue, $statusTotals.Get_Item($matchValue) + 1)
               }
            }
         }
   
         foreach ( $dictEntry in $statusTotals.GetEnumerator() | Sort-Object Name)
         {
            $statusString += $outVal = [string]::format("{0}:{1} ", $dictEntry.Name, $dictEntry.Value)
         }
      }
      else
      {
         $statusString += "nothing to commit (working dir clean)"
      }
   }
   else
   {
      $statusString = "PS "
   }

   if ($statusString.StartsWith("GIT")) {
      Write-Host ($statusString + [System.Environment]::NewLine + $(get-location) + ">") -nonewline -foregroundcolor yellow
   }
   else {
      Write-Host ($statusString + $(get-location) + ">") -nonewline -foregroundcolor green
   }
   return " "
}

You can place your profile.ps1 file in your ~\Document\WindowsPowerShell folder.

I created a short screen cast to go over the Git PowerShell prompt as well:

Hope you like it :-) It has brought me much Git PowerShell happiness :-D

Share/Bookmark
Wednesday, July 21, 2010 11:36:53 PM (Eastern Daylight Time, UTC-04:00)  #    Disclaimer  |  Comments [0]  | 
Close up blog entry

It can be useful when mocking a function with Moq to use parameters passed into the Setup of a mocked function in the Returns function.

I created a unit test to illustrate the basic idea:

  
[TestFixture]
public class TestFixture1
{
   public interface ITest
   {
      int Func(int x, int y);
   }

   [Test]
   public void Test()
   {
      Mock<ITest> mockTest = new Mock<ITest>(MockBehavior.Strict);

      mockTest.Setup(x => x.Func(It.IsAny<int>(), It.IsAny<int>()))
         .Returns((int x, int y) => { return FuncData(x, y); });

      ITest test = mockTest.Object;

      Assert.AreEqual(5, test.Func(2, 3));
      Assert.AreEqual(3, test.Func(1, 2));
   }

   private int FuncData(int x, int y)
   {
      return x + y;
   }
}

The main idea is to use a lambda expression in the Returns function that specifies the arguments that can be used in whatever way is needed.

I created a short screen cast to demo how this works:

I hope this helps! Code on :-)

.NET | Moq | Technical
Share/Bookmark
Wednesday, July 21, 2010 11:18:25 PM (Eastern Daylight Time, UTC-04:00)  #    Disclaimer  |  Comments [0]  | 
Close up blog entry

There is no clear cut mechanism provided by the ASP.NET MVC framework to pass data from a view to it’s master page. I’ve been using a technique that I’m reasonably happy with, which I’ll describe in this blog post.

For starters, my example will be using the ITypeInstanceDictionary provided in the TTC Tools. Please read my blog post pass data from an ASP.NET MVC controller to view to learn more. The code shown in this blog post is included in a demo web application on the TTC Tools GitHub page.

There are many scenarios in which you want to drive master page values from the view. For example: some CSS styling that exists in the master page will be dependent on the view. Personally I feel this should not be driven by the controller, as there are many situations in which CSS styles are purely display driven.

I’ll summarize the basic concepts for passing data from the view to it’s master page.

  • Create an interface that is used by the master page for use in the master page markup code, and allow the view to override these values.
  • Setup the master page view data at the top of the master page
  • Follow up the master page code with a content place holder.
  • This content place holder will be used by the view to override any master page data values.

My example code will show how to override the pages body element id in the master page from a view.

The following code snippet shows an example master page implementation:

Setup ASP.NET MVC Master Page To Allow View To Override Data

The view can override master page view data by creating a content control for the master page’s content place holder which follows the setup of the master page’s view data.

ASP.NET MVC View Pass Data To Master Page

The ASP.NET MVC view engine rendering execution flow will start at the top of the master page and then flow into the content place holders. This allows the view to override the master page view data before it is used later in the master page markup code.

I created a screen cast which might help explain this technique in a bit more detail.

If you need any further explanation or have any questions please let me know. I hope someone finds this useful :-)

Share/Bookmark
Wednesday, July 21, 2010 11:05:35 PM (Eastern Daylight Time, UTC-04:00)  #    Disclaimer  |  Comments [0]  | 
Close up blog entry

The default mechanism for passing data from an ASP.NET MVC controller to the view is to use the ViewData property of the controller and view which is of type ViewDataDictionary. The ViewDataDictionary is a string indexed collection of type object. My main issue with this mechanism is two fold. I’m not a big fan of string indexed collections and I don’t want to have to cast to get at the type of the class instance in the collection.

The next option is to use the Model property of the ViewDataDictionary. The Model property of ViewData allows a strongly typed way to pass data from the controller to the view. The question becomes what type should be used? One choice is to use a specific type for each controller action and view, but this gets a bit cumbersome. Instead I propose using a collection indexed by type which is assigned to the Model property of ViewData.

I created a project called TTC Tools which houses the ITypeInstanceDictionary, which provides a generic mechanism to pass various types from controller to view in a more strongly typed way than the ViewDataDictionary.

I documented some basic usage scenarios for the type instance dictionary on my wiki. I also created a screen cast to document basic usage in an ASP.NET MVC application.

The basic idea is the dictionary provides the template functions Add<T>, Get<T>, and Contains<T>. The type T is used as the indexer of the dictionary, so instances of various types can be added and retrieved from the dictionary.

A very basic usage scenario from a unit test is illustrated as follows:

ITypeInstanceDictionary Basic Usage Example

The type does not have to be specified for the Add call, as the declared type of the parameter will be used. The Get call must declare the type, as the type T is used for the dictionary index.

The type instance dictionary does allow multiple instances of the same type to be added, but only if some other type of instance identifier is supplied. For example an enumeration can be used to differentiate the types. An example of adding and retrieving multiple instances of the same type using an enumerated type for the instance id follows:

ITypeInstanceDictionary Multiple Of Same Instance Example

Basic use in an ASP.NET MVC application

For convenience, derive all your controllers from a base controller that provides a  property of type ITypeInstanceDictionary. My example uses a property named TypeViewData.  The code for the base controller follows:

   public class BaseController : Controller
   {
      ITypeInstanceDictionary _typeViewData = new TypeInstanceDictionary();

      public ITypeInstanceDictionary TypeViewData
      {
         get
         {
            return _typeViewData;
         }
         set
         {
            _typeViewData = value;
         }
      }
   }

Controller actions that return a ViewResult should use the controller View function overload which accepts the model argument. The TypeViewData property should be passed into the view function as follows:

Return ITypeInstanceDictionary From ASP.NET MVC Controller Action

The view should then use the template version of the ViewPage class, with the type set to TTC.Tools.ITypeInstanceDictionary. The template version of the ViewPage class strongly types the Model property of the View.  The Model property can then use the Get<T> function to retrieve the data sent from the controller. An example of the view code follows:

ITypeInstanceDictionary ASP.NET MVC View Example

You can find the code, which includes a sample web site using the type instance dictionary, at the TTC Tools GitHub page. I’ve also created some basic documentation on my wiki.

If you have any questions please let me know!  I’m not sure if others will find this useful, but you never know :-)

Share/Bookmark
Wednesday, July 21, 2010 10:40:22 PM (Eastern Daylight Time, UTC-04:00)  #    Disclaimer  |  Comments [0]  | 
# Wednesday, July 07, 2010
Close up blog entry

You might get stuck on the Wii Lego Harry Potter Expelliarmus Lesson. You can end up in the room stuck on the tables unable to get off.  I searched a bit and found the solution on this Lego Harry Potter walkthrough. The short of it is to press C to skip the video of Snape attacking the other teacher :-)

Share/Bookmark
Wednesday, July 07, 2010 10:41:21 PM (Eastern Daylight Time, UTC-04:00)  #    Disclaimer  |  Comments [0]  | 
# Sunday, July 04, 2010
Close up blog entry

I happened across a nice shortcut to copy and paste text in Windows PowerShell. Highlight some text in PowerShell with the mouse, right click to copy the text, and right click again to paste the text at the cursor. If you right click in PowerShell, the current text content of the clipboard will be copied at the cursor.

I did a short video showing this happy shortcut on Windows 7.

Share/Bookmark
Sunday, July 04, 2010 11:42:40 PM (Eastern Daylight Time, UTC-04:00)  #    Disclaimer  |  Comments [0]  |