ObjectSharp Blogs

You are currently viewing

Dave Lloyd's 2 Cents

A .NET Developer's Perspective


Snippets

Well Snippets are so easy it doesn't make sense not to create them.

I am working on a VB.net 2005 windows application. This client has a very well defined framework. So implementing some feature of the framework is often a matter of finding an example of it in another part of the code then copy and paste. This application was started in VS 2003 and this is primarily what we did. In the beginning we had templates but no one ever wants to keep them up to date as the framework evolves.

Now we have migrated to VS 2005, it's time to take advantage of snippets. So I thought I should try making some, see if it's worth the effort. I was shocked at how easy it is. Let me give you an example: We have a common IU for all forms that display what we define as a document (Order, Product, Customer)

 If you have a piece of code to write that is common in your application. You find a sample of the code in another object. You copy it and paste it into your own object then change the names to suite your object. If you take a few minutes and create a code snippet from this the next time it will be that much faster. The time it takes to create the snippet, you will get back the next time you use it.

  • There will be no looking for it in another object.
  • There will be no removing of the extra stuff from the other example.
  • It will be easier to change the names in the new code.

So here is what I did.

First of all to organize your snippets just create a directory structure to store them in. For example in this application there are 4 distinct object types. Representing different layers. This is a windows application therefore we have Windows Forms, Business Objects, Business Entities and Data Access. Therefore I divided the snippets into this directory structure.

YourName
    Windows Form
    Business Object
    Business Entity
    Data Access

Now that we have the directory structure all you have to do is open Visual Studio 2005 and select the menu item Tools |Code Snippet Manager... This dialog is a bit confusing at first there is an add and an import button. Import is for importing a single snippet into an existing Folder. Add allows you to add a directory of snippets all at once. So We'll use the add button to add your folder to snippets. The folder names show up in VS when you select the Snippets so choose them carefully. Even if you don't have snippets for each object type yet add the folder anyway. They won't show up until you put snippets in them.

Creating a Snippet

Now lets create a snippet to add to our folders. Snippets are just an XML file. If you like writing XML as much as I do then you will want to find a snippet editor :) . I downloaded a couple but only installed one. I had every intension of trying out a few of them but this one was so easy to use that I had created about 10 snippets before I realized I was still using the first snippet editor. The one I am using is called snippy it's one of the Power toys for Visual Studio 8.

 

How to use Snippy

First you enter some basic information like the name of the snippet a description of it and your name. The shortcut can be used by the developer to jump straight to this snippet instead of going through the snippet selector.

Then you select the type of snippet. Expansion, Surrounds With, or Refactoring. I have only tried Expansion so far. So that is what we will continue on with. :)

 

Enter any name spaces you need to include for the code being added.

 

Now for the code. You have to select the language (C#, VB, J# or XML ) and kind of code you are entering. Then paste the code in. Anything that you want the user to replace at run time just enter a keyword with dollar signs around it. In my example above the object we are creating will have a name (ie: Customer, Order, Product) so I used the keyword object with dollar signs around it where ever I want to replace the ObjectName in the code. When the snippet is inserted into the code the user will be able to tab through the code changing any of the items you have identified as $object$.

 

Now for the Literals and Objects this is where you create those place holders mentioned in the code section. Click the add button and you can add a literal for anything you put in the code that the user should replace. The ID must be the same as the name you surrounded with dollar signs.

How simple is that?

Comments

    No Comments

Leave a Comment

(required) 
(optional)
(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS