Thursday, December 31, 2015

Broadcast and Emit in angularjs

In Angular $broadcast() service is always used to propagate event to all of his child controller and it’s registered parent $rootscope.scope listeners.  

  $rootScope.$broadcast('SummaryEvent', {
                priority: priority               
            });




$on() service is used to listen on any given type of event raised by $broadcast and $emit
   $scope.$on(''SummaryEvent'', function (event, args) {
Vm.priority=args.priority
        });



AngularJs  $broadcast () , $on() and $emit()
AngularJs  $broadcast ()  and  $on()  



$emit service is similar to $broadcast but it is used to propagate event to upwards through the scope hierarchy and notify to the registered $rootScope.Scope listeners

AngularJs  $broadcast () , $on() and $emit()
AngularJs  $on() and $emit()

AngularJs scope.$watch, $watchgroup and $watchCollection

$watch service helps us to do something or run any business logic when any property or variable value attached to $scope changed



$watch(variable, listener)
function MyController($scope) {

   $scope.myVar = 1;

   $scope.$watch('myVar', function() {
       alert('hey, myVar has changed!');
   });

   $scope.buttonClicked = function() {
      $scope.myVar = 2; // This will trigger $watch expression to kick in
   };

}

There are some other additional angular service $watchCollection and $watchgroup .

Scope.$ watchgroup: it helps us to watch a set of group of variables together , if anyone of them is changed, it will trigger $watchGroup expression.

$watchGroup(watchExpression, listener)

Scope.$watchCollection : it helps us to watch whole object’s properties , we need to pass object instead of each properties of object.


Wednesday, December 30, 2015

C#: Recursive Read Exception along with Inner Exception

this blog demonstrates how to log the exception message along with n depth of innerexception in C#. the better logging always helps to developer to find out the optimal solution for problem.

Here is  a recursive method to log exception along with inner exception :

C# : 


C#: Recursive Read Exception along with Inner Exception

the above LogInnerException method  recursively log the inner exception untill it reach to NULL.

C# : 

C#: Recursive Read Exception along with Inner Exception

above readException method reads the exception as well as aggregateException message too.

Thanks for Visiting !!

Tuesday, December 29, 2015

oDATA ASP.NET Web API $Filter

OData supports various kinds of filter options for querying data and $filter allows clients to filter a collection of result that are getting by requested oData URL.



Operator
Description
Example
Logical Operators
Eq
Equal
/Suppliers?$filter=Address/City eq 'Redmond'
Ne
Not equal
/Suppliers?$filter=Address/City ne 'London'
Gt
Greater than
/Products?$filter=Price gt 20
Ge
Greater than or equal
/Products?$filter=Price ge 10
Lt
Less than
/Products?$filter=Price lt 20
Le
Less than or equal
/Products?$filter=Price le 100
And
Logical and
/Products?$filter=Price le 200 and Price gt 3.5
Or
Logical or
/Products?$filter=Price le 3.5 or Price gt 200
Not
Logical negation
/Products?$filter=not endswith(Description,'milk')
Arithmetic Operators
Add
Addition
/Products?$filter=Price add 5 gt 10
Sub
Subtraction
/Products?$filter=Price sub 5 gt 10
Mul
Multiplication
/Products?$filter=Price mul 2 gt 2000
Div
Division
/Products?$filter=Price div 2 gt 4
Mod
Modulo
/Products?$filter=Price mod 2 eq 0
Grouping Operators
( )
Precedence grouping
/Products?$filter=(Price sub 5) gt 10


Sunday, December 27, 2015

C# : Singleton Vs Static Class


A singleton object stores common data in only one place and in similar a static class is also used to store single-instance data so by using singleton or static class we can able to save state between usages and store cache data to improve performance.

Singleton is an object which can only be instantiated once and shared.

C# Singleton Pattern

The big difference between a singleton and a bunch of static methods is that singletons can implement interfaces (or derive from useful base classes, although that's less common IME), so you can pass around the singleton as if it were "just another" implementation.

Other differences as below:
  • Singleton object stores in Heap but, static object stores in stack
  • We can clone the object of Singleton but, we cannot clone the static class object
  • Singleton class follow the OOP(object oriented principles) but not static class
  • We can implement interface with Singleton class but not with Static class.
The Singleton pattern has several advantages over static classes. 

·         A singleton can extend classes and implement interfaces, while a static class cannot (it can extend classes, but it does not inherit their instance members). 

·         A singleton can be initialized lazily or asynchronously while a static class is generally initialized when it is first loaded, leading to potential class loader issues. 

However the most important advantage, though, is that singletons can be handled polymorphic ally without forcing their users to assume that there is only one instance.

Singleton Pattern: 

Here is an example of singleton pattern implemented class HttpClientManager, which manages the single instance of HttpClient, if there is no any instance of HttpClient, it creates new object of HttpClient.

C# Code:
public sealed class HttpClientManager
    {
        private static HttpClient instance;
        private static object syncRoot = new object();
        public static HttpClient Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            var handler = new HttpClientHandler {
UseDefaultCredentials = true,
Credentials = CredentialCache.DefaultCredentials };
                            var client = new HttpClient(handler, false)
                            {
                                BaseAddress = new Uri("http://localhost")
                            };
                            instance = client;
                        }
                    }
                }
                return instance;
            }
        }

    }
 


