Sunday, September 9, 2018

Angular - Components

Everything in angular is a component!

  • Components are the basic building blocks in Angular. 
  • A component in Angular allows us to create a reusable UI widget.
  • A component can be used inside another component - Nested Components
A component in Angular is a class with a view template and a decorator. In simple terms we can say a component in angular is composed of
o   Template
§  Defines the user interface.
§  Contains html, directive and data bindings)
o   Class
§  Contains the code for the view template.
§  Like any object oriented programming language like C# or Java, a class in Angular can contain properties and methods.
§  Properties contain data for the view template.
§  Methods contain logic for the view template

o   Decorator
§  Adds meta data to the class, for making it an Angular Component
§  Applying decorator to a class is similar to applying attributes to a class in C#.

Example: An Angular application always need to have a root component called AppComponent
In below example:



  • A class AppComponent is defined with export keyword to make that usable in other part of the application.  
  • This class has only one property called name and a value ”Angular 2” is stored in it.
  • To add the component decorator (provided by Angular) we need to use import keyword (which is similar to using keyword in C#) in below example we are importing “Component” from “angular/core”.
  • Now Apply “Component” Decorator to the class (right now we are just using two properties from the Component Decorator)
o   selector – contains the directive name, where we want this component to be displayed. In our example its inside index.html
o   template  - defines HTML required for the view. And HTML required should be inside Backtick character
  • {{ }} – databinding expression
  • At runtime the default content from <my-app> directive will be replaced with above defined component’s template.
PS: You can use single quotes or double quotes in place of Backtick character when view template HTML is in single line, if the view template HTML is in more than one line you must use backtick character else you will get an error.

We can have either an inline view template as above or an external view template as below (for the same above example):





The external template view, is referred with keyword 'templateUrl' in decorator. The path value is relative to the index.html (the file which uses the component)

Disadvantages of having inline view templates:
  • We loose VisualStudio editor intellisense, code-completion and formatting features.
  • TypeScript code is not easier to read and understand when it is mixed with the inline template HTML.
Advantages of having external view template:
  • We have  loose VisualStudio editor intellisense, code-completion and formatting features available.
  • Not only the code in TypeScript file (in our example app.component.ts) is clean but also easier to read and understand.
Angular 2 recommends to extract templates to a separate file (external view template), if  view template HTML is longer than 3 lines.

Saturday, September 8, 2018

Angular - Setting up Angular in Visual Studio 2017

In this post, I will be providing you the steps to set up Angular in Visual Studio 2017 

Step 1 : Install Node.js and npm

  • node version 4.6.x or greater
  • npm version 3.x.x or greater
  • Site to download: https://nodejs.org/en/download/
  • To check the versions installed run following commands from cmd prompt (if node and npm are not installed on the system, the commands will not run and it will say “unrecognized internal or external command)
    • node –v
    • npm –v
Step 2: You also need VS 2017 (or VS 2015 update 3) 

Step 3: Configure environment settings for node and nmp
  • Tools -> Options -> Projects and Solutions -> External Web Tools
  • Move global path entry above the dev environment entry. This tells VS that look for external tools like npm is global path before internal paths
Before:



After:



              
Step 4: Install TypeScript for VS 2017
  • Angular 2 requires TypeScript version 2.2.0 or later
  • Download TypeScript For visual Studio 2017 from here 
https://www.microsoft.com/en-ca/download/details.aspx?id=55258
For VS 2015 from here: 
https://www.microsoft.com/en-us/download/details.aspx?id=48593
  • To check the version of typescript installed in VS, go to help-> About 


Step 5: Download “Quick Start Files” from Angular web site 
https://github.com/angular/quickstart Click on “clone or download” button and download zip file

Step 6: Create an empty web application in Visual Studio

Step 7: Copy “Quick Start Files” in created web application:
  • We don’t need all the files  from extracted “Quick Start Files” only following are required to be copied to empty web application)
§  “scr” folder and all its content
§  bs-config.json
§  package.json
§  tslint.json

  • Copy all these 4 items to the root project folder of the web application solution
Step 8: Restore required packages
  • Right click on the package.json -> restore packages (if the restore option is not available , try restarting Visual Studio and do clean solution)
  • Caution: it restores all the packages in the projects and a new folder node_modules folder is also seen under the projects, make sure that you don’t include that in the project.

Step 9 : Run the Project from command prompt
  • Open Command prompt
  • Go to root project location
  • And then type nmp start
  • It does following things
