Monday, May 28, 2018

What is AngularJS

AngularJS is an open source JavaScript MVC framework for web application or web sites.


Features of Angular Js
  1. It is  a powerful JavaScript based development framework to create RICH Internet Application (RIA).
  2. It provides developers options to write client side application (using JavaScript) in a clean MVC (Model View Controller) way.
  3. Application written in AngularJS is cross-browser ,It automatically handles JavaScript code suitable for each browser.
  4. It is open source, completely free.
  5. It is licensed under the Apache License version 2.0.
Miško Hevery, a Google employee, started to work with AngularJS in 2009.

and now officially supported by Google.

AngularJS Features

  1. MVC – The framework is built on the famous concept of MVC (Model-View-Controller). This is a design pattern used in all modern day web applications. This pattern is based on splitting the business logic layer, the data layer, and presentation layer into separate sections. The division into different sections is done so that each one could be managed more easily.
  2. Data Model Binding – You don't need to write special code to bind data to the HTML controls. This can be done by Angular by just adding a few snippets of code.
  3. Very less Code – When carrying out DOM manipulation a lot of JavaScript was required to be written to design any application.  But with angular you will be writing very less amount of code.
4 UnitTest : The designers at Google not only developed Angular but also developed a testing framework called "Karma" which helps in designing unit tests for AngularJS applications.

5 It provides routing features.


6 The AngularJS framework uses Plain Old JavaScript Objects(POJO), it doesn’t need the getter or setter functions.


Two way Data Binding:





The first step we need to do is add the javascript function as shown below:


<script src="~/Script/angular.min.js"></script> 


AngularJS Architecture



Setup AngularJS 

The below things required to set up angular js environment.

  1. AngularJS Library
  2. Editor/IDE
  3. Browser
  4. Web server

Angular JS Library:

To download angular js we need to go to the below URL
https://angularjs.org/

Click on Download Angular.js below screen gets displayed.



You can also use the below Angular Js Libarary



Editor: Visual Studio

The following editors are also recommended:

Web server:

We can use any web server such as IIS, apache etc., locally for development purpose.

Browser:

You can install any browser of your choice as AngularJS supports cross-browser compatibility. However, it is recommended to use Google Chrome while developing an application.

Setup AngularJS project in Visual Studio:

Open Visual Studio -File->New Project.




Give any name and the Location where you want to Save. Click on Ok Button



In the New ASP.NET Project dialog box, select Empty template and then click OK.



Now, we will install Angular JS Library Using Nuget Packages.







Now, this will add the script folder in your projects as shown below:



AngularJS Directives


ng-app: 

This is very important in Angular Application, which is used to indicate starting of an Angular Application to AngularJS HTML compiler ($compile), like a “Main()” function in any compile any of time language like C#, Java or C++ etc. 

If we do not use this directive first and directly try to write other directives, it gives an error.

This framework will first check for ng-app directive in a HTML document after the entire document is loaded and if ng-app is found, it bootstraps itself and compiles the HTML template.

Typically ng-app directives should be placed at the root of an HTML document e.g. <html> or <body> tag, so that it can control the entire DOM hierarchy. However, you can place it in any DOM element.


The AngularJS framework will only process the DOM elements and its child elements where the ng-app directive is applied.

Example:


Basic Example to Add two numbers


In the below example everything under the div tag is managed under angular.


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="angular.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div ng-app>
    10+20={{10+20}}
    </div>
    </form>
</body>

</html>

In the below example. everything under the body tag are managed by angular.

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
    <title></title>
    <script src="angular.min.js"></script>
</head>
<body ng-app>
    <form id="form1" runat="server">
    <div>
    10+20={{10+20}}
    </div>
        <div>
             30 +20= {{30+20 }}
        </div>
    </form>
</body>

</html>


Output:



Another Basic Example: 

To check whether it is true or false





Using Javascript Object




Using Arrays



Angular Modules and Controllers:


What  is Module:


It is container for different parts of your application , i.e controller, services, directories, filters.


Controllers always belong to a module.


It is like a main method in other types of application.


How to create a Module:


using the AngularJS function angular.module


Example :



<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>
    <script src="angular.min.js"></script>
</head>

<body>

    <form id="form1" runat="server">
        <div ng-app="myApp">...</div>
        
         <script>
            var app = angular.module("myApp", []);
        </script>

    </form>

</body>
</html>

Note: In the above code, i am passing an empty Array. Module can  be depended on other Module. So in the above i am passing as Empty.



What is Controller

It is java script function.And the job of it is to build a model for the view to display.


How to create a controller


ng-controller: It is used to define a controller.


Example :


