Saturday, April 16, 2011

Printing in Silverlight 4.0 with MVVM


Microsoft Silverlight 4.0 supports Printing with the help of Printing API. The article will basically deals with step by step approach in achieving Print functionality with MVVM pattern.
  1. Start Visual Web Developer 2010 Express and create a Silverlight Application

    image

  2. Select Silverlight 4 from Silverlight version dropdown and click Ok

    image

  3. Create three folders, Model, ViewModel and Command. 

    image

  4. Right click on Model folder and create a class for model. Let name it as Employee.

    image

    and add the following properties

       1: public class Employee
       2: {
       3:     /// <summary>
       4:     /// Gets or sets Employee Id.
       5:     /// </summary>
       6:     public int Id { get; set; }
       7:  
       8:     /// <summary>
       9:     /// Gets or sets Employee's First name.
      10:     /// </summary>
      11:     public String FirstName { get; set; }
      12:  
      13:     /// <summary>
      14:     /// Gets or sets Employee's Last name.
      15:     /// </summary>
      16:     public String LastName { get; set; }
      17:  
      18:     /// <summary>
      19:     /// Gets or sets Location of Employee.
      20:     /// </summary>
      21:     public String Location { get; set; }
      22:  
      23:     /// <summary>
      24:     /// Gets Employee's full name.
      25:     /// </summary>
      26:     public String FullName { get { return FirstName + " " + LastName; } }
      27:  
      28:     /// <summary>
      29:     /// Returns a collection of dummy details
      30:     /// </summary>
      31:     /// <returns>Collection of Employee</returns>
      32:     public static Collection<Employee> GetEmployeeDetails()
      33:     {
      34:         Collection<Employee> empList = new Collection<Employee>() 
      35:         { 
      36:             new Employee() { Id=001, FirstName = "Amanda", 
      37:                 LastName = "Hartshorn", Location="United States" },
      38:             new Employee() { Id=002, FirstName = "Binu", 
      39:                 LastName = "Babu", Location="India" },
      40:             new Employee() { Id=003, FirstName = "Nihas", 
      41:                 LastName = "Alangaden", Location="India" },
      42:             new Employee() { Id=004, FirstName = "Parvathi", 
      43:                 LastName = "Mahesh", Location="United States" },
      44:             new Employee() { Id=005, FirstName = "Tony", 
      45:                 LastName = "Xavier", Location="India" }
      46:         };
      47:  
      48:         return empList;
      49:     }
      50: }

  5. Right click on ViewModel folder in the Silverlight project create a BaseViewModel class

    image


    and implement INotifyPropertyChanged interface as shown below




       1: public class BaseViewModel : INotifyPropertyChanged
       2: {
       3:     /// <summary>
       4:     /// This event occurs when a property value changes
       5:     /// </summary>
       6:     public event PropertyChangedEventHandler PropertyChanged;
       7:  
       8:     protected void OnPropertyChanged(string name)
       9:     {
      10:         if (PropertyChanged!= null)
      11:         {
      12:             PropertyChanged(this, new PropertyChangedEventArgs(name));
      13:         }
      14:     }
      15: }



  6. Create MainPageViewModel class inside ViewModel folder and Command class inside Command folder.

  7. Implement ICommand interface in Command class as shown below
     


       1: public class Command : ICommand
       2: {
       3:     private bool m_canExecuteCache;
       4:     private Action<object> m_executeAction;
       5:     private Func<object, bool> m_canExecute;
       6:  
       7:     /// <summary>
       8:     /// This event occurs when changes occur that 
       9:     /// affect whether the command should execute.
      10:     /// </summary>
      11:     public event EventHandler CanExecuteChanged;
      12:  
      13:     /// <summary>
      14:     /// Initializes a new instance of <see cref="Command"/> class.
      15:     /// </summary>
      16:     /// <param name="canExecute">
      17:     /// Encapsulates a method that has one 
      18:     /// parameter and returns a value of the type bool.
      19:     /// </param>
      20:     /// <param name="executeAction">
      21:     /// Encapsulates a method that takes a single 
      22:     /// parameter and does not return a value.
      23:     /// </param>
      24:     public Command(Func<object, bool> canExecute, Action<object> executeAction)
      25:     {
      26:         m_canExecute = canExecute;
      27:         m_executeAction = executeAction;
      28:     }
      29:  
      30:     /// <summary>
      31:     /// This method determines whether the 
      32:     /// command can execute in its current state.
      33:     /// </summary>
      34:     /// <param name="parameter">
      35:     /// Data used by the command. If the command does not 
      36:     /// require data to be passed, this object can be set to null.
      37:     /// </param>
      38:     /// <returns>
      39:     /// true if this command can be executed; otherwise, false.
      40:     /// </returns>
      41:     public bool CanExecute(object parameter)
      42:     {
      43:         bool enable = m_canExecute(parameter);
      44:  
      45:         if (m_canExecuteCache != enable)
      46:         {
      47:             m_canExecuteCache = enable;
      48:  
      49:             if (CanExecuteChanged != null)
      50:             {
      51:                 CanExecuteChanged(this, new EventArgs());
      52:             }
      53:         }
      54:  
      55:         return m_canExecuteCache;
      56:     }
      57:  
      58:     /// <summary>
      59:     /// This method will be called when the command is invoked.
      60:     /// </summary>
      61:     /// <param name="parameter">
      62:     /// Data used by the command. If the command does not 
      63:     /// require data to be passed, this object can be set to null.
      64:     /// </param>
      65:     public void Execute(object parameter)
      66:     {
      67:         m_executeAction(parameter);
      68:     }
      69: }

  8. Create a property for Print Command and another one for Employees collection in the view model. Create an instance of Command and pass the methods which handles can execute and print. 


       1: public class MainPageViewModel : BaseViewModel
       2: {
       3:     /// <summary>
       4:     /// Initializes a new instance of <see cref="MainPageViewModel"/> class.
       5:     /// </summary>
       6:     public MainPageViewModel()
       7:     {
       8:         //Gets dummy data from model
       9:         Employees = Employee.GetEmployeeDetails();
      10:         //Setting the Command to the property
      11:         PrintCommand = new Command.Command(CanPrint, Print);
      12:     }
      13:  
      14:     /// <summary>
      15:     /// Gets or sets a collection of Employee.
      16:     /// </summary>
      17:     public Collection<Employee> Employees { get; private set; }
      18:  
      19:     /// <summary>
      20:     /// Gets or sets the Print Command.
      21:     /// </summary>
      22:     public ICommand PrintCommand { get; private set; }
      23:  
      24:     #region Private Methods
      25:  
      26:     /// <summary>
      27:     /// Method which is called when the command is invoked.
      28:     /// </summary>
      29:     /// <param name="obj">Data used for Printing</param>
      30:     private void Print(object obj)
      31:     {
      32:         //Creates a new instance of the System.Windows.Printing.PrintDocument class.
      33:         PrintDocument document = new PrintDocument();
      34:  
      35:         //Event handler which fires when each page is printing.
      36:         document.PrintPage += (s, arg) => { arg.PageVisual = obj as UIElement; };
      37:  
      38:         // Starts the printing process for the specified 
      39:         // document by opening the print dialog box.
      40:         document.Print("Print");
      41:     }
      42:  
      43:     /// <summary>
      44:     /// Method which determines whether the 
      45:     /// command can execute in its current state.
      46:     /// </summary>
      47:     /// <param name="obj">Data used for Printing</param>
      48:     /// <returns>true if the command can be executed; otherwise, false</returns>
      49:     private bool CanPrint(object obj)
      50:     {
      51:         return true;
      52:     }
      53:  
      54:     #endregion
      55: }

  9. Build the Solution and import ViewModel namespace in MainPage.xaml as shown below


       1: xmlns:vm="clr-namespace:PrintApplication.ViewModel"

    Set the DataContext of the MainPage usercontrol.




       1: <UserControl.DataContext>
       2:     <vm:MainPageViewModel />
       3: </UserControl.DataContext>

  10. Add reference to System.Windows.Controls.Data.dll to the Silverlight project and

    image


    import System.Windows.Controls.Data as shown below for adding DataGrid Control to list the Employee details.


       1:  xmlns:data="clr-namespace:System.Windows.Controls; assembly=System.Windows.Controls.Data"

  11. Add a TextBlock for displaying Title, DataGrid for displaying the Employees Collection and a button for handling Print action in MainPage.xaml.



       1: <UserControl x:Class="PrintApplication.MainPage"
       2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       4:     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
       5:     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
       6:     xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
       7:     xmlns:vm="clr-namespace:PrintApplication.ViewModel" mc:Ignorable="d">
       8:     <UserControl.DataContext>
       9:         <vm:MainPageViewModel />
      10:     </UserControl.DataContext>
      11:     <Grid x:Name="LayoutRoot" Background="White" Height="270">
      12:         <Grid.RowDefinitions>
      13:             <RowDefinition Height="50" />
      14:             <RowDefinition />
      15:             <RowDefinition Height="50" />
      16:         </Grid.RowDefinitions>
      17:         <TextBlock x:Name="trbBlock" Text="Printing API Sample" FontSize="18" 
      18:                    Margin="3,10,3,14" HorizontalAlignment="Center" Height="26" />
      19:         <StackPanel x:Name="DisplayPanel" Grid.Row="1" HorizontalAlignment="Center">
      20:             <data:DataGrid ItemsSource="{Binding Path=Employees}" 
      21:                            AutoGenerateColumns="False">
      22:                 <data:DataGrid.Columns>
      23:                     <data:DataGridTextColumn Header="Id" Width="50"
      24:                                              Binding="{Binding Id}" />
      25:                     <data:DataGridTextColumn Header="First Name" Width="100"
      26:                                              Binding="{Binding FirstName}" />
      27:                     <data:DataGridTextColumn Header="Last Name" Width="100"
      28:                                              Binding="{Binding LastName}" />
      29:                     <data:DataGridTextColumn Header="Location" Width="100"
      30:                                              Binding="{Binding Location}" />
      31:                 </data:DataGrid.Columns>
      32:             </data:DataGrid>
      33:         </StackPanel>
      34:         <Button Content="Print" Grid.Row="2" Width="100" Height="30" 
      35:                 Command="{Binding PrintCommand}" 
      36:                 CommandParameter="{Binding ElementName=DisplayPanel}" />
      37:     </Grid>
      38: </UserControl>



  12. Run the solution and click the Print button to get the Print dialog box.  

    image


