Generating XML Comments with GhostDoc

Date Added: July 20, 2010 08:26 by By Edward
Categories: ASP.NET, Development Resources, Other

GhostDoc is a free Visual Studio extension which I stumbled accross while looking for something to help me with commenting my code. GhostDoc automatically generates XML documentation comments for methods and properties based on their type, parameters, name, and other contextual information. It takes the 'pain' out of commenting each method word for word - saving you time doing what you do best - coding!

Most documentation created will be a waste of time for a developer, and usually when there is no time to waste a developer might find the documentation is slowing him down. For any decent documentation to be useful, a developer must know that when the documentation was initially generated it was both correct and complete, it has also been updated as the project scope changed, or amendments was made to the code. I ofter find code to out of sync with documentation, which leaves me with lots of 'catch up' to do, before I feel in control and productive.

You can set it up, so when you right click on a method the "Document This" option becomes available that allows you to generate summary comments for your method.

GhostDoc comments

Here is an example of summary comments I created using this tool.       

        /// <summary>
        /// Checks for SQL injection.
        /// </summary>
        /// <param name="userInput">The user input.</param>
        /// <returns></returns>
        public static string checkForSQLInjection(string userInput)
        {
        // code here
       }

It is also supported in the following versions of the Visual Studio IDEs:

  • Visual Studio 2010
  • Visual Studio 2008
  • Visual Studio 2005

Supported Languages:

  • VB.NET
  • C#

Download it from here:

http://visualstudiogallery.msdn.microsoft.com/en-us/46A20578-F0D5-4B1E-B55D-F001A6345748

New Overload Method Available for String.Concat - available in .Net 4 Framework

Date Added: April 30, 2010 16:57 by By Edward
Categories: ASP.NET, Development Resources

A new feature I noticed while doing some prototyping some functionality with Visual Studio 2010, is the new overload method available for the 'String.Concat' method that takes an IEnumerable<T>. I found this to be very useful with LINQ query expressions.

Below is a simple example:

       

 public static void Main()
        {
            List<Employee> employees = new List<Employee>();

            employees.Add(new employees("John", "Doe"));
            employees.Add(new employees("Jane", "Doe"));
            employees.Add(new employees("John", "Code"));

            string output = String.Concat(employees.Where(employee =>
                          (employee.Surname == "Doe")));

            //write the output to screen
            Console.WriteLine(output);
        }


The Enumerable.Where extension method is called to extract the Employee objects whose 'Surname' property equals "Doe". The result is passed to the Concat<T>(IEnumerable<T>) method and displayed to the console.

 

String.IsNullOrWhiteSpace - New method available in .NET Framework 4

Date Added: February 19, 2010 14:11 by By Edward
Categories: ASP.NET, Development Resources

String.IsNullOrWhiteSpace - New method available in .NET Framework 4

I found a very nice addition to the .NET Framework(version 4) while experimenting with code in Visual Studio 2010. There is a new method called "IsNullOrWhiteSpace", which is more powerful than the more familiar "IsNullOrEmpty" method. Incase you were wondering, the "IsNullOrEmpty" method is still available to be used.

For example, the common whitespace symbol " " represents a blank space, as used between words and sentences. The most common whitespace characters may be typed via the space bar or the Tab key. Depending on context, a line-break generated by the Return key (Enter key) may be considered whitespace as well. This brings concerns when using the "IsNullOrEmpty" method.

Introducing "IsNullOrWhiteSpace"! In the .NET Framework 4, string.IsNullOrWhiteSpace() will return true if a string is full of whitespace characters. I found this very helpful when dealing with getting XML data from external web services.

Here is a short example of how to use this new method.

   1:          static void Main(string[] args)
   2:          {
   3:              // set some test values
   4:              string[] testValues = { null, String.Empty, "ABCDE", 
   5:                            new String(' ', 20), "  	   ", 
   6:                            new String(' ', 10) };
   7:              //loop through the list and return result
   8:              foreach (string value in testValues)
   9:              {
  10:                  Console.WriteLine("Return is: " + 
  11:              String.IsNullOrWhiteSpace(value));
  12:              }
  13:              // we want to see the return
  14:              Console.ReadLine();
  15:          }

 

