Sunday, March 20, 2016

Basics of TypeScript

I was just learning type script and i realized to publish it on internet so any one can easily get it in lay man language:

What is TypeScript ?

As described on official TypeScript website “TypeScript is a typed superset of JavaScript that compiles to plain JavaScript”. TypeScript is not a replacement of JavaScript, it does not add any new features in JavaScript. TypeScript provides developers with features like type safety, compile time type checking, object oriented constructs on top of JavaScript, basically lets developers think in object oriented terms when writing JavaScript. The most amazing thing about TypeScript is, it compiles in JavaScript so we don’t have to support any new VM to run TypeScript which then can be referenced in a web page, used in server side like in NodeJS.

TypeScript is an open-source developed by Microsoft, but is in no way ties to Microsoft platform and can be used anywhere in any environment where there is a need to write JavaScript. Though Microsoft does provide a great support in Visual Studio for TypeScript and we will be using Visual Studio for our examples but we can use TypeScript compiler as well from command prompt to compile TypeScript in JavaScript (we will see a brief example of that as well).

How to install TypeScript ?

Visual Studio has inbuilt support for type script from 2013 and of course any one can install it on 2012 as well. For creating TypeScript Project in Visual Studio 2015 follow below steps:-

1. Click on file-> New->Project-> Select HTML Application With TypeScript From TypeScript Tab.
2. After naming it you will have TypeScript project open which will have app.ts default type script written by Visual Studio and index.html file.


TypeScript file template can be created in similar way as other files are, by selecting “Add New Item” and then selecting a TypeScript file.
All TypeScript files have *.ts extension. Visual Studio plugin for TypeScript automatically creates and updates a corresponding JavaScript file with same name whenever we modify the TypeScript file and save. This generated JavaScript file can then be used as a normal file and included in any web page.

Features Of TypeScript:

Developers can write code in TypeScript which has object oriented concepts and compile time type checking on top of JavaScript which helps writing more structured, maintainable and robust code. TypeScript introduces few of the standard object oriented terms like Classes, Interfaces, Module and Variables which in end gets converted into various different forms of JavaScript.

1.  Module : Module is like a namespace in .Net world and can contain classes and interfaces.


Here Class Comedy is part of Movie module like namespaces in .Net.

2.Interfaces : Developers can create interfaces in type script and implements those in particular class like we are doing in .Net.Compiler will give errors here as well if we are not implementing any member of interface and that is the best feature of type script other wise it is same as java script.


Above is the simple example of interface and its implementation in type script. Interface IStudent has two members Student's yearOfBirth and second one is method which gives us age of student based on yearOfBirth. Here compiler will make sure that methods we are calling from classes has appropriate arguments on all those as we does in .net. When we compile type script interfaces those will go - those will be omitted in actual java script.

3. Classes : Classes are again very similar to .Net/Java world. Classes contains variables, properties and methods which form one logical entity. TypeScript also allows to set scope of the variable and functions with keyword like “private” and “public” though that scope does not have any effect on the JavaScript generated.

In above example we have class Student which has members like firstName ( private) and lastName (public) if we do not specify any access modifier by default it is public in type script. also Constructor can be created using Key word constructor().  When compiler converts type script to java script there is no meaning of Public and private keywords.

4.Functions : Functions are methods which can have logic written in side. in above example FullName is a tyoe script function which alerts fullName of Student.

5. Variables : Variables are the fields defined inside a class or a function. TypeScript allows us to define a variable using keyword “var” and assign a data type to it. Once a data type is assigned any further usage of the variable has to be with same data type else TypeScript will generate error on design and compile time.


Note:
For each of the features in TypeScript we have been relating to corresponding features in OOPs language such as C# and Java but, we need to keep in mind that TypeScript is not a OOPs language. In fact, TypeScript is just a tool on top of JavaScript which provides us with more robust code structure, type safety. TypeScript enhances the productivity of developers writing JavaScript code and it in itself does not provide any specific functionalities or features like libraries such as Jquery.
Most of the features which we use in TypeScript gets removed from the compiled JavaScript file and we are left with nothing more than a pure JavaScript code.

Saturday, February 13, 2016

What is Reflection in C# ?


Reflection is the ability of a managed code to read its own metadata for the purpose of finding assemblies, modules and type information at runtime. In other words, reflection provides objects that encapsulate assemblies, modules and types. A program reflects on itself by extracting metadata from its assembly and using that metadata either to inform the user or to modify its own behavior. Reflection is similar to C++ RTTI (Runtime Type Information), but much broader in scope and capability. By using Reflection in C#, one is able to find out details of an object, method, and create objects and invoke methods at runtime. The System.Reflectionnamespace contains classes and interfaces that provide a managed view of loaded types, methods, and fields, with the ability to dynamically create and invoke types. When writing a C# code that uses reflection, the coder can use the typeof operator to get the object's type or use the GetType() method to get the type of the current instance. Here are sample codes that demonstrate the use of reflection:
Example:

