Thursday, November 27, 2014

What and Why of CDN

What is CDN?
CDN is short for Content Delivery Network. A CDN in simple terms is a collection of servers that are spread across the globe. In other words, a CDN is a network of servers in which each request will go to the closest server.

Why Do We Need CDN?
For any web application, data can be categorized into either static or dynamic content. Dynamic content is the one which generally comes from a database. Static content is the one like CSS, images, JavaScript, flash files, video files, etc.

Now one may ask, how are requests served when a user enters an URL in the browser? Interesting… let’s take a look at it. Before knowing a CDN and its usage, it is very important to understand this process.

How is a Web Page Served?
When a client requests any URL, let’s say in the browser, there is a server configured to listen to any request that comes in for A server in turn performs some computations and sends raw HTML back to the client.

Raw HTML contains lots of links like CSS, JavaScript, etc. Now for each and every link (CSS, JavaScript, etc.), the client will make more requests to other servers, which means for each and every static resource, the client will make a request to the server. Now one can easily imagine the number of requests sent for a single web page. You can view this by using any of the debugging tools.

In case of a small website, all these requests are served from a single server. So, a single client is having many connections with the same server.

For a server to be serving millions and millions of requests, it may run out of resources. Can’t we optimize something here?

If you will look at the figure shown above, the points marked in red are the intermediate nodes which serve free of cost and nodes at both ends fall in the paid category. We may have very good bandwidth at the first and last node but those intermediate nodes hardly guarantee for any performance or throughput. In essence, the further the user is from our infrastructure, the more he will notice these performance issues.

A CDN has been designed to solve all these shortcomings. There are many CDN providers available in the market today (i.e. Akamai, Interwoven). One of the CDNs that I worked with is KeyCDN.

KeyCDN has points of presence (POPs) all around the world. So, whenever a request for a webpage is submitted by a client, it will go to the closest server available. In this manner, your request doesn’t need to go a long distance plus there won’t be many number of requests between the actual server and the client. KeyCDN also provides various ways to control your settings including delivery status and reporting. It also has a very intuitive dashboard and the most impressive thing is, it is affordable!

A CDN will deliver all the static content. So, actual dynamic content is kept on company’s server and the static content is redirected to CDN provider. So, there are multiple advantages of doing this.

Advantages of using CDN
  • Client request will go to the closest POP to get the static content rather than going all the way to the web server.
  • The actual web server will have more bandwidth available as the CDN takes a lot of load.
  • Failover: In case a server goes down, the CDN will automatically reroute the traffic to the next available server
  • The biggest advantage is caching. Whenever a request comes in for the first time, the content will be cached on an edge server. When the next request comes in, it will read the data directly from its cache.
Enjoy learning!!!

Saturday, November 22, 2014

Handling UI control's events in ViewModel [Prism 5.0]

Recently I came across a requirement in which I was supposed to data bind a command from my viewModel to an event. In other words, my code-behind was not supposed to contain any code related to event handlers of a control.

After digging more into Prism, luckily I found my answer. Above requirement can be achieved using InvokeCommandAction provided in Prism 5.0.

Well, so my article will elaborate more on how to achieve this.

InvokeCommandAction consents us to invoke ICommand implementation on our viewModel to an event that exist on our control. It basically means, we are no longer bound or required to have a command property on a control in order to invoke a command.

Now, you may say you have seen this similar thing before.
And you are right because Blend SDK ships one. But the InvokeCommandAction which Prism provides is little bit different in two ways:
  • First, it manages the state of the element. It updates the enable state of the control that is attached to based on the return value of the command CanExecute. So, here you are getting the same behavior that you do for any control, which is super cool. Isn't it?
  • Second, it allows us to pass event arguments as a parameter to the command.
Now one may ask a question, whether we have to pass entire EventArgs as an parameter or we can just pass the required stuff ?

And the answer is YES. Of course, you are not forced to pass entire EventArgs to your viewModel. One can also pass specific portion of an EventArgs which is really required. This can be achieved by setting TriggerPathParameter on the InvokeCommandAction.
Let's quickly jump on to the code.

Using Code
In order to explain the notion of InvokeCommandAction, I'll create a very simple application having a ListBox and a Label.  The Label will be updated based on the selected item in the list.
Expectation: Whenever an item is selected in the list, we have to listen to the SelectionChanged event and then handle that event in viewModel through command and then update the selected text item on Label.