app.controller("myCtrlName"function($scope) {
    $scope.firstName "Rajakonda";
    $scope.lastName "Uday Kumar";
});


or , we can write the above code  also as


<form id="form1" runat="server">


        <div ng-app="myApp">...</div>

        <script>
              var myController = function ($scope) {
              $scope.firstName "Rajakonda";
              $scope.lastName "Uday Kumar";            }   
        </script>
    
</form>

What is $scope:



The scope is the binding part between the HTML (view) and the JavaScript (controller).

The scope is an object with the available properties and methods.

The scope is available for both the view and the controller.

A context where the model is stored so that controllers, directives and expressions can access it


How we will use this Scope:

When we create a controller, there we will be passing $scope as an argument.

Example:

<head runat="server">

    <title></title>
    <script src="angular.min.js"></script>
</head>

<body>

    <form id="form1" runat="server">

        <div ng-app="myApp" ng-controller="myCtrl">

            <h1>{{firstName}}</h1>
            <h1>{{lastName}}</h1>
        </div>

        <script>

            var app = angular.module('myApp', []);
            app.controller('myCtrl', function ($scope) {
                $scope.firstName = "Rajakonda";
                $scope.lastName = "Uday Kumar";;
            });
        </script>

    </form>

</body>
</html>


Output:

Example:

Now, i am going to create a new javascript File for this example:


Right click on the Project and Select Add-->Add New Item->Select Javascript File and write any Name and Click on Add as shown below:





Now, the newly created java script file will contains the custom javascript code


First add the reference of Angular js in the newly created javascript file




Default.aspx:


<html xmlns="http://www.w3.org/1999/xhtml" ng-app="myModule">


<head runat="server" >

    <title></title>
    <script src="angular.min.js"></script>
    <script src="MyJavascript.js"></script>
</head>

<body>

    <form id="form1" runat="server">
       <div ng-controller="myController">
           {{ Message }}
       </div>
    </form>
</body>

</html>





Output:



Basic example with Angular js and Jquery 


AngularJS Example:


<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js">
    </script>
</head>
<body ng-app>
    Enter Your Name: <input type="text" ng-model="name" /> <br />
    Hello <label ng-bind="name"></label>
</body>
</html>

output:





Using JQuery Example We  need to write more lines of code.


<html>

<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js">
    </script>
</head>
<body>
    Enter Your Name: <input type="text" id="txtName" /> <br />
    Hello <label id="lblName"></label>

    <script>

        $(document).ready( function () {
            $('#txtName').keyup(function () {
                $('#lblName').text($('#txtName').val());
            });
        });
    </script>
</body>

</html>


Output:


Multiplication of Two Numbers:



<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Sample Example</title>
    <script src="scripts/angular.js"></script>
</head>
<body ng-app >
    <h1>Multiplication of Two Number</h1>

    Enter Numbers to Multiply: 
    <input type="text" ng-model="Num1" /> x <input type="text" ng-model="Num2" /> 
    = <span>{{Num1 * Num2}}</span>  
</body>
</html>

Output:




Expression:

It is javaScript code which is usually wrapped inside double curly braces such as {{ expression }}. In the above example, {{ Num1 * Num2}} will simply display the product of Num1 and Num2.

AngularJS Scopes:


The Scope is an object that is specified as a binding part between the HTML (view) and the JavaScript (controller). It plays a role of joining controller with the views. It is available for both the view and the controller

Overall Angular Js is good for

Supports Single Page Application.
It is very easy in Unit test.
No need to learn scripting language, as this is javascript code only
Open source JavaScript MVC framework.
Supported by Google

Supports separation of concerns by using MVC design pattern
Built-in attributes (directives) makes HTML dynamic.


Sunday, May 27, 2018

@@IDENTITY , SCOPE_IDENTITY():

SELECT @@IDENTITY: returns the last identity value generated for any table in the current session, across all scopes(i.e. global scope).

  1. @@IDENTITY returns the last identity value generated for any table in the current session, across all scopes.
  2. After an INSERT, SELECT INTO, or bulk copy statement completes, @@IDENTITY contains the last identity value generated by the statement.
  3. If the statement did not affect any tables with identity columns, @@IDENTITY returns NULL.
  4. If multiple rows are inserted, generating multiple identity values, @@IDENTITY returns the last identity value generated.
  5. The @@IDENTITY value does not revert to a previous setting if the INSERT or SELECT INTO statement or bulk copy fails, or if the transaction is rolled back.
Note:

It will return last or newly inserted record id of any table in current session but it’s not limited to current scope. In current session if any trigger or functions inserted record in any table that it will return that latest inserted record id regardless of table. We need to use this property whenever we don’t have any other functions or triggers that run automatically.