using System; 
using System.Reflection;

 public class MyClass 
  public virtual int Add(int numb1,int numb2) 
   { 
          int result = numb1 + numb2; return result;
    } 

 class MyMainClass 
  public static int Main() 
 { 
     Console.WriteLine ("\nReflection.MethodInfo"); 

    // Create MyClass object 
    MyClass myClassObj = new MyClass();
 
   // Get the Type information. 
   Type myTypeObj = myClassObj.GetType("MyClass");  // we can check which type of class is this

 // Get Method Information.
   MethodInfo myMethodInfo = myTypeObj.GetMethod("Add"); 
 
 object[] mParam = new object[] {5, 10}; 
// Get and display the Invoke method. 

 Console.Write("\nFirst method - " + myTypeObj.FullName + " returns " + myMethodInfo.Invoke(myClassObj, mParam) + "\n"); return 0; 
 } 
}

We can do many things with reflection. Like we can know if property exists in class, class is abstract o not ? , which method have how many parameters ?, we can decide runtime which method to invoke.

For more visit MSDN : https://msdn.microsoft.com/en-us/library/ms173183.aspx

Tuesday, February 9, 2016

What are Strongly Typed Views in MVC



A Strongly Typed view means it has a ViewModel associated to it that the controller is passing to it and all the elements in that View can use those ViewModel properties You can have strongly typed partials as well. Meaning that piece of Html needs specific data so you type it to a certain ViewModel.

 Here is an example of a Strongly Typed View @model SomeViewModel ...// all the html comes after A view that is strongly typed have a @model SomeViewModel line

 Here's one that renders a strongly typed View 

public ActionResult Index()
 {
   var model = new SomeViewModel();
    return View(model); 

And the view makes use of that ViewModel by having the @model SomeViewModel at the top of the file. So now that the view has a ViewModel I can display elements that are bound to the ViewModel like

@Model.Name

@Model.Location

OR for Controls------ @Html.TextBoxFor(m => m.Name) @Html.TextBoxFor(m => m.Location)

Wednesday, December 30, 2015

Started Personal Git Repository

I just pushed whatever i am learning for MVC5 on my personal Git repository-https://github.com/Nirav-Bhadradiya/learn.git
Below is the repository - anyone can point that and look at the stuff. I will be pushing more in next coming days.
Helpful for beginners.!!!! 

Setup Form Authentication in MVC 5

I was just trying Form authentication in MVC5 and i faced some problems while setting it up. i want to share it with you guys so you will not spend your precious time to find out solution.

Step 1: Add below tag in your project's web.config. Remember that you will see one web.config in View folder as well but that is for only View purpose.

<!--  attribute loginUrl is case sensitive please don't change it -->
    <authentication mode="Forms">
      <forms loginUrl="~/Authentication/Login"></forms>
    </authentication>

Here Login URL will be the View where you wanted to redirect your users when they tried accessing unauthorized Action methods.

Step 2; Add below attribute on your methods which you wanted to make secure. in this case i tried with my main action method of one of my Employee.cs class. You can make your whole controller secured with putting Authorize attribute for it.

[Authorize]
        public ActionResult Index()
        {
            var employeeListViewModel = new EmployeeListViewModel {UserName = User.Identity.Name};
            .................................................................
............................................................................
        }

Step 3: Now Modify your Login() Action to set cookies of loggedin user  FormsAuthentication.SetAuthCookie(u.UserName, false);

Step 4: Browse your Action method and it will take you to Login View.



Saturday, October 10, 2015

Fix The ‘Setup Couldn’t Start Properly’ Error When Downloading Windows 10

When you are trying to Upgrade windows 10 and in beetween if something gone wrong and it stopped then if next time you will try to start it it will show below error :

"Setup couldn't start properly.Please reboot your PC and try running windows 10 setup again."

media-creation-tool-error

To solve this issue follow below steps:

Step 1: Browse your drive where system is installed. usually (C drive)

Step 2: Make sure you can see hidden folders and files.

Step 3: Delete the folders named; $Windows.~BT and $Windows.~WS

Step 4: If when performing step 3 it gives any error that you can not delete this folder because it is used by some process then, download Unlocker from here : http://www.filehippo.com/download_unlocker/ 

Step 5: Run Windows Medic Creation Tool and it will again start from scratch.

Saturday, September 26, 2015

Pivot/Unpivot Columns in SQL Server

Today i just used Unpivot Functionality of SQL Server and i want to share with you guys.

What is Pivot and Unpivot ?

You can use the PIVOT and UNPIVOT relational operators to change a table-valued expression into another table. PIVOT rotates a table-valued expression by turning the unique values from one column in the expression into multiple columns in the output, and performs aggregations where they are required on any remaining column values that are wanted in the final output. UNPIVOT performs the opposite operation to PIVOT by rotating columns of a table-valued expression into column values.


Here is the simple fiddler for the same : http://sqlfiddle.com/#!3/e32bd/1