Setting up a View
<StackPanel HorizontalAlignment="Left" VerticalAlignment="Top">
      <ListBox Height="100" Width="300" Margin="1,20,0,0" ItemsSource="{Binding ListOfItems}" SelectionMode="Single"/>
      <LabelFontWeight="Bold" Width="170" Height="30" Content="Please select"/>
I am setting datacontext for this view in code-behind. But same can be done in XAML also.

Setting up a ViewModel
Here I'll not explain each and every line of viewModel's code as I already covered the same in my other articles in detailed manner.
public class MainWindowViewModel:BindableBase
        public ObservableCollection<string> ListOfItems { get; set; }
        public ICommand SelectCommand { get; set; }

        private string _selectedItem = string.Empty;
        public string SelectedItem
            get { return _selectedItem; }
            set { SetProperty<string>(ref _selectedItem, value); }

        public MainWindowViewModel()
            ListOfItems = new ObservableCollection<string>() {"One","Two", "Three", "Four", "Five"};
            SelectCommand = new DelegateCommand<object[]>(SetSelectedItem);           

        private void SetSelectedItem(object[] listOfItems)
            if (listOfItems != null && listOfItems.Count()>0)
If you will notice the parameter of DelegateCommand, it is an object array. That's because our viewModel is going to expect a command action in the form of object array from the list of items we want to choose from.
Next thing is to handle a SelectionChanged event of our ListBox but we don't want to handle that in our code-behind as we are dealing with MVVM structure. So, in order to achieve this behavior, we will use triggers.

Let's go ahead and add a reference of System.Windows.Interactivity and add the namespace in XAML as:


Now above namespace can be used to attach the interaction triggers to the attached property as:
<ListBox Height="100" Width="300" Margin="1,20,0,0" ItemsSource="{Binding ListOfItems}" SelectionMode="Single">
Next we need to add is the EventTrigger with EventName property set as SelectionChanged. Please note, your EventName should match with the event of the control. In this case, we are interested in ListBox's SelectionChanged event.
Now whenever this event occurs, we want to invoke our command. So, this is where we are using our Prism's InvokeCommandAction as:

Let's quickly run the application and see how it reacts.

As soon as you try to select any item in ListBox, you will get a BIG exception:

It looks like, the parameter being passed to a command is the SelectionChangedEventArgs. We don't want that. We want something specific, we want our object. So, let's come back to our InvokeCommandAction and set property TriggerParamaterPath with the path of incoming EventArgs of the trigger. In our case, we want the AddedItems. Hence,
 <ListBox Height="100" Width="300" Margin="1,20,0,0" ItemsSource="{Binding ListOfItems}" SelectionMode="Single">
           <interactivity:EventTrigger EventName="SelectionChanged">
                  <prism:InvokeCommandAction Command="{Binding SelectCommand}" TriggerParameterPath="AddedItems"/>

Now when you will run the application, you will get the output as expected.

Hope you enjoyed learning!!!

Friday, November 14, 2014

Skype calls in browser

Microsoft just announced that soon we will be able to make voice and video calls from our web browser. This Skype for Web (beta) calls will work on Firefox, Safari, Chrome and of course IE. As of now, it will work as a plugin but soon it will work natively on all browsers without plugins. So, are you excited to try this out ???

Friday, November 7, 2014

Microsoft to unveil its first Nokia-less Lumia on November 11

Yes, its true. Soon Microsoft is about to reveal its own Lumia having 5-inch display, 1.2 GHz processor, 1GB of RAM, 8GB of storage and 3G-only connectivity. First look of this Lumia is already out:

More info

Thursday, October 23, 2014

PopUps with Interactivity using ConfirmationRequest [Prism 5.0]

In continuation to my previous blog on PopUps with Interactivity, here we will see how to implement IConfirmation request. In order to make this post short, I'll implement IConfirmation request on top of previous example. 

Let's open our viewModel and add property for InteractionRequest of type IConfirmation  as:

public InteractionRequest<IConfirmation> ConfirmationRequest { get; set; }

Now for every getter/setter we should have a corresponding command, which will help us in invoking this request:

public ICommand ConfirmationCommand { get; set; }

Just like NotificationRequest, we need to create instances of these objects in constructor as:

If you will see above snippet closely, you will notice that in case of Confirmation, we are handling Status in slight different manner.

Next step is to update our View as we did for NotificationRequest.

Now everything is in place. Let's quickly run the application and click on PopUp button:

Based on user's choice, whether OK or Cancel button is clicked, status message will be displayed as shown:

Hope you understood the use of ConfirmationRequest. 

Friday, October 17, 2014