Design Patterns 101 - The Abstract Factory Pattern

Date Added: January 31, 2010 18:41 by By Edward
Categories: ASP.NET, Development Resources, Social Media

In my second article on design patterns, I am going to give you a quick overview of the "Abstract Factory pattern". Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

The Abstract Factory pattern is one level of abstraction higher than the factory pattern. One of the nice things about abstraction is that it lets you "take care" of the bigger picture and you only have to worry about the "details" later. The great advantage is that you are able to rely upon some other class to fill in the details for you.

You can use this pattern when you want to return one of several related classes of objects, each of which can return several different objects on request. In other words, the Abstract Factory is a factory object that returns one of several factories.

The code below demonstrates the Abstract Factory pattern creating parallel hierarchies of objects. Object creation has been abstracted and there is no need for hard-coded class names in the client code.

 

   1:  // Abstract Factory pattern - Structural example
   2:   
   3:  using System;
   4:   
   5:  namespace MyApp.AbstractFactory.Structural 
   6:  {
   7:      /// <summary>
   8:      /// MainApp startup class for Structural
   9:      /// Abstract Factory Design Pattern.
  10:      /// </summary>
  11:      internal class MainApp
  12:      {
  13:          /// <summary>
  14:          /// Entry point into console application.
  15:          /// </summary>
  16:          public static void Main()
  17:          {
  18:              // Abstract factory #1
  19:              AbstractFactory factory1 = new ConcreteFactory1();
  20:              Client client1 = new Client(factory1);
  21:              client1.Run();
  22:   
  23:              // Abstract factory #2
  24:              AbstractFactory factory2 = new ConcreteFactory2();
  25:              Client client2 = new Client(factory2);
  26:              client2.Run();
  27:   
  28:   
  29:              // Wait for user input
  30:              Console.ReadKey();
  31:          }
  32:      }
  33:   
  34:      /// <summary>
  35:      /// The 'AbstractFactory' abstract class
  36:      /// </summary>
  37:      internal abstract class AbstractFactory
  38:      {
  39:          public abstract AbstractProductA CreateProductA();
  40:          public abstract AbstractProductB CreateProductB();
  41:      }
  42:   
  43:      /// <summary>
  44:      /// The 'ConcreteFactory1' class
  45:      /// </summary>
  46:      internal class ConcreteFactory1 : AbstractFactory
  47:      {
  48:          public override AbstractProductA CreateProductA()
  49:          {
  50:              return new ProductA1();
  51:          }
  52:   
  53:          public override AbstractProductB CreateProductB()
  54:          {
  55:              return new ProductB1();
  56:          }
  57:      }
  58:   
  59:      /// <summary>
  60:      /// The 'ConcreteFactory2' class
  61:      /// </summary>
  62:      internal class ConcreteFactory2 : AbstractFactory
  63:      {
  64:          public override AbstractProductA CreateProductA()
  65:          {
  66:              return new ProductA2();
  67:          }
  68:   
  69:          public override AbstractProductB CreateProductB()
  70:          {
  71:              return new ProductB2();
  72:          }
  73:      }
  74:   
  75:      /// <summary>
  76:      /// The 'AbstractProductA' abstract class
  77:      /// </summary>
  78:      internal abstract class AbstractProductA
  79:      {
  80:      }
  81:   
  82:      /// <summary>
  83:      /// The 'AbstractProductB' abstract class
  84:      /// </summary>
  85:      internal abstract class AbstractProductB
  86:      {
  87:          public abstract void Interact(AbstractProductA a);
  88:      }
  89:   
  90:      /// <summary>
  91:      /// The 'ProductA1' class
  92:      /// </summary>
  93:      internal class ProductA1 : AbstractProductA
  94:      {
  95:      }
  96:   
  97:      /// <summary>
  98:      /// The 'ProductB1' class
  99:      /// </summary>
 100:      internal class ProductB1 : AbstractProductB
 101:      {
 102:          public override void Interact(AbstractProductA a)
 103:          {
 104:              Console.WriteLine(this.GetType().Name +
 105:                                " interacts with " + a.GetType().Name);
 106:          }
 107:      }
 108:   
 109:      /// <summary>
 110:      /// The 'ProductA2' class
 111:      /// </summary>
 112:      internal class ProductA2 : AbstractProductA
 113:      {
 114:      }
 115:   
 116:      /// <summary>
 117:      /// The 'ProductB2' class
 118:      /// </summary>
 119:      internal class ProductB2 : AbstractProductB
 120:      {
 121:          public override void Interact(AbstractProductA a)
 122:          {
 123:              Console.WriteLine(this.GetType().Name +
 124:                                " interacts with " + a.GetType().Name);
 125:          }
 126:      }
 127:   
 128:      /// <summary>
 129:      /// The 'Client' class. Interaction environment for the products.
 130:      /// </summary>
 131:      internal class Client
 132:      {
 133:          private AbstractProductA _abstractProductA;
 134:          private AbstractProductB _abstractProductB;
 135:          
 136:          // Constructor
 137:   
 138:          public Client(AbstractFactory factory)
 139:          {
 140:              _abstractProductB = factory.CreateProductB();
 141:              _abstractProductA = factory.CreateProductA();
 142:          }
 143:          
 144:          public void Run()
 145:          {
 146:              _abstractProductB.Interact(_abstractProductA);
 147:          }
 148:      }
 149:  }

Output:

ProductB1 interacts with ProductA1
ProductB2 interacts with ProductA2


The Usage of this pattern makes it possible to interchange concrete classes without changing the code that uses them, even at runtime! However, employment of this pattern, as with similar design patterns, may result in unnecessary complexity and extra work in the initial writing of code. Used correctly the "extra work" pays off in the second instance of using the Factory.

Design Patterns 101 - The Singleton Pattern

Date Added: January 29, 2010 18:53 by By Edward
Categories: ASP.NET, Development Resources, Social Media

You probably heard of it, you probably don't even know that you are implementing it, but you need it on your resume! Once you start to use design patterns you will find that your code structure is improving. This is a "101" article about the simplest pattern - the Singleton pattern.

The purpose of this pattern is to ensure that a class has only one instance, and provide a global point of access to it. Any class in your application that has access to its namespace doesn't have to create or initialize the singleton. The caller can access properties and methods through the singleton's instance property. The singleton will retain state across calls.

   1:  // Singleton pattern - Structural example
   2:   
   3:  using System; 
   4:   
   5:  namespace MyApp.Singleton.Structural
   6:  {
   7:   
   8:    /// <summary>
   9:    /// MainApp startup class for Structural
  10:    /// Singleton Design Pattern.
  11:    /// </summary>
  12:   
  13:    class MainApp
  14:    {
  15:      /// <summary>
  16:      /// Entry point into console application.
  17:      /// </summary>
  18:   
  19:      static void Main()
  20:      {
  21:        // Constructor is protected -- cannot use new
  22:        Singleton s1 = Singleton.Instance();
  23:        Singleton s2 = Singleton.Instance(); 
  24:   
  25:        // Test for same instance
  26:        if (s1 == s2)
  27:        {
  28:          Console.WriteLine("Objects are the same instance");
  29:        }
  30:   
  31:        // Wait for user
  32:        Console.ReadKey();
  33:      }
  34:    }
  35:   
  36:    /// <summary>
  37:    /// The 'Singleton' class
  38:    /// </summary>
  39:   
  40:    class Singleton
  41:    {
  42:   
  43:      private static Singleton _instance;
  44:   
  45:      // Constructor is 'protected'
  46:      protected Singleton()
  47:      {
  48:      }
  49:   
  50:      public static Singleton Instance()
  51:      {
  52:        // Uses lazy initialization.
  53:        // Note: this is not thread safe.
  54:        if (_instance == null)
  55:        {
  56:          _instance = new Singleton();
  57:        }
  58:   
  59:   
  60:        return _instance;
  61:      }
  62:    }
  63:  }