Thanks
Bimal

Monday, March 14, 2011

Testing MVC 3 Applications with xUnit.net


Microsoft Visual Web Developer Express edition doesn’t support MS Test. But we can create test with the help of xUnit.net testing framework mainly for MVC Web Applications.

  1. Start Visual Web Developer 2010 Express and create a MVC 3.0 Web Application

    image

  2. Select Internet Application project template and check the Create a unit test project. By default the test project name will be <Project Name>.Tests and the test framework will be xUnit.net.

    image

    The project structure will be as shown below

    image

  3. Right click on Controllers folder and select  Controller from Add menu.

    image

  4. Set some text to the Message property of ViewBag as shown in line number 12

       1: using System.Web.Mvc;
       2:  
       3: namespace SampleMVCWithxUnit.Controllers
       4: {
       5:     public class EmployeeController : Controller
       6:     {
       7:         //
       8:         // GET: /Employee/
       9:  
      10:         public ActionResult Index()
      11:         {
      12:             ViewBag.Message = "My xUnit.net test";
      13:             return View();
      14:         }
      15:  
      16:         //
      17:         // GET: /Employee/Details/5
      18:  
      19:         public ActionResult Details(int id)
      20:         {
      21:             return View();
      22:         }
      23:  
      24:         //
      25:         // GET: /Employee/Create
      26:  
      27:         public ActionResult Create()
      28:         {
      29:             return View();
      30:         } 
      31:  
      32:         //
      33:         // POST: /Employee/Create
      34:  
      35:         [HttpPost]
      36:         public ActionResult Create(FormCollection collection)
      37:         {
      38:             try
      39:             {
      40:                 // TODO: Add insert logic here
      41:  
      42:                 return RedirectToAction("Index");
      43:             }
      44:             catch
      45:             {
      46:                 return View();
      47:             }
      48:         }
      49:         
      50:         //
      51:         // GET: /Employee/Edit/5
      52:  
      53:         public ActionResult Edit(int id)
      54:         {
      55:             return View();
      56:         }
      57:  
      58:         //
      59:         // POST: /Employee/Edit/5
      60:  
      61:         [HttpPost]
      62:         public ActionResult Edit(int id, FormCollection collection)
      63:         {
      64:             try
      65:             {
      66:                 // TODO: Add update logic here
      67:  
      68:                 return RedirectToAction("Index");
      69:             }
      70:             catch
      71:             {
      72:                 return View();
      73:             }
      74:         }
      75:  
      76:         //
      77:         // GET: /Employee/Delete/5
      78:  
      79:         public ActionResult Delete(int id)
      80:         {
      81:             return View();
      82:         }
      83:  
      84:         //
      85:         // POST: /Employee/Delete/5
      86:  
      87:         [HttpPost]
      88:         public ActionResult Delete(int id, FormCollection collection)
      89:         {
      90:             try
      91:             {
      92:                 // TODO: Add delete logic here
      93:  
      94:                 return RedirectToAction("Index");
      95:             }
      96:             catch
      97:             {
      98:                 return View();
      99:             }
     100:         }
     101:     }
     102: }



  5. Create a class in the Test project and name it EmployeeControllerFacts.cs which will hold the test cases for Employee Controller.

    image


  6. Build the test project and open the xUnit test runner from the installation folder. Add the test project dll with the help of Open menu from Assembly toolbar.

    image


  7. Click the Run Selected button to execute the test methods

    image


    Thanks,
    Bimal

