Wednesday, 25 January 2017

What is Routing in AngularJS?

AngularJS Routing helps you to divide your app into multiple views and bind different views to Controllers. The magic of Routing is taken care by an AngularJS service $routeProvider. $routeProvider service provides method when() and otherwise() to define the routes for your app. Routing has dependency on ngRoute module.

Defining Route for your application

 <script type="text/javascript">
var myApp = angular.module('myApp', ['ngRoute']);
myApp.config(['$routeProvider',
function ($routeProvider) {
$routeProvider.
when('/products', { //route
templateUrl: 'views/products.html',
controller: 'productController'
}).
when('/product/:productId', { //route with parameter
templateUrl: 'views/product.html',
controller: 'productController'
}).
otherwise({ //default route
redirectTo: '/index'
});
}]);
</script>

What is difference between $cookies and $cookieStore service in AngularJS?

$cookies - This service provides read/write access to browser's cookies.
If you want to use existing cookie solution, say read/write cookies from your existing server session system then use $cookie. 

var app=angular.module('cookiesExample', ['ngCookies']);
app.controller('ExampleController', function ($cookies) {
// Retrieving a cookie
var favoriteCookie = $cookies.myFavorite;
// Setting a cookie
$cookies.myFavorite = 'meal';
});

$cookiesStore - $cookieStore is a thin wrapper around $cookies. It provides a key-value (string-object) storage that is backed by session cookies. The objects which are put or retrieved from this storage are automatically serialized or deserialized by angular to JSON and vice versa.


If you are creating a new solution which will persist cookies based on key/value pairs, use $cookieStore.

var app=angular.module('cookieStoreExample', ['ngCookies']);
app.controller('ExampleController',function ($cookieStore) {
// Put cookie
$cookieStore.put('myFavorite', 'meal');
// Get cookie
var favoriteCookie = $cookieStore.get('myFavorite');
// Removing a cookie
$cookieStore.remove('myFavorite');
});

What is the use of Using Keyword in C#


Using statement is used to work with an object in c# that inherits IDisposable interface.
IDisposable interface has one public methoda called Dispose that is used to dispose off the object.When we use Using
statement,we don't need to explicitly dispose the object in the code, the using statement takes care of it.

using(SqlConnection conn= new SqlConnection)
{
}

When we use above code block,internally the code is generated like this

SqlConnection conn= new SqlConnection()


try
{

}
finally
{
   //calss the dispose method of the connection object.
}

Difference Between Finalize and Dispose Method

.Net Framework provides two methods Finalize and Dispose for releasing unmanaged resources like files, database connections, COM etc. This article helps you to understand the difference between Finalize and Dispose method.
Difference between Dispose & Finalize Method

Dispose
Finalize
It is used to free unmanaged resources like files, database connections etc. at any time.
It can be used to free unmanaged resources (when you implement it) like files, database connections etc. held by an object before that object is destroyed.
Explicitly, it is called by user code and the class which is implementing dispose method, must has to implement IDisposable interface.
Internally, it is called by Garbage Collector and cannot be called by user code.
It belongs to IDisposable interface.
It belongs to Object class.
It's implemented by implementing IDisposable interface Dispose() method.
It's implemented with the help of destructor in C++ & C#.
There is no performance costs associated with Dispose method.
There is performance costs associated with Finalize method since it doesn't clean the memory immediately and called by GC automatically.

Example for implementing the dispose method

  1. public class MyClass : IDisposable
  2. {
  3. private bool disposed = false;
  4. //Implement IDisposable.
  5. public void Dispose()
  6. {
  7. Dispose(true);
  8. GC.SuppressFinalize(this);
  9. }
  10.  
  11. protected virtual void Dispose(bool disposing)
  12. {
  13. if (!disposed)
  14. {
  15. if (disposing)
  16. {
  17. // TO DO: clean up managed objects
  18. }
  19. // TO DO: clean up unmanaged objects
  20.  
  21. disposed = true;
  22. }
  23. }
  24. }

Example for implementing Finalize method

If you want to implement Finalize method, it is recommended to use Finalize and Dispose method together as shown below:
  1. // Using Dispose and Finalize method together
  2. public class MyClass : IDisposable
  3. {
  4. private bool disposed = false;
  5. //Implement IDisposable.
  6. public void Dispose()
  7. {
  8. Dispose(true);
  9. GC.SuppressFinalize(this);
  10. }
  11.  
  12. protected virtual void Dispose(bool disposing)
  13. {
  14. if (!disposed)
  15. {
  16. if (disposing)
  17. {
  18. // TO DO: clean up managed objects
  19. }
  20. // TO DO: clean up unmanaged objects
  21. disposed = true;
  22. }
  23. }
  24. //At runtime C# destructor is automatically Converted to Finalize method
  25. ~MyClass()
  26. {
  27. Dispose(false);
  28. }
  29. }

Note

  1. It is always recommended to use Dispose method to clean unmanaged resources. You should not implement the Finalize method until it is extremely necessary.
  2. At runtime C#, C++ destructors are automatically Converted to Finalize method. But in VB.NET you need to override Finalize method, since it does not support destructor.
  3. You should not implement a Finalize method for managed objects, because the garbage collector cleans up managed resources automatically.
  4. A Dispose method should call the GC.SuppressFinalize() method for the object of a class which has destructor because it has already done the work to clean up the object, then it is not necessary for the garbage collector to call the object's Finalize method.

Differences between Object, Var and Dynamic type

C# is rich in data type. It provides three types Object, Var and Dynamic to store data of any type. 
In this article, I am trying to explore the differences among these three.
Difference between Object, Var and Dynamic type

Object
Var
Dynamic
Object was introduced with C# 1.0
Var was introduced with C# 3.0
Dynamic was introduced with C# 4.0
It can store any kind of value, because object is the base class of all type in .NET framework.
It can store any type of value but It is mandatory to initialize var types at the time of declaration.
It can store any type of the variable, similar to old VB language variable.
Compiler has little information about the type.
It is type safe i.e. Compiler has all information about the stored value, so that it doesn't cause any issue at run-time.
It is not type safe i.e. Compiler doesn't have any information about the type of variable.
Object type can be passed as method argument and method also can return object type.
Var type cannot be passed as method argument and method cannot return object type. Var type work in the scope where it defined.
Dynamic type can be passed as method argument and method also can return dynamic type.
Need to cast object variable to original type to use it and performing desired operations.
No need to cast because compiler has all information to perform operations.
Casting is not required but you need to know the properties and methods related to stored type.
Cause the problem at run time if the stored value is not getting converted to underlying data type.
Doesn't cause problem because compiler has all information about stored value.
Cause problem if the wrong properties or methods are accessed because all the information about stored value is get resolve only at run time.
Useful when we don’t have more information about the data type.
Useful when we don’t know actual type i.e. type is anonymous.
Useful when we need to code using reflection or dynamic languages or with the COM objects, because you need to write less code.

Monday, 23 January 2017

What is the difference between $scope and scope in AngularJS?

The module factory methods like controller, directive, factory, filter, service, animation, config and run receive arguments through dependency injection (DI). In case of DI, you inject the scope object with the dollar prefix i.e. $scope. The reason is the injected arguments must match to the names of injectable objects followed by dollar ($) prefix.

For example, you can inject the scope and element objects into a controller as given below:

module.controller('MyController', function ($scope, $element) 

    // injected arguments 
});

When the methods like directive linker function don’t receive arguments through dependency injection, you just pass the scope object without using dollar prefix i.e. scope. The reason is the passing arguments are received by its caller.

module.directive('myDirective', function () // injected arguments here {
 return 

       // linker function does not use dependency injection 
   link: function (scope, el, attrs) 
  { 
       // the calling function will passes the three arguments to the linker: scope, element and attributes,         //  in the same order 
   } 
};
});

In the case of non-dependency injected arguments, you can give the name of injected objects as you wish.
The above code can be re-written as:

 module.directive("myDirective", function () { 
return 
{
    link: function (s, e, a) 
   { 
     // s == scope // e == element // a == attributes   } 
    }; 
});

In short, in case of DI the scope object is received as $scope while in case of non-DI scope object is received as scope or with any name.