Thanks for visiting!!


Monday, December 7, 2015

WCF : The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.

I encountered the WCF message size exception, while pulling more than 2K records from database

Exception: The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.

As per error message, the value of MaxReceivedMessageSize property should be increased to resolve this problem. by default, the value of MaxReceivedMessageSize is 65536 and we can change its value to integer max size (2147483647)

<basicHttpBinding><binding name="BasicHttpBindingService"  maxReceivedMessageSize="2147483647" maxBufferSize="65536  transferMode="Buffered/>   </basicHttpBinding>



by default transferMode is "Bufferd", then you will get another exception

Exception: For TransferMode.Buffered, MaxReceivedMessageSize and MaxBufferSize must be the same value. Parameter name: bindingElement

As per error  message, if TransferMode is Buffered the both MaxReceivedMessageSize and MaxBufferSize  value should be same.

<basicHttpBinding><binding name="BasicHttpBindingService"  maxReceivedMessageSize="2147483647" maxBufferSize="2147483647 transferMode="Buffered/>   </basicHttpBinding>

For more information about Transfer Mode, please visit on this url (WCF Transfer Mode)

If you are still getting this message size issue, then you should explore the other message encoding pattern, by default it is Text\XML, you can choose MTMO or Binary encoding pattern.

What is MTOM? 

MTOM is an efficient technology for transmitting binary data in WCF messages and  MTOM encoder attempts to create a balance between efficiency and interoperability. The MTOM encoding transmits most XMLin textual form, but optimizes large blocks of binary data by transmitting them as-is, without conversion to text and for more information please visit on this url (WCF MTOM MessageEncoding)

Thanks for Visiting!!

Tuesday, December 1, 2015

ODATA ASP.NET WEB API - $top $skip $orderby $expand $select $filter

OData stands for Open Data Protocol and OData technology gives you the best way to design the Restful service similar asp.net Web API.

OData defines an abstract data model and a protocol that let any client access information exposed by any data source and it allows the creation and consumption of querable and interoperable RESTFULL APIs in simple and standardway.

You can expose data service as OData api for product entity as below manner and can do many things on top of that

  • $top
  • $skip
  • $orderby
  • $expand
  • $select
  • $filter


$top :

A data service URI with a $top System Query Option identifies a subset of the Entries in the Collection of Entries identified by the Resource Path section of the URI. This subset is formed by selecting only the first N items of the set, where N is a positive integer specified by this query option.


The first 5 Product Entries returned where the Collection of Products are sorted using a scheme determined by the OData service.

$skip :

A data service URI with a $skip System Query Option identifies a subset of the Entries in the Collection of Entries identified by the Resource Path section of the URI. That subset is defined by seeking N Entries into the Collection and selecting only the remaining Entries (starting with Entry N+1). N is a positive integer as specified by this query option.


The set of Product Entries (associated with the Category Entry identified by key value 1) starting with the third product.

$orderby :

data service URI with a $orderby System Query Option specifies an expression for determining what values are used to order the collection of Entries identified by the Resource Path section of the URI. This query option is only supported when the resource path identifies a Collection of Entries.


The set of Products is subsequently sorted (in descending order) by the Name property of the related Category Entry.




$expand:

 A URI with a $expand System Query Option indicates that Entries associated with the Entry or   Collection of Entries identified by the Resource Path section of the URI must be represented inline


Identifies the Collection of Categories as well as each of the Products associated with each Category

$select:

A data service URI with a $select System Query Option identifies the same set of Entries as a URI without a $select query option; however, the value of $select specifies that a response from an OData service should return a subset of the Properties which would have been returned had the URI not included a $select query option.

In a response from an OData service, only the Price and Name Property values are returned for each Product Entry within the Collection of products identified.

Querying Data
OData supports various kinds of query options for querying data.

$filter

The $filter allows clients to filter a collection of result that are getting by requested oData URL.

Operator
Description
Example
Logical Operators
Eq
Equal
/Suppliers?$filter=Address/City eq 'Redmond'
Ne
Not equal
/Suppliers?$filter=Address/City ne 'London'
Gt
Greater than
/Products?$filter=Price gt 20
Ge
Greater than or equal
/Products?$filter=Price ge 10
Lt
Less than
/Products?$filter=Price lt 20
Le
Less than or equal
/Products?$filter=Price le 100
And
Logical and
/Products?$filter=Price le 200 and Price gt 3.5
Or
Logical or
/Products?$filter=Price le 3.5 or Price gt 200
Not
Logical negation
/Products?$filter=not endswith(Description,'milk')
Arithmetic Operators
Add
Addition
/Products?$filter=Price add 5 gt 10
Sub
Subtraction
/Products?$filter=Price sub 5 gt 10
Mul
Multiplication
/Products?$filter=Price mul 2 gt 2000
Div
Division
/Products?$filter=Price div 2 gt 4
Mod
Modulo
/Products?$filter=Price mod 2 eq 0
Grouping Operators
( )
Precedence grouping
/Products?$filter=(Price sub 5) gt 10


Other Source :


SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...