Thursday, March 10, 2011

Unit Testing with XUnit.net 1.7


Introduction

xUnit.net is a testing framework which is simple to use for .net developers. It comes with a testing framework library and Test runners for .net 2.0 and 4.0.

Implementation

  1. Download the xUnit.net framework from here and install it.

    image

  2. Add a Visual Studio Class Library Project and name it as Calculate.Test

    image

  3. Add reference to the Project that need to test.
  4. Add reference to xUnit dll. The solution structure will looks like this.  

    image

  5. Open CalculateTest.cs file write a test method for Add method in the Calculate Project.

    Let’s try for a fail case

    image

  6. Open xUnit.net Test Runner application from installation folder

    image

  7. Select Open from Assembly menu and add the test dll i.e Calculate.Test.dll which will be displayed in xUnit.net Test Runner as shown below

    image

  8. Click the Run All button to execute the test methods and the results will be displayed as shown below. 

    image 

  9. Modify the test case as shown below to fix the test case

    image

  10. Run the test will give the green signal.

    image

Conclusion

xUnit.net is a easy to use framework for .net developers.

Thanks,
Bimal

Tuesday, March 08, 2011

Unit Testing with NUnit 2.5.9


Introduction

NUnit is a xunit based unit-testing framework written in C# for Microsoft.Net. It comes with a testing framework assembly and a test runner for displaying the test results.

Implementation

  1. Download NUnit framework from here and install it.

    image

  2. Add a Visual Studio Class Library Project and name it as Calculate.Test.

    image

  3. Add reference to the Project that need to test.
  4. Add reference to NUnit framework

    image

    The Solution structure will looks like this

    image

  5. Open CalculateTest.cs file and decorate the public class with TestFixture attribute. The TextFixture attribute compiled into an assembly when we build the project.

    Let’s try for a fail case

    image

  6. Run NUnit runner application from Windows Program Menu

    image

  7. Select Open Project from File menu and load the Calculate.Test.dll. The test runner will load the assembly and using reflection API’s looks for any type that has a TestFixture attribute on it. Once the test runner identifies the TestFixture class, it will create an instance of that class and executes all the methods decorated with Test attribute.

    Click the Run button to see the result.

    image

  8. Modify the test case as shown below to fix the test case.

    image

  9. Run the test will give the green signal.

    image

Thanks,
Bimal

.Net Unit Testing Frameworks