SELECT IDENT_CURRENT : returns the last identity value generated for any table in the current session and the current scope(i.e. local scope).


SELECT SCOPE_IDENTITY(): returns the last identity value generated for a specific table in any session and any scope(i.e. global scope).


Example

Now i am going to create a two new tables


Creation of two tables


CREATE TABLE Table1(id int IDENTITY)



CREATE TABLE Table2(id int IDENTITY(100,1))

Create a trigger on table1 as below:


CREATE TRIGGER TG_Table1 ON Table1 FOR 
INSERT

AS
BEGIN       

        INSERT table2 DEFAULT VALUES
END



 Run the following SQL statements and observe the output


INSERT empTable1 DEFAULT VALUES


SELECT @@IDENTITY      -- It will consider identity value changed by trigger as trigger is another scope.


SELECT SCOPE_IDENTITY() -- It will NOT consider identity value changed by trigger as trigger is another scope.







Scope_Identity()

SCOPE_IDENTITY returns the last IDENTITY value inserted into an IDENTITY column in the same scope.

A scope is a module; a Stored Procedure, trigger, function, or batch.

The SCOPE_IDENTITY() function will return the NULL value if the function is invoked before any insert statements into an identity column occur in the scope.


Note:

This property will return last or newly inserted record id of table in current session or connection and it’s limited to current scope that means it will return id of newly inserted record in current session / connection stored procedure or query executed by you in current scope even we have any other functions or triggers that run automatically. Its better we can go with property whenever we need to get last or newly inserted record id in table.



IDENT_CURRENT is:
  1. IDENT_CURRENT returns the last identity value generated for a specific table in any session and any scope.
  2. IDENT_CURRENT is not limited by scope and session; it is limited to a specified table.

SELECT IDENT_CURRENT('empTable2')



When to Use Interface in Real Projects

It is also user-defined type like a class which only contains abstract members in it and these abstract members should be given implementation under a child class of an interface. 

Note: A List, Dictionary, or Hash Table  are derived from an Interface, IEnumerable.


C#, multiple inheritance is not supported. When there is a situation like multiple inheritance, use Interface.





The solution of the multiple inheritance can be provided by Interface. So, we can do this example using Interface as follows. In this way, the diamond problem of Inheritance is solved.



Now, we will learn  where to use Interface. 


Now let us do this example with Classes first so that you can understand where exactly interface comes into picture.



public class Bike
    {
        public string Wheel()
        {
            return "2 wheeler";
        }
        public string Brakes()
        {
            return "Disc brakes";
        }
    }
    public class Unicorn : Bike
    {
        static void Main(string[] args)
        {
            Unicorn Toy = new Unicorn();
            Console.WriteLine(Toy.Wheel());
            Console.WriteLine(Toy.Brakes());
            Console.ReadLine();
        }

    }

Now i have one more class that is Shine Class, which will also inherit the Class Bike

public class Bike
    {
        public string Wheel()
        {
            return "2 wheeler";
        }
        public string Brakes()
        {
            return "Disc brakes";
        }
    }
    public class Shine : Bike
    {
        static void Main(string[] args)
        {
            Shine objShine = new Shine();
            Console.WriteLine(objShine.Wheel());
            Console.WriteLine(objShine.Brakes());
            Console.ReadLine();
        }
    }

Here  in the Shine Class i want to add a new method , So how we will Do it ?

How can we implement this, and in which way we can implement


CASE 1 - By Creating simple class


In that simple class, i am going to add a New Method  so that Shine Class can use it



 class NewFeatures

    {
        public void NoDiscBrakes()
        {
            Console.WriteLine("No Disc Brakes");
        }

    }

Now, inherit the Shine class from NewFeatures. , it was previously inherited from Bike class and for now, again 





It is giving me the Error, if i go with Classes


Case 2: Will create a Abstract Class and will try






CASE 3 - Direct creating a new method in the Shine Class


But this is not correct, because today i have one method, but they may be situation where you need 100 of methods to be changed. So it wont work



CASE 4 - By defining Interface

public class Bike
    {
        public string Wheel()
        {
            return "2 wheeler";
        }
        public string Brakes()
        {
            return "Disc brakes";
        }
    }
    public interface INewFeatures
    {
        void NoDiscBrakes();
    }
    public class Shine : Bike, INewFeatures
    {
        public  void NoDiscBrakes()
        {
            Console.WriteLine("No Disc Brakes");

        }
        static void Main(string[] args)
        {
            Shine objShine = new Shine();
            Console.WriteLine(objShine.Wheel());
            Console.WriteLine(objShine.Brakes());
            objShine.NoDiscBrakes();
            Console.ReadLine();
        }

    }








Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...