Category Archives: WPF

What are benefits of PRISM?

Modular development:- As we are developing components as independent units we can assign these units to different developers and do modular parallel development. With parallel development project will be delivered faster.
High reusability:- As the components are developed in individual units we can plug them using PRISM and create composed UI in an easy way.

What is PRISM?

PRISM is a framework to develop composite application in WPF and Silverlight. Composite applications are built using composition. In other words rather than building application from scratch we take prebuilt components, assemble them together and create the application.

Prism

Prism

Take the below example of simple WPF UI. You can see it has lots of sections. Now rather than building the whole UI as one big unit, we can develop all these section as independent unit. Later by using PRISM we can compose WPF UI by taking all these independent units.

PrismIndependentUnit

Explain delegate command in WPF?

First let us answer in short: – “Delegate command makes a MVVM command class independent of the view model”. Now let’s understand the long way.
In MVVM architecture view talks with the view model and view model talks with the model. When actions are sent from the view they are sent to WPF commands for handling the events. WPF commands invoked methods of view model internally.
In other words command needs reference of view model class.

Delegates in WPF

Delegates in WPF

If you see a typical WPF MVVM command it looks as shown below. You can see the
“CustomerViewModel” class referenced inside the ‘btnCommand” class. If you think with your eyes closed this reference of “CustomerViewModel” class inside the command is a problem. This will lead to tight coupling between command classes and view model.

If you visualize command it is nothing but click , double click , left mouse click , drag and drop etc. It’s an ACTION created by the user. Now wouldn’t be great if we can just attach this command with any view model. So like click event gets connected with “CustomerViewModel” or “SupplierViewModel”.

This is achieved by using delegate command.

public class btnCommand : ICommand
{
   Private CustomerViewModel Viewobj = new CustomerViewModel();
   public btnCommand(CustomerViewModel obj)
    {
      Viewobj = obj;
    }
   public bool CanExecute(object parameter) // When he should execute
    {
        return Viewobj.IsValid();
    }
   public void Execute(object parameter) // What to execute
    {
        ViewObj.Add();
    }
}

To decouple the view model class from command we can use delegates i.e. “Action” and “Func”. If you see the command class we need only two things “WhattoExecute” and “WhentoExecute”. So how about passing these methods as generic delegates. You can see the constructor of “btnCommand” takes two delegates one what to execute and when to execute.
You can see in the below code the “btnCommand” class has no reference of the view model class but has references to delegates which are just abstract pointer to functions / methods. So this command class can now be attached with any view model class. This approach is termed as “Delegate command”.

public class btnCommand : ICommand // Step 1 :- Create command
{
   private Action WhattoExecute;
   private Func<bool> WhentoExecute;
   public btnCommand(Action What , Func<bool> When)
     {
         WhattoExecute = What;
         WhentoExecute = When;
     }
   public bool CanExecute(object parameter) // When he should execute
     {
         return WhentoExecute();
     }
   public void Execute(object parameter) // What to execute
     {
        WhattoExecute();
     }
}

What is the difference between MVVM and 3 layer architecture?

MVVM has an extra layer as compared to 3 layer architecture. In 3 layer architecture we have UI (view) , business logic ( model) and Data access layer ( DAL). In MVVM we have an extra layer in between view and model i.e. the view model class.
3 layer architecture complements MVVM architecture.

MVVM vs 3-Tier

MVVM vs 3-Tier

What is the importance of command and bindings in MVVM pattern?

MVVM in the most used architecture because of command and bindings facility provided by WPF. WPF MVVM is incomplete without command and bindings.

MVVM Commands and Bindings

MVVM Commands and Bindings

Command and bindings helps you to connect view (WPF UI) with view model class without writing lot of behind code. Binding connects the UI input elements (textbox, combo box etc.) with the view model class properties and the UI actions like button click, right click are connected to the methods of the class by commands.

What are the benefits of MVVM?

Below are the benefits of MVVM pattern:-

Separation of concern: – As the project is divided in to layers, every layer handles its own responsibility. This leads to better maintenance because when we change one layer the other layer does not get affected.

Increased UI Reusability: – The whole point of MVVM is to remove behind code i.e. XAML.CS code. The problem with behind code is that it is tied up with a UI technology for example ASPX.CS code is tied up with the ASP page class, XAML.CS file is tied with WPF UI technology and so on. So we cannot use ASPX.CS behind code with WPF XAML UI.

By moving the behind code to the view model class we can now use this with any UI technology.

BenefitsofMVVM

Automated UI Unit testing: – View model class represents your UI. The properties of the class represent UI text boxes, combo boxes and the methods of the class represent action. Now as the UI is represented by the view model class, we can do automated UI testing using unit testing by creating the objects of view model class and doing the necessary asserts.

What is MVVM?

MVVM is an architecture pattern where we divide the project in to three logical layers and every layer has its own responsibility.

MVVM in WPF

MVVM in WPF

Below are the three logical layers with explanation what they do:-

  • View: – This layer handles responsibility of taking inputs from end user, positioning of controls, look and feel, design, visuals, colors etc.
  • Model: – This layer represents your middle layer objects like customer, supplier etc. It handles business logic and interaction with data access layer.
  • View Model: – View model as it says View plus model represents your user interface. You can also visualize this class as the behind code. This class is the bridge between model and view. It handles connection logic , data transformation logic and action mapping between model and view. For example it will have following types of logics as shown below :-
    • Replicating and propagating data between views and models. When someone enters data in to UI or the model gets updated this layer will ensure that the propagation of data happens between these entities.
    • Handling data transformation from view to model and vice versa. For example you have a model which has gender property with data as “M” for male and “F” for female. But on the View or UI you would like to display as a check box with true and false. This transformation logic is written in the view model class.
    • View model also map UI actions to methods. For example you have “btn_Add” click
      event and when any one clicks on this button you would like to invoke “Customer.Add()” method from the customer class. This connection code is again a part of view model.