§  Launch TypeScript compiler and compiles the application code
§  After that launches lite server which in turns launches the browser
  • Now go and do a changes in startup project file "app.component.ts" and save it. This will directly be reflected in the browser as the lite-server is listening to the changes we are doing in the project files.
  • At this point we don’t have an option to run the Project in Visual Studio by pressing F5 or Ctlr + F5 that’s due to the wrong required file (css, and js) locatiosn in index.html page to fix that do following:

§  Changes  <base href="/"> to  <base href="/src/">  
§  Change script location to have a forward “/”  before node_modules
<script src="/node_modules/core-js/client/shim.min.js"></script>           <script src="/node_modules/zone.js/dist/zone.js"></script>
<script src="/node_modules/systemjs/dist/system.src.js"></script>

§  We also need to make a change in systemjs.config.js - 'npm:': '/node_modules/




PS: Sometimes one might see this error in while building the project in VS:
"(TS) Property 'lift' in type 'Subject<T>' is not assignable to the same property in base type 'Observable<T>'.  Type '<R>(operator: Operator<T, R>) => Observable<T>' is not assignable to type '<R>(operator: Operator<T, R>) => Observable<R>'.    Type 'Observable<T>' is not assignable to type 'Observable<R>'.      Type 'T' is not assignable to type 'R'."

To solve this issue, inside file tsconfig.json, put it in "compilerOptions" and set it to true.


Angular - Introduction

This is the introductory post for the series of post I am gonna write on Angular - a front-end web application framework.

Before heading to Angular let me give a small note on its predecessor AngularJS: 
  • A JavaScript based open source front-end web application framework (maintained by google) – a library written in JavaScript.
  • Normally referred as Angulrar 1 or Angular1.X ( released on October 2010)
  • It’s perfect for SPAs (Single Page Applications) – a web application or a web site that interacts with the user by dynamically rewriting the current page rather than reloading entire new pages from server.
Even though Angular is successor of AngularJS but its completely different than AngularJS, since its rewritten from ground up using TypeScript, so you cannot directly upgrade your application to Angular from AngularJS.

Angular is commonly referred as Angular 2+ or Angular v2 and above.

Versions:
o   Angular 2 –  released on year 2016
o   Angular 4 –  released on early 2017
o   Angular 5 –  released on late 2017               
o   Angular 6 – released on year 2018

o   Angular 7 – future 

Difference between Angular and AngularJS
  •  Performance
    • Improved test-ability, dependency injection  
  • Support for Mobile Applications
    • AngularJS was not directly compatible on mobile devices, one had to use third party frameworks to make AngularJS application run on mobile devices on the other hand Angular 2 is written from ground up with the support for mobile devices, so any single page application built in Angular works across mobile and desktop devices.
  •  Component Based Development
    • Component based development is the future of web development and in Angular 2 everything is component.
    • Component facilitate code reuse.
    • Use of component makes Angular 2 more testable.
  • More language choices – with Angular 2 we have more language choices available to write the code, below is the list of supported languages
    • ECMAScript 5 (Javsactipt?)
    • ECMAScript6 (aka ES 2015)
    • TypeScript (the most popular language among the list, Angular itself is written in TypeScript. TypeScript has great support for ECMAScript 6 standard.)
    • Dart
    • PureScript
    • Elm etc

Tuesday, August 1, 2017

ASP.NET - Web Application Architecture

Web Application works in Client/Server architecture. On Client side you need a web browser that understands HTML and on Server side you need a Web Server to host the application

ASP .NET Web application runs under Microsoft Internet Information Service (IIS).

Let's understand how a request is being handled in web application.

When a user requests for a URL in a web browser, the request goes to the web server (in our case it is IIS) through internet using HTTP protocol (Hyper Text Transfer Protocol). Once the web server, receives the requests it process that request (For example if the data need to be retrieved from database, IIS contacts the application DLL to get the data from database) and send the HTML content back to the browser.

Protocol - Set of standard rules, that describes how two or more items communicate over internet.

ASP .NET - Web Application Introduction

A web application is an application that is accessed by users by using a web browser. Example of web browsers:

  • Microsoft Internet Explorer (IE)
  • Google Chrome
  • Mozilla Firefox
  • Apple Safari
  • Netscape
There are many frameworks to build a web application and ASP.NET is the one among them, that is created by Microsoft to build dynamic data driven web application and web servicesASP .Net is introduced in 2002 and it is the successor of the Classic ASP. 

