SQL Server 2016 includes R Language

SQL Server 2016 should hit RTM sometime later this year. It includes several new features. You can read about some of the new features in my previous blog posts:

https://www.webagesolutions.com/blog/archives/1482

https://www.webagesolutions.com/blog/archives/1429

 

SQL Server 2016 also includes the R language for statistical computing and graphics. Here are some more details on the topic:

http://www.theregister.co.uk/2015/10/28/sql_server_2016_with_r/

Generating Class(es) from JSON in Visual Studio

JSON is a very popular lightweight data-interchange format. Visual Studio makes it very easy to generate class / classes out of JSON. Say, we have the following JSON available:

{
  "players": {
    "player": [
      {
        "playerId": 1,
        "playerName": "Crosby",
        "gamesPlayed": 100
      },
      {
        "playerId": 2,
        "playerName": "Niedermayer",
        "gamesPlayed": 300
      }
    ]
  }
}

 

To generate C# classes out of the above JSON, copy the JSON to clipboard, in Visual Studio click Edit > Paste Special > Paste JSON as Classes.
It will generate following classes:
 
 public class Rootobject
    {
        public Players players { get; set; }
    }

    public class Players
    {
        public Player[] player { get; set; }
    }

    public class Player
    {
        public int playerId { get; set; }
        public string playerName { get; set; }
        public int gamesPlayed { get; set; }
    }
 
 
Now you can fine-tune some of the code, e.g. change Player[] to List<Player> etc.
 
This paste special option works well in Visual Studio 2015. In case if you have any older version of Visual Studio then you might want to 
add NewtonSoft JSON NuGet package to your project to make it work:
 
image

Object Oriented Programming: JavaScript vs TypeScript in Visual Studio 2015

JavaScript has been the defacto web language for a very long time. It’s used for implementing client-side validation, animation, DOM manipulation, AJAX and for several other reasons. Developers have been using JavaScript in one form or other: e.g. plain vanilla JavaScript, jQuery, AngularJS etc. OOP in JavaScript is quite different than other languages, say C++/Java/C# etc. Here’s an example of a simple class in JavaScript:

//class, constructor function
var Employee = function (employeeId, employeeName, department) {
    //properties
    this.employeeId = employeeId;
    this.employeeName = employeeName;
    this.department = department;

    //method
    this.insertEmployee = function () {
        //insertEmployee logic goes here
    };
};
 
Alternatively, the above code can be written like this:
 
function Employee(employeeId, employeeName, department) { 
  this.employeeId = employeeId; 
  this.employeeName = employeeName; 
  this.department = department; 
} 
 
Employee.prototype.addEmployee = function () { 
  //insertEmployee logic goes here 
}; 
 
 
 
Wait, what? There’s no class keyword? No data types? class is a function? Yes! everything is a function in JavaScript.
Now you might say that you have spent years learning OOP syntax in other languages so how can you use similar syntax in JavaScript.
That’s where TypeScript comes into play. It’s created by Microsoft and lets you use a more “natural” / typical syntax and generates the JavaScript code 
behind the scenes. TypeScript is gaining popularity especially since AngularJS 2.0 is heavily utilizing it.
 
If you have Visual Studio 2015 then TypeScript is available OOB. For older version, like 2013, extension can be downloaded here: 
https://visualstudiogallery.msdn.microsoft.com/b1fff87e-d68b-4266-8bba-46fad76bbf22
 
Right click a web project in Visual Studio 2015, click Add New Item and select TypeScript:
image
 
Here’s the code for Employee.ts file:
 
class Employee {
    employeeId: number;
    employeeName: string;
    department: string;

    constructor(employeeId: number, employeeName: string, department: string) {
        this.employeeId = employeeId;
        this.employeeName = employeeName;
        this.department = department;
    }

    addEmployee() {
        //insertEmployee logic goes here
    }
};

 

That’s much closer to typical OOP syntax. There’s class keyword, properties construction & methods have much easier syntax and data types are there as well.
Visual Studio generates the corresponding JavaScript file behind the scenes

image

Here’s what the auto-generated code looks like:

var Employee = (function () {
    function Employee(employeeId, employeeName, department) {
        this.employeeId = employeeId;
        this.employeeName = employeeName;
        this.department = department;
    }
    Employee.prototype.addEmployee = function () {
        //insertEmployee logic goes here
    };
    return Employee;
})();
;

Here it’s using IIFE JavaScript syntax to return Employee function which represents the class.

So, we can see TypeScript has a much easier syntax. On day to day basis we can maintain the code in a TypeScript .ts file, but we do have to reference the auto-generated JavaScript file since web browsers only understand JavaScript, not the TypeScript.