I never really used the new Code Snippet feature in Visual Studio 2005. Since I've been spending time with Castle lately I put together a few snippets after feeling a strong pattern in my fingers. These should be good in general for any DI framework. Enjoy. (If you are viewing through the web, sorry about the dark colors. I recently switched to a darker color scheme in an attempt to reduce eye strain.)
Check if a a passed in argument is null and throw an exception accordingly:
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>argn</Title>
<Shortcut>argn</Shortcut>
<Description>Perform an argument null check</Description>
<Author>Bill Pierce</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>argument</ID>
<ToolTip>Argument name</ToolTip>
<Default>Arg</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[if ($argument$ == null) throw new ArgumentNullException("$argument$");]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
Declare a string field/property that defaults to string.Empty and converts nulls to string.Empty:
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>props</Title>
<Shortcut>props</Shortcut>
<Description>Code snippet for a string property and backing field</Description>
<Author>Bill Pierce</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>property</ID>
<ToolTip>Property name</ToolTip>
<Default>MyProperty</Default>
</Literal>
<Literal>
<ID>field</ID>
<ToolTip>The variable backing this property</ToolTip>
<Default>myVar</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[private string $field$ = string.Empty;
public virtual string $property$
{
get { return $field$;}
set
{
if( value == null ) value = string.Empty;
$field$ = value;
}
}
$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
Declare a propery that is set through dependency injection:
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>propdi</Title>
<Shortcut>propdi</Shortcut>
<Description>Code snippet for property set through dependency injection</Description>
<Author>Bill Pierce</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>type</ID>
<ToolTip>Property type</ToolTip>
<Default>int</Default>
</Literal>
<Literal>
<ID>property</ID>
<ToolTip>Property name</ToolTip>
<Default>MyProperty</Default>
</Literal>
<Literal>
<ID>field</ID>
<ToolTip>The variable backing this property</ToolTip>
<Default>myVar</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[private $type$ $field$;
public virtual $type$ $property$
{
protected get
{
if ($field$ == null) throw new ArgumentNullException("$property$");
return $field$;
}
set
{
if (value == null) throw new ArgumentNullException("$property$");
$field$ = value;
}
}
$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>