PopUps with Interactivity using NotificationRequest [Prism 5.0]

Just about every application has a need to notify user about an event or ask for confirmation before proceeding onto the next operation. Prior to MVVM, we would have used the MessageBox class in the code-behind. But for MVVM applications, that’s not the appropriate way as it breaks the separation of concerns from the view or viewmodel. There are lots of ways to show popups in MVVM application. In Prism, we just happen to use triggers.

Triggers are used to initiate actions when a specific event is raised. So, it means we have to setup a view to detect the interaction request of event and then present an appropriate visual display for that request.

What is required for raising events?
Now for raising events, we need an event trigger. But not just any event trigger. We don’t want to use the built-in event trigger, instead Prism provide its own InteractionRequestTrigger. This trigger binds to the source object or the InteractionRequest object that exist in your viewmodel. It automatically wires up and connects to the appropriate raised event on that request.

What next?
Once that request event is raised, the InteractionRequest should than invoke an action and this action call the PopUpWindowAction and displays a popup window to the user. When it is shown its data context is set to the context parameter of the InteractionRequest.

You can even specify your own custom window content by setting the window content property on the popup window action object. The Tile of the popup window is bind to the Tile property of the context object

There is couple of interfaces one need to know about:
1)      INotification
2)      IConfirmation
3)      Custom

INotification has two contracts on its two properties as Tile and Content. The Tile property, I just talked about is the property it reads from. Next is the Content property, which is going to be our message. So, if you are not providing your own window content, this message is what’s going to show in the default popup window, that is shown using this request. I like to mention that INotification request is only used when you are trying to notify user about something.

Next we have is IConfirmation request, which extends INotification. It adds a new confirmed property which basically signifies, if the request was confirmed or not. We use IConfirmation request for scenarios where we want to use a messagebox for Yes/No type answer.

And of course you can always create your custom request. So, if you want to pass custom objects or custom information or INotification - IConfirmation doesn’t solve your problem, you can create your own.
Implementing Popups
Implementing popups are not at all difficult whilst below steps are followed:
1)      Declare InteractionObject<T> object in viewmodel
2)      Need a DelegateCommand to raise request
3)      Add trigger in view
4)      Inside trigger, add InteractionRequestTrigger
5)      Add PopupWindowAction
6)      Bind command to button

Code starts here...
Let's create a simple view with a button and a label. On click of this button, we will see how to show notifications using MVVM and my label will display the status of notification.

I have a viewModel which has a single property called Status and this property will be used to display response of my request. 

Before moving further, let's go ahead and add reference of Prism.Interactivity using Nuget. Once reference is in place, we will quickly modify our viewModel by adding property of type InteractionRequest<T>. Here T is the type of request, we want to use which is INotification in our case:

 public InteractionRequest<INotification> NotificationRequest { get; set; }

Now for every getter/setter we should have a corresponding command, which will help us in invoking this request:

public ICommand NotificationCommand { get; set; }

Next thing is to instantiate above properties in our constructor of viewModel and raising the notification as:

In above snippet, you will notice that I also provided callback. This callback will get executed, when user acknowledges the notification and dialog closes. In my case, I am setting my Status property to 'Done' in callback.

Now, rest of work is in our view. In order to support notifications, couple of namespaces need to be added in view for Interactivity and Prism.
Next we need to add some interaction triggers:
        <prism:InteractionRequestTrigger SourceObject="">
            // TODO: Define action here

In above snippet, we need to set SourceObject. In our case, SourceObject is the request object, which is set in our viewModel and we call it NotificationRequest.
Once SourceObject is set, we need to define an action. For us, it will be PopupWindowAction. Next thing is invoking the request, which will fire our trigger and will in turn show our popup window. In order to achieve that, we need to set a command property on our button. Once all above things are done, our code will look like:

We are all set. Quickly build and run your solution. You will land up with below screen:

Click on the PopUp button, you will receive a notification message as:

As soon as user clicks on OK button, label on the MainWindow will get updated as:

User is notified. So, our callback successfully updated our status property. Hope you enjoyed learning !!!

Saturday, October 4, 2014

Introducing Windows 10

Hope most of you are aware that in the mid of last week, Microsoft announced Windows 10 (Technical Preview). This preview is a small set of new code and is not the final one. It means lot more is on the way :)

This release contains desktop-focused features, primarily for business customers. Actual consumer preview is plan to launch somewhere around January.

Windows 10 will run across unbelievably broad set of devices from 4 inch screens to 80 inch screens. Some of the devices we can hold in hand and others are 10 feet away.
So, are you ready to dive into the new features?
Well, let's start.

