November Happy Hour will be moved to Thursday December 5th.

Henrik Fransas
Nov 13, 2015
  4585
(4 votes)

Create a prop-snippet to support property with virtual

When I am developing a site and creating a pagetype or block type I use the shortcut for creating a property inside Visual Studio a lot. The problem with that one is that is does not include the virtual part so I often forget that and without setting a property to virtual EPiServer will not use it.

These shortcuts are called snippets and are realy easy to create you own of. If you have Visual studio 2015 and using csharp you can find a lot of the snippets here:

C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC#\Snippets\1033\Visual C#

To create your own epi-version of the prop shortcut just copy the one that is called prop.snippet and rename it to epiprop. Then open it and make shore it looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>epiprop</Title>
			<Shortcut>epiprop</Shortcut>
			<Description>Code snippet for an automatically implemented property
Language Version: C# 3.0 or higher</Description>
			<Author>Microsoft Corporation</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>order</ID>
					<ToolTip>Sort Order</ToolTip>
					<Default>10</Default>
				</Literal>
				<Literal>
					<ID>name</ID>
					<ToolTip>Property Name</ToolTip>
					<Default>Name</Default>
				</Literal>
				<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[[Display(
			Name = "$name$"
			GroupName = SystemTabNames.Content,
			Order = $order$)]
			public virtual $type$ $property$ { get; set; }$end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

This will give you a shortcut that if you write (after restarting Visual Studio, this is important) epiprop and press tab will look like this:

        [Display(
            Name = "Name"
            GroupName = SystemTabNames.Content,
            Order = 10)]
        public virtual int MyProperty { get; set; }

I hope this will help all others that aslo forget virtual sometimes

Nov 13, 2015

Comments

valdis
valdis Nov 13, 2015 07:06 AM

there should be a Roslyn analyzer that checks this during compilation time :)

Nov 13, 2015 08:07 AM

Nice! I've internally commented on this many times, but never bothered to look how to change/add such a shortcut.

Per Magne Skuseth
Per Magne Skuseth Nov 13, 2015 08:54 AM

Tip: Resharper has a handy features called Template Explorer. This makes it really easy to add and edit snippets and code templates.
Resharper -> Tools -> Templates Explorer.

Henrik Fransas
Henrik Fransas Nov 14, 2015 05:25 AM

Thanks.

True Per Magne, R# has a lot of good stuff :)

Petra Liljecrantz
Petra Liljecrantz Nov 20, 2015 08:31 AM

Good stuff! Thanks for sharing! :)

Please login to comment.
Latest blogs
Adding Geolocation Personalisation to Optimizely CMS with Cloudflare

Enhance your Optimizely CMS personalisation by integrating Cloudflare's geolocation headers. Learn how my Cloudflare Geo-location Criteria package...

Andy Blyth | Nov 26, 2024 | Syndicated blog

Optimizely SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog