ZenCoding Visual Studio plugin Overview

The ZenCoding extension for Visual Studio can be installed by itself or as part of the WebEssentials plug-in bundle, see also WebEssentials website. In ZenCoding you use a shorthand to describe a block of html, with the cursor at the end of the line hitting the TAB key will generate the full html code. Getting down to business, here is a list of the formatting characters and their meaning.

#Indicates text defines an element id
>Secified a child element
+A sibling element
.css class(es)
*nElement repeats n times
[]Explicitly specify atrribute.
{xxx}Explicit text content
$Numeric increment

Example 1 – Element Id
So as a first example, type this in to Visual Studio and with the cursor at the end of the line hit the TAB key.

div#sidePanel

And the following HTML is produced

<div id='sidePanel'></div>


That is pretty much all there is to it. For brevity the following examples show the shorthand to type followed by the HTML generated when the TAB key is pressed (when the cursor is at the end of the line).

Example 2 – Explicit Attributes

img[alt='logo image']

&amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;logo image&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;

Exampe 3 – Css Class

span.heading

&amp;amp;amp;amp;lt;span class=&amp;amp;amp;amp;quot;heading&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;

For multiple css classes the [] can be used

img[class='logo tooltip-flyout']
or specify each class using the dot notation
img.logo.tooltip-flyout

&amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; class=&amp;amp;amp;amp;quot;logo tooltip-flyout&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;

Example 4 – Repeating Elements

ul#features&amp;amp;amp;amp;gt;li*3

&amp;amp;amp;amp;lt;ul id=&amp;amp;amp;amp;quot;features&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/ul

Example 5 – Explicit Text

label{submit}

&amp;amp;amp;amp;lt;label&amp;amp;amp;amp;gt;submit&amp;amp;amp;amp;lt;/label&amp;amp;amp;amp;gt;

Example 6 – Putting It All Together

div#sidePanel&amp;amp;amp;amp;gt;span.heading+img.logo[alt='logo image']+ul#features&amp;amp;amp;amp;gt;li*3&amp;amp;amp;amp;gt;a{link $}

&amp;amp;amp;amp;lt;div id=&amp;amp;amp;amp;quot;sidePanel&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;span class=&amp;amp;amp;amp;quot;heading&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;logo image&amp;amp;amp;amp;quot; class=&amp;amp;amp;amp;quot;logo&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;ul id=&amp;amp;amp;amp;quot;features&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;
        &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;link 1&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
        &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;link 2&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
        &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;link 3&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;/ul&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;

There are a few special features built in, Generation of lorem ipsum text, Lorem Pixel Code and PlaceHold.it blank images.

Example 7 – Generating Lorem Ipsum Text
Here we specify 5 list items each containing 4 words.

ul&amp;amp;amp;amp;gt;li*5&amp;amp;amp;amp;gt;lorem4

&amp;amp;amp;amp;lt;ul&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;Amet, consectetur adipiscing elit.&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;Fusce vel sapien elit.&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;In malesuada semper mi.&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;Id sollicitudin urna fermentum.&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;Ut fusce varius nisl.&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/ul&amp;amp;amp;amp;gt;

Example 8 – Lorem Pixel Code
Lorempixel is a free image place holder service. Useful for mocking up layouts where you do not have all your own images yet. Below are a number of Zencoding examples. There are more features like greyscale images and images with text, see the lorempixel.com site for a full list

A completely random image with a default 30 by 30 pixel size.

pix

&amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;http://lorempixel.com/30/30/&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;

A random image of a specific but equal with and height.

pix-200

&amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;http://lorempixel.com/200/200/&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;

A random image with different width and height

pix-200x100

&amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;http://lorempixel.com/200/100/&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;

An image of a specific but equal with and height from a specific category. A random image index will be selected from those in the category, in this case 7, though this can be changed if another image fits your needs. There are generally 20 images per category currently though the odd category may have less than 20 images.

pix-200-animals

&amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;http://lorempixel.com/200/200/animals/7/&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;

Mixing in some of the other Zencoding shorthand. A random image with an ALT attribute and specific CSS class.

pix[alt='product image'].borderless

&amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;http://lorempixel.com/30/30/&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;product image&amp;amp;amp;amp;quot; class=&amp;amp;amp;amp;quot;borderless&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;

Example 9 – PlaceHold.it Images
Placeholder.com is another image service with similar features. The big difference with this service is the images are grey tone with the image size as text which is quite useful for when you are mocking up layouts containing a number of different image sizes.

A basic image, defaults to 30 by 30 pixels.

place

&amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;http://placehold.it/30x30/&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;

An image with a specific but equal width and height.

place-200

&amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;http://placehold.it/200x200/&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;

An image of a specific but different width and height

place-200x100

&amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;http://placehold.it/200x100/&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;

Mixing in some of the other Zencoding shorthand. A random image with an ALT attribute and specific CSS class.

place[alt='product image'].borderless

&amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;http://placehold.it/30x30/&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;product image&amp;amp;amp;amp;quot; class=&amp;amp;amp;amp;quot;borderless&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;

ZenCoding is one of those things where you either love it and tend to use it a lot or don’t see the point in it and so never use it. In the latter case I can see how taking the time to learn and use ZenCoding, which can take a few attempts when trying to create a specific formatted HTML layout, takes more time than just typing (i.e. Copy/Paste) the desired html code in the first place. On the Visual Studio Marketplace ZenCoding pages some people have mentioned how the plug-in does not work in the Community 2017 edition. All the above examples were created using VS2017 Community edition, apart from a few bugs (highlighted below) I can say it does work.

Below are some examples of bugs I have experienced when using Zencoding in MVC views in VS2017 Community edition, although they have worked with Web Forms.

ul&amp;amp;amp;amp;gt;li*3&amp;amp;amp;amp;gt;a

The above produces half a job by formatting the link
ul&amp;amp;amp;amp;gt;li*3&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;#&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;content&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;

where I was hoping for an un-ordered list of 3 list items, with each containing a link.
&amp;amp;amp;amp;lt;ul&amp;amp;amp;amp;gt;
	&amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
	&amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
	&amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/ul&amp;amp;amp;amp;gt;

Altough in MVC Razor view the following works.

ul&amp;amp;amp;amp;gt;li*3&amp;amp;amp;amp;gt;a.referenceLink

&amp;amp;amp;amp;lt;ul&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; class=&amp;amp;amp;amp;quot;referenceLink&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; class=&amp;amp;amp;amp;quot;referenceLink&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; class=&amp;amp;amp;amp;quot;referenceLink&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/ul&amp;amp;amp;amp;gt;

Trying to generate a list with explicit text fails but in a different way.

div&amp;amp;amp;amp;gt;ul&amp;amp;amp;amp;gt;li*3{abc}

&amp;amp;amp;amp;lt;div&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;ul&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/ul&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;

For me, the Zencoding plug in is a simple tool that enhances my coding time.

Searching text of all stored procedures in MSSQL

Here are a few useful SQL statements I use occasionally, so occasionally that I have to keep searching to dig them out of their “safe place”.
All these scripts have been written with MSSQL in mind, the where clause can easily be modified to change how matches are found, match exactly, match containing etc.

/*************************************************
 List all stored procedures containing given text 
*************************************************/

--method 1
SELECT DISTINCT so.name 
FROM syscomments sc 
INNER JOIN sysobjects so on sc.id=so.id WHERE sc.text LIKE '%amr%'

--method 2
SELECT o.name,o.type
FROM sys.sql_modules sm
INNER JOIN sys.objects o ON
    o.object_id = sm.object_id
WHERE sm.definition LIKE '%amr%'


/*************************************************
 List all tables with partially matching columns
*************************************************/

select so.name, sc.name from syscolumns sc 
INNER JOIN sysobjects so on sc.id=so.id WHERE sc.name like '%turnover%'


/*************************************************
 Use built in views to get schema information
*************************************************/

SELECT * FROM information_schema.tables
ORDER BY table_name

SELECT * FROM information_schema.columns
where column_name like '%amr%'
ORDER BY table_name, ordinal_position

Using EntityFramework 6 Part 2: Database Connections

This post is part of a mini series on using EntityFramework (EF). If you do not have EF installed go to Part 1: Introduction and Setup to see what (basic) steps we took to get EF installed.
In this part we are going to use Code-First methods to start model building, then eventually connecting to the database . This is not the only way and you do not have to do things in exactly the same order as I have but if you follow along you should have a working version at the end.

Database
Firstly, create a database at your preferred location. Once you have a database add the connection string to the web.config file. I added a database to the App_Data folder so this is what the connectionsString definition look like in my web.config file.

  &lt;connectionStrings&gt;
    &lt;add name=&quot;DBConn&quot; connectionString=&quot;Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\DB.mdf;Integrated Security=True&quot;
         providerName=&quot;System.Data.SqlClient&quot; /&gt;
  &lt;/connectionStrings&gt;

Next, add a class which inherits from DBContext. An instance of DBContext, LibraryDBContext in this example, is used to perform CRUD operations against a database. To read up on DBContext see these articles
http://www.entityframeworktutorial.net/EntityFramework4.3/dbcontext-vs-objectcontext.aspx

namespace WebApplication2.Models
{
    public class LibraryDBContext:DbContext
    {
        public LibraryDBContext() : base(&quot;DBConn&quot;)
        { }