Output:

Objects are the same instance

Note: The singleton pattern must be carefully constructed in multi-threaded applications. If two threads are to execute the creation method at the same time when a singleton does not yet exist, they both must check for an instance of the singleton and then only one should create the new one.

Download Updated VS2010 and .NET 4 Training Kit

Date Added: January 12, 2010 14:41 by By Edward
Categories: ASP.NET, Development Resources, Technology

The updated VS2010 and .NET 4 Training Kit includes presentations, hands-on labs, and demos. Some of the features include:

  • C# 4.0
  • Visual Basic 10
  • F#
  • Parallel Extensions
  • Windows Communication Foundation
  • Windows Workflow
  • Windows Presentation Foundation
  • ASP.NET 4
  • Windows 7
  • Entity Framework
  • ADO.NET Data Services
  • Managed Extensibility Framework
  • Visual Studio Team System

You can download the training kit from the Microsoft website.

Come next month, Microsoft will offer testers yet another development milestone release of both Visual Studio 2010 and .NET Framework 4.

This version of the Training Kit works with Visual Studio 2010 Beta 2 and .NET Framework 4 Beta 2.

What is ahead for .Net in 2010?

Date Added: November 29, 2009 18:04 by By Edward
Categories: AJAX/JQuery, ASP.NET, Development Resources, Technology

We are almost at the end of 2009, and this year there have been some interesting things happening in the world of .Net and Microsoft. There was the release of Silverlight 3, Internet Explorer 8, updates to the AJAX library and toolkit, and just over a month ago we got Windows 7.

I thought I would point out a few new .Net technologies to look out for next year.

  • Visual Studio 2010 and ASP.Net 4.0: Microsoft already released beta versions of Visual Studio 2010 that will be running the ASP.Net framework 4.0. They aim to release the newest edition of Visual Studio and ASP.Net 4.0 on 22 March 2010.
  • MVC Framework: The first version of this framework has been released in March 2009. Currently MVC 2.0 Beta is available for download. Microsoft has made several changes to this framework since the first release. You should find the latest version available for download in the first half of 2010.
  • JQuery: It has taken Microsoft a while to wake up, but it is nice to know that JQuery is being adopted by Visual Studio. JQuery is a Javascript library that has a lot of neat tools in it's bag.  It is very helpful for taking care of mundane tasks like "get that div" or "set that text box value."  It also has a great set of methods for dealing with AJAX.  
  • WCF, WF and WPF: ASP.Net is a maturing framework that will continue to move forward, with or without you. The best advice I have is to get on the wagon. Tools and accompanying frameworks are maturing in line, but behind, the .NET framework - your applications should be as well.  The earlier you start, the better. WCF, or known as Windows Communication Framework, is used to abstract the "plumbing" of your application. It can save you hours and hours of coding.
  • Silverlight 4.0: Microsoft Silverlight is a web application framework that provides functionalities similar to those in Adobe Flash, integrating multimedia, graphics, animations and interactivity into a single runtime environment. Silverlight 4.0 Beta has been released last month, so do not be surprised to see the latest version available before June 2010.

List of Windows 7 Shortcuts

Date Added: October 29, 2009 18:08 by By Edward
Categories: Development Resources, Other, Technology

As Windows 7 starts rolling out, and more people are starting to use it, I thought I would add a few Windows shortcuts.

The full list of keyboard shortcuts includes:

  • Win+Home: Clear all but the active window.
  • Win+Space: All windows become transparent so you can see through to the desktop.
  • Win+Up arrow: Maximize the active window.
  • Shift+Win+Up arrow: Maximize the active window vertically.
  • Win+Down arrow: Minimize the window/Restore the window if it's maximized.
  • Win+Left/Right arrows: Dock the window to each side of the monitor.
  • Shift+Win+Left/Right arrows: Move the window to the monitor on the left or right.


