Sunday, April 24, 2011

Creating a virtual property code snippet

Hi,

For those of you who use NHibernate, you must know by now that NHibernate forces us to declare our entity’s methods and properties as virtual since it uses proxies to implement lazy load of relations.

I created a small code snippet called vprop – same as prop only virtual.

It looks likes this:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>vprop</Title>
            <Shortcut>vprop</Shortcut>
            <Description>Code snippet for an automatically virtual implemented property
Language Version: C# 3.0 or higher</Description>
            <Author>matang</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>
            </Declarations>
            <Code Language="csharp"><![CDATA[public virtual $type$ $property$ { get; set; }$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Just make sure to save this xml to:


…\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets\vprop.snippet


And typing “vprop” followed by two Tabs will generate a virtual property.


Enjoy!

No comments:

Post a Comment