        public DbSet&lt;Book&gt;Books{ get; set; }
    }
}

This gives sufficient Entity Framework code to enable our application to access a database.

The Model
Now we will start the model by creating a Book entity. Create a Book class in the Models folder of your application.

namespace WebApplication2.Models
{
    public class Book
    {
        public int BookId { get; set; }
        public string Author { get; set; }
        public string Title { get; set; }
    }
}

Following Code-First conventions, because we have defined an property called BookId (class name ‘Book’ plus ‘Id’) Entity Framework will nominate this property as an identity column when the database tables are generated. If we had a property simply named Id, Entity Framework would have nominated that as the identity and created an identity column called Id.

Displaying Book Info
Add a Controller to the application, I have called it BooksController. The Index action retrieves all the books from what will be the Books table.

using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using WebApplication2.Models;

namespace WebApplication2.Controllers
{
    public class BooksController : Controller
    {
          public ActionResult Index()
        {
            var books = new List&lt;Book&gt;();
            using (var db = new LibraryDBContext())
            {
                books = db.Books.ToList();
            }
            return View(books);
        }
    }
}

The last code related change is to create a View for the Index Action to display all the books.

@model IEnumerable&lt;WebApplication2.Models.Book&gt;
    @Html.ActionLink(&quot;Create New&quot;, &quot;Create&quot;)
&lt;table class=&quot;table&quot;&gt;
&lt;tr&gt;
&lt;th&gt;
            @Html.DisplayNameFor(model =&gt; model.Author)&lt;/th&gt;
&lt;th&gt;
            @Html.DisplayNameFor(model =&gt; model.Title)&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
@foreach (var item in Model) {
&lt;tr&gt;
&lt;td&gt;
            @Html.DisplayFor(modelItem =&gt; item.Author)&lt;/td&gt;
&lt;td&gt;
            @Html.DisplayFor(modelItem =&gt; item.Title)&lt;/td&gt;
&lt;td&gt;
            @Html.ActionLink(&quot;Edit&quot;, &quot;Edit&quot;, new { id=item.BookId }) |
            @Html.ActionLink(&quot;Details&quot;, &quot;Details&quot;, new { id=item.BookId }) |
            @Html.ActionLink(&quot;Delete&quot;, &quot;Delete&quot;, new { id=item.BookId })&lt;/td&gt;
&lt;/tr&gt;
}&lt;/table&gt;

Running the Application
The application should build at this point. Currently we have a database that contains no tables. If you run the application now The database/tables will only be created/modified on a read/write action. So run the application and navigate to your controller (e.g. \books). Because the Index action of the Books controller contains code that retrieves data via the inherited DBContext class, and because Entity Framework knows there are models with no corresponding database table, the tables will be created with columns types that match the model properties.

Using EntityFramework 6 Part 1: Introduction and Setup

In this mini series we will be looking at how to implement EntityFramework 6 (from now on referred to as EF) into an MVC application, primarily using Code First methods. In this first part we are going to set up our project.  If you already have a project ready skip to the Setup EF section. If you already have EF version 6 installed feel free to go to part 2: Putting Database Connections into Context.

New MVC Project

I was going to lay out the steps required to create a new project. However, I assume you are able to complete that without step-by-step instructions. Depending on which options you select when creating a new project will determine whether you need to install EF. If you selected an internet application with individual user accounts then EF will have been installed during project creation with a DataContext used for user authentication.

Setup EntityFramework
There are two primary ways to install a NuGet package. By using PowerShell commands in the Package Manager Console or by using a visual interface.
To use the Package Manager Console go to Tools > Nuget Package Manager > Package Manager Console. At the NuGet command line enter this command to install EF

Install-Package EntityFramework -projectname DemoApplication

The -projectname switch forces EF to be installed in a specific project. If you have only one project in your solution you can omit the parameter and use

Install-Package EntityFramework

Either way you should see a message confirming EF has been successfully installed.

To use the visual interface go to Tools > NuGet Package Manager > Manage NuGet Packages for Solution… to open the interface.  On the left select Online and in the search box on the right enter entity framework.

nuget-entityframework6

I already have EF installed which is why you see a green circle with a tick.  If it is not installed then you will see an install button.
Once you have EF installed you are ready to continue.
 

Changing JSON properties to camel case notation

A quick note on how to change the JSON property names to camel notation. This is achieved by adding the following lines to the WebApiConfig class.

var settings = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
settings.Formatting = Formatting.Indented;

This is how it would look in the class.

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Web.Http;

namespace Carrera
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            var settings = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;
            settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            settings.Formatting = Formatting.Indented;

            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: &quot;DefaultApi&quot;,
                routeTemplate: &quot;api/{controller}/{id}&quot;,
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}