Start Menu: Start menu with Live tiles and favourite apps

Everything runs in a window: Windows store apps now opens in the same fashion as desktop apps format – can be re-sized and move around – have title bars

Snap Enhancements: New quadrant layout to view four snapped apps in single shot

Multiple Desktops: Creation and switching between multiple desktops

Find files faster: Feature to list out recently used files in File Explorer

Sometimes, trying out such an early build can be risky. So, it is always recommended to read the precautionary notes before proceeding for installation on a primary/business machine.
Once Windows Technical Preview is installed, recovery partition won’t work. It means, if you want to go back to your previous version of Windows, then you have to re-install that version from the installation media. More...

Wednesday, September 17, 2014

What's New In Prism 5.0?

Are you WPF, Silverlight or Windows Phone developer and used Microsoft’s patterns and practices library to build your applications? If you are, then you might want to know that Microsoft’s patterns and practices team have just released Prism 5.0. All the applications built using the previous versions of Prism are now broken. So, in this artifact, I’ll be discussing about the new assemblies, new objects and deprecated objects which can/can’t be used with Prism 4.1 and Prism 5.0.
Downloading Prism 5.0

Prism 5.0 can be downloaded and installed either from patterns and practices site having URL as or by using Nuget package inside Visual Studio. Mentioned link also discusses about all the changes which are part of Prism 5.0

Supported Platforms
Let’s have a quick look at the supported platforms of Prism 5.0. While working with previous versions of Prism (i.e. 4.1), one was able to create applications like WPF (.Net 4.0), Silverlight 5 and Windows Phone (7.5). Point to note here is, Prism 5.0 only supports WPF (.Net 4.5).

Prism 4.1
Prism 5.0
WPF (.Net 4.0)
WPF(.Net 4.5)
Silverlight 5
Windows Phone (7.5)

In other words, if your application is written in Silverlight or Windows Phone and you are planning to upgrade to Prism 5.0, then it’s not going to work. In this case, either you have to use some tool and make your application as WPF application (if possible) or simply stick to the existing version of Prism.
Assembly Changes

This section discusses about all the assembly related changes that Prism 5.0 introduces. Please note, in below table all the assemblies are prefixed with Microsoft.Practices:

Prism 4.1
Prism 5.0
Prism.Composition [Renamed]
Prism.Interactivity [No change here]
ServiceLocation [No change here]
Unity [It is now a PCL]
Prism.SharedInterfaces [Is is also a PCL]
Prism.MVVM [It is also a PCL]
Prism.PubSubEvents [It is also a PCL]

If you will see above table, the assembly named Microsoft.Practices.Prism in Prism 4.1 is no longer called Microsoft.Practices.Prism in Prism 5.0. Now this assembly is renamed to Prism.Composition.
There is no change in Prism.Interactivity, ServiceLocation, Prism.UnityExtensions and Prism.MefExtensions front.

This Prism.Composition takes dependency on another new assembly called Prism.SharedInterfaces, which is a PCL (Portable Class Library).
On MVVM front, there are two more additions. One is Prism.MVVM which is again a PCL and shares a common MVVM functionality across Windows Store Apps and Windows Presentation Foundation applications. Now to get around of few limitations and some needed enhancements on WPF, a separate assembly is created having name as Prism.MVVM.Desktop, which is specifically meant to be used on desktop.

One more addition is Prism.PubSubEvents. This is event aggregator. So, event aggregator is called out of Prism and has been kept into its own PCL library in Prism 5.0

Deprecated Objects
Deprecated objects are the objects which are still in assemblies but we just don’t want to use them anymore and if you are currently using them, then you have to move them to different object instance. Below is the list of such objects:




Microsoft.Practices.Prism.MVVM.BindableBase class




Microsoft.Practices.Prism.MVVM.PropertySupport class




Microsoft.Practices.Prism.PubSubEvents.PubSubEvent class



Objects moved to new location

There are few objects which are given the new home in Prism 5.0. If you are using any of the below mentioned objects, then you have to re-reference your assemblies with the new ones. Apart from assemblies, namespace is also changed. Please note, below mentioned changes are the breaking changes.

New Location

Removed Objects
There are few objects in Prism 4.1, which are completely removed from Prism 5.0. This section discusses about the objects that are completely gone. This is again considered as a breaking change.

ButtonBaseClickCommandBehavior Click
Marked as obsolete in Prism 4.1

Apart from all these changes, few changes are made to Quick Starts and help files also.