Spread the love

One of the great things about Visual Studio Code is the ability to extended it in pretty much any way that you wish by creating extensions and snippets. In this blog post, we will take a look at how you can create your own snippets for Visual Studio Code, which will force you to consider some of the performance patterns that are available to you. We are going to cover CompressionType on tables https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/properties/devenv-compressiontype-property and DataAccessIntent on reports, API Pages and Queries https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/properties/devenv-dataaccessintent-property now I am not going to go too much in dept, but if you are interested in the topics, I can highly recommend watching Jens Møller-Pedersen and Kennie Nybo Pontoppidan video on Coding for performance which was part of Microsoft Virtual Event https://vshow.on24.com/vshow/BCVE/exhibits/Developer 

CompressionType: 

Setting compression type on your tables will tell the SQL server how it should be stored, it is a good idea to always add this property to your tables, thereby telling everyone else that might read your code that you have taken an active choice even if you just set it to None. The choices that you have available are None, Page, Row, and Unspecified. 

DataAccessIntent: 

The Data Access Intent tells your client if it should read your data from a read-only replica of your database or if it should read from your “real” database, the idea behind the data access intent is that you do not have to overload your production database with a lot of reading transactions, and just like the Compression Type, it is a good idea to always add the property to your API Pages, Queries and reports. The current values you can choose from are ReadOnly and ReadWrite. 

Well, let us create some snippets to help us implement these properties. In Visual Studio code bring up your command pallet by pressing CTRL + SHIFT + P or F1 and type in Preferences: Configure User Snippets  

and choose New Global Snippets File.

And give it a name, this will generate a new code-snippets file for you. Now add the following to your files: 

{
	"Create a performance ready table": {
	 	"scope": "al",
	 	"prefix": "ttableperformnace",
	 	"body": [
	 		"table id Prefix_MyTable",
			 "{",
				 "  DataClassification = ToBeClassified;",
				 "  CompressionType = None;",
				 "  Caption = '';",
				 "",
				 "  fields",
				 "  {",
				 "    field(1;MyField; Integer)",
				 "      {",
				 "         DataClassification = ToBeClassified;",					 
			     "       }",
				 "  }",
				 "",
				 "  keys",
				 "  {",
				 "    key(PK; MyField)",
				 "     {",
				 "       Clustered = true;",
				 "     }",
				 "  }",
			"}"
	 	],
	 	"description": "Create a performance ready table"
	},

	"Create a performance report": {
		"scope": "al",
		"prefix": "reportperformnace",
		"body": [
			"report Id Prefix_MyReport",
			"{",
			"  UsageCategory = Administration;",
			"  ApplicationArea = All;",
			"  DataAccessIntent = ReadWrite;",
			"",
			"  dataset",
			"  {",
			"    dataitem(DataItemName; SourceTableName)",
			"    {",
			"     column(ColumnName; SourceFieldName)",
			"      {",
			"",
			"      }",
			"     }",
			"  }",
			"",
			"requestpage",
			"  {",
			"    layout",
			"    {",
			"      area(Content)",
			"      {",
			"        group(GroupName)",
			"        {",
			"          field(Name; SourceExpression)",
			"          {",
			"            ApplicationArea = All;",
			"",
			"          }",
			"        }",
			"       }",
			"     }",
			"",	
			"  actions",
			"  {",
			"    area(processing)",
			"     {",
			"       action(ActionName)",
			"       {",
			"         ApplicationArea = All;",
			"",
			"       }",
			"     }",
			"   }",
			"  }",
			"",
			"var",
			"  myInt: Integer;",
			"}"
		],
		"description": "Create a performance ready report"
   }
}

So to break this down the first line is the name of your snippet, the scope will tell you in which type of files your snippet should be available, the prefix is the text that the user will enter to create your snippet, the body is well the body of your snippet, and the description is what the user will see as a tooltip.  

Once you have added the snippets that you wish, you can go to any .al file a type your prefix and it should appear. 

And that is all there is to creating your own snippets in Visual Studio Code, it is also very useful if you are for example supporting your own ISV product and you know all your objects should be prefixed with something, if you add this to your snippets, you will never forget to prefix your objects ever again. Also, if you have some very cool way of doing web service Integrations, you could create a snippet of this, and next time you need it, it is there out of the box. And once you have built op an awesome library of snippets you can bundle them all and create an extension, either on the marketplace or just for your own organization. That was all for now until next time stay safe 😷

Leave a Reply