There are many advantages of having a web application over a desktop application (Example of desktop application is Microsoft Office).

  • Easy Deployment: Desktop application need to be deployed in each of the users's computer who need the access to the application, so lets say if there are 5000 user which requires the same application it has to be installed in 5000 computers. On the other hand a web application is deployed in a web server and accessed using a web URL in a web browser,  and all the 5000 users can access that if they have a wen browser which understand HTML.
  • Ease of Maintenance/Support/Patch: If a bug fix is done in the application, in case of desktop application patch has to be deployed to ease user's system where as in case of web application the patch is only need to be applied to the Web Server where the application is being hosted. 
  • No additional software required to access the web application as long as user has the web browser that can understand HTML, in place of Desktop application each user need to have the application installed in their computers.
  • Cross Platform: In case of desktop application, if the application is developed in Windows platform it can only be accessed in windows OS, to make the application accessible in other platforms it has to be re developed in those platforms, where as in case of web application irrespective of which platform the application is being developed, it can be accessible to the user if user has a browser that understands HTML.
Framework - In simple terms framework is a collection of classes.

Monday, December 12, 2016

"Favor Composition over Inheritance" Is this Correct ???

You must have heard or read "Favour Composition over Inheritance" from may people, many blogs and books.  Here goes my opinion on the same...

Anyone who had worked on .NET framework to build applications has extensvily used both Inheritance as well as Composition. When a class object is being referenced by the another class object, it forms the parent/child hierarchy, where the object being referenced is the object of sub-class and the object which is holding the the referenced object is the super-class object, and this parent-child hierarchy is refereed as Inheritance. When a class uses another object to provide some or all of its functionalities then it's refereed as Composition

Whenever Inheritance is being discussed, knowing or unknowingly Composition comes into the picture. Lets understand both the concepts:

  • Both supports code reuse
    • Composition - The purpose of it is to make wholes out of parts using code reuse. 
    • Inheritance - The purpose of it is to make parent-child hierarchy to reuse code (common code defined in parent class). 
  • "Is A" and "Has A" Relationship 
    • Composition is a "Has A" relationship as the the class owns the object of another class.
    • Inheritance is a "Is A" relationship as the sub-class is also a type of super-class hence the object of super class can be used as reference object to hold the object of sub-class.
  • Coupling
    • In case of composition the class object is been used directly by the another class, so the reusable class is robust.
    • In case of Inheritance, the super-class doesn't know anything about the sub-classes so any changes goes into the sub-classes there is no change in the behaviours of super-class but the sub-class knows about the super-class as it inherits all its behaviours so any changes goes into the super-class the sub-classes may get affected which makes a tight coupling between them.
  • Test-ability
    • Composition provided more flexibility for testing as its is easy class to mock objects used inside the class.
    • In case of Inheritance you will need the base class (super-class). Since the derived classes (sub-classes) are tightly coupled with the base class (super-class) it become slightly difficult to mock derived class objects.
    • In case of TDD mocking Composition are far much faster and easier than mocking derived classes.

Many say that go with composition when you need multiple functionalities in a class, as you can't inherit from more than one class in .NET. But what I believe is, if you have only this reason (a class can't inherit from multiple base classes) to go for Composition then you don't have to as we can achieve this by making class implement multiple interfaces.

Both Inheritance & Composition are useful while developing applications. Just that you need to know when to use what? Below are the steps I follow while selecting Inheritance or Composition.
  • First Approach:
    • Use Inheritance when you want to reuse the class without modifying any of its behaviours. Basically If sub-class is adding functionality to the base class go with Inheritance, if sub-class is removing behaviours from the super-class, question inheriting from super-class.
  • Second Approach:
    • Use Inheritance when you have "Is A" relationship like, a car "is a" vehicle. 
    • Use Composition when you have "Has A" relationship like, a car "has an" engine. 

Sunday, December 11, 2016

DIP vs DI vs IoC vs SL

Whenever DIP is discussed, a lot of developers confuse this with DI and IoC. So today we are going to see how these three are different from one another.
  • DIP - Dependency Inversion Principe
    • Its a principle which says 
      • High level modules should not depend on low level modules. Both should dependent upon abstraction.
      • Abstraction should not depend upon details. Details should depend on abstraction.
    • It never say how this principle can be achieved.
  • IoC - Inversion of Control
    • A programming style where a framework or run-time controls the flow.
  • DI - Dependency Injection
    • A software Design Pattern of injecting a class's dependencies into at run-time.
    • DI uses a builder object to initialize objects and provide the required dependencies to the object. 
    • There are three ways to achieve this
      • Constructor Injection
      • Setter Injection (Property Injection)
      • Interface Injection
  • SL - Service Locator
    • Introduces a locator object that, objects use to resolve dependencies.