Dave Burke : Freelance .NET Web Developer specializing in Online Communities

CodeSmith 4.0 ActiveSnippet Example

ActiveSnippets are a new feature in CodeSmith 4.0 and much like Visual Studio 2005 snippets.  The CodeSmith 4.0 Press Release describes ActiveSnippets with, "Imagine Visual Studio 2005 snippets, but with the full power of CodeSmith available to execute any logic or access any complex metadata (including database schema and xml data) to control the output of your snippets."

That's a very helpful description, I think, but ActiveSnippets are not 100% integrated into the Visual Studio IDE like VS2005 snippets are, where you can type a few characters and Intellisense takes care of the rest.  That said, ActiveSnippets are actually pretty tightly integrated into the VS2005 IDE, so seeing them in action here should be enough to get you started.

First, ActiveSnippet templates are like any other CodeSmith template as far as content.  There may be additional argument handling required in an ActiveSnippet template than a normal template, but other than that they're pretty much identical. 

ActiveSnippets are accessed in VS2005 through the Tools->CodeSmith menu as shown here.

 

 

CodeSmith ActiveSnippets are created with the ActiveSnippet Configuration tool available from the CodeSmith tools menu and looks like the window below.

 

 

Any time you want to see your ActiveSnippet list, you can use the "Output ActiveSnippet Usage" option which will display the window below.

 

 

We're going to demo the "CV" ActiveSnippet and enter "cv MySweetEvent ArgOne=string" in our VS2005 source editor.  Now, to get additional info about the ActiveSnippet we can then use "Output ActiveSnippet Usage" again (Ctrl-E, Ctrl-R) and see its usage with parameters.

 

 

Now we're ready to rock.  With our cursor anywhere on the "cv MySweetEvent One=string" line, we use the Expand ActiveSnippet menu (Ctrl-E, Ctrl-E) and shazam!  The following code appears.

 

#region 'MySweetEvent' event definition code
/// <summary>
/// EventArgs derived type which holds the custom event fields
/// </summary>
[Serializable]
public class MySweetEventEventArgs : System.EventArgs
{
/// <summary>
/// Use this constructor to initialize the event arguments
/// object with the custom event fields
/// </summary>
public MySweetEventEventArgs(string one)
{
_one = one;
}

private string _one;

public string One
{
get { return _one; }
set { _one = value; }
}

}

/// <summary>
/// This represents the delegate method prototype that
/// event receivers must implement
/// </summary>
public delegate void MySweetEventEventHandler(object sender, MySweetEventEventArgs e);

/// <summary>
/// TODO: Describe the purpose of MySweetEvent here
/// </summary>
public event MySweetEventEventHandler MySweetEvent;

/// <summary>
/// This is the method that is responsible for notifying
/// receivers that the event occurred
/// </summary>
protected virtual void OnMySweetEvent(MySweetEventEventArgs e)
{
if (MySweetEvent != null)
{
MySweetEvent(this, e);
}
}

#endregion //('MySweetEvent' event definition code)
}
}

Comments (3) | Post RSS RSS comment feed

Posted on 1/4/2007 3:29:49 PM by Dave Burke
Categories: CodeSmith
Tags:

Related posts

Comments (3) -

1/5/2007 5:15:09 AM Permalink

Dave,

That is simply too cool.   I upgraded to Codesmith 4 the other week but haven't had a chance to play with it much yet.  

Keep em coming

Dave

Dave Kekish |

1/5/2007 8:07:50 AM Permalink

Hey, Dave!  I saw on the .netTiers site that you were on the original .netTiers team.  That's outstanding!  And yes, lots more CodeSmith Cool to cover.  Thanks for your comments. -Dave

daveburke |

1/5/2007 11:31:07 AM Permalink

Yep,  That was me,   I fixed a few errors and added some stuff then kind of drifted away from it.  

The netTiers setup is nice, but a lot of it is serious overkill for what I do on a day to day basis.  I leverage their stuff and other stuff swiped from the Enterprise library into a nice small (and lean) package we use here at the clinic.

Dave Kekish |


Powered by BlogEngine.NET 2.0.0.36
Theme by Dave Burke