A list of shortcut combinations to launch the applications in their respective position on the taskbar:

  • Win+number (1-9): Starts the application pinned to the taskbar in that position, or switches to that program.
  • Shift+Win+number (1-9): Starts a new instance of the application pinned to the taskbar in that position.
  • Ctrl+Win+number (1-9): Cycles through open windows for the application pinned to the taskbar in that position.
  • Alt+Win+number (1-9): Opens the Jump List for the application pinned to the taskbar.
  • Win+T: Focus and scroll through items on the taskbar.
  • Win+B: Focuses the System Tray icons


A list of more advanced shortcuts:

  • Ctrl+Shift+N: Creates a new folder in Windows Explorer.
  • Alt+Up: Goes up a folder level in Windows Explorer.
  • Alt+P: Toggles the preview pane in Windows Explorer.
  • Shift+Right-Click on a file: Adds Copy as Path, which copies the path of a file to the clipboard.
  • Shift+Right-Click on a file: Adds extra hidden items to the Send To menu.
  • Shift+Right-Click on a folder: Adds Command Prompt Here, which lets you easily open a command prompt in that folder.
  • Win+P: Adjust presentation settings for your display.
  • Win+(+/-): Zoom in/out.
  • Win+G: Cycle between the Windows Gadgets on your screen.

Cleanup your code - remove unused using statements

Date Added: October 20, 2009 06:12 by By Edward
Categories: ASP.NET, Development Resources

One of the annoying things I have been dealing with in Visual Studio 2008 and Framework 3.5, is the unnecessary "using" statements the IDE adds, when you add a new webpage to your project.

Visual Studio does give you the option to remove these unused using statements, but they do not "tell" you about it.

Here is a quick way of removing unused using statements.

From the Context Menu:

  1. 1) Right-click anywhere inside the code editor,
  2. 2) point to Organize Usings,
  3. 3) now click Remove Unused Usings.


Here is an example:


Before:

   1:  using System;
   2:  using System.Linq;
   3:  using System.Collections.Generic;
   4:  using System.Text;
   5:  using System;
   6:   
   7:  namespace ConsoleApplication1
   8:  {
   9:      class Program
  10:      {
  11:          static void Main(string[] args)
  12:          {
  13:              Console.WriteLine("DasCode.Net test page, removing 
unused using statements"
);
  14:          }
  15:      }
  16:  }


After:

   1:  using System;
   2:   
   3:  namespace ConsoleApplication1
   4:  {
   5:      class Program
   6:      {
   7:          static void Main(string[] args)
   8:          {
   9:              Console.WriteLine("DasCode.Net test page, removing 
unused using statements"
);
  10:          }
  11:      }
  12:  }

 

Microsoft announces the WebsiteSpark Program

Date Added: September 24, 2009 14:10 by By Edward
Categories: Development Resources, Technology

Visibility, support and software for professional Web Developers and Designers - at no upfront cost!

Scott Guthrie from Microsoft today announced the WebsiteSpark Program, a program that encourages independent web developers or web development companies that build web applications on behalf of others. This program enables you to use strategic and sometimes expensive software from Microsoft for FREE, thus at no cost, for up to three years.

Here is a list of software you can enrol for:

  1. 3 licenses of Visual Studio 2008 Professional Edition
  2. 1 license of Expression Studio 3 (which includes Expression Blend, Sketchflow, and Web)
  3. 2 licenses of Expression Web 3
  4. 4 processor licenses of Windows Web Server 2008 R2
  5. 4 processor licenses of SQL Server 2008 Web Edition
  6. DotNetPanel control panel (enabling easy remote/hosted management of your servers)

The only two requirements to join the program are:

  •    Your company(or yourself if self-employed) builds web sites and web application on behalf of others.
  •    Your company currently has less than 10 employees.

For more information on this program, visit Scott's blog entry, or the program website.

About DasCode.Net

I'm a ASP.NET web developer and code enthusiast. Blogging about everything .Net related.

Code... that's .net