博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

This topic describes how to convert a business entity into a form for display in your view.

Collapse imagePrerequisites

The steps in this topic assume that you have an existing smart client application that contains one module with a single view. You can use the Smart Client Development Package guidance package to quickly create the starting point for this how to topic.

To prepare a solution for this topic

  1. Install the Smart Client Development Package. For information about how to install this guidance package, see Guidance Automation.
  2. Use the Visual Studio template Smart Client Application to create the initial smart client solution. For information about how to create a solution with this template, see How to: Create Smart Client Solutions.
  3. Use the Add Business Module template to add a module to your project. Name the module MyModule. For information about how to create a business module, see How to: Create a Business Module.
  4. Use the Add View (with presenter) Recipe to create a view in the MyModule module. Enter MyView for the name of the view. For information about how to create a view, see How to: Add a View with a Presenter.

 

Collapse imageSteps

You will create the initial smart client solution in the following stages:

  1. Create the business entity class in the module.
  2. Add a ListView control to the view.
  3. Create a class that maps the business entity to a ListViewItem.
  4. Add code to the view to display the business entity in a ListView.

 

Your first task is to create a business entity object.

To create the business entity

  1. In Solution Explorer, right-click MyModule, point to Add, and then click Class. The Add New Item dialog box appears.
  2. Enter Attachment.cs as the name of the file.
  3. Click OK. Visual Studio creates the file and displays the Attachment class.
  4. Replace the contents of the Attachment class with the following code.
    C# 
    public class Attachment
                    {
                    public event EventHandler StatusChanged;
                    public enum AttachmentStatus
                    {
                    NotAvailable,
                    Downloading,
                    AvailableNotModified,
                    AvailableModified,
                    ToBeUploaded,
                    Uploading,
                    Uploaded
                    }
                    private string _displayName;
                    public string DisplayName
                    {
                    get { return _displayName; }
                    set { _displayName = value; }
                    }
                    private string _fileName;
                    public string FileName
                    {
                    get { return _fileName; }
                    set { _fileName = value; }
                    }
                    private string _documentUrl;
                    public string DocumentUrl
                    {
                    get { return _documentUrl; }
                    set { _documentUrl = value; }
                    }
                    private AttachmentStatus _status;
                    public AttachmentStatus Status
                    {
                    get { return _status; }
                    set
                    {
                    _status = value;
                    }
                    }
                    private DateTime _localCreationTime;
                    public DateTime LocalCreationTime
                    {
                    get { return _localCreationTime; }
                    set { _localCreationTime = value; }
                    }
                    }
                    
  5. Save the file.

 

Next, add a ListView control to the view MyView.

To add the ListView control

  1. Open the view MyView in the Visual Studio Designer. To do this, right-click MyView.cs in Solution Explorer, and then click View Designer.
  2. Open the Visual Studio Toolbox.
  3. Drag a ListView control onto the design surface. Change the name of the ListView control to _attachmentsListView.

 

Create a class that maps on object of type Attachment to an object of type ListViewItem.

Note:
The following procedure uses "rootnamespace" to refer to the root namespace that you used when you created your smart client solution. Replace "rootnamespace" with your application's root namespace.

 

To create the class that maps the business entity into a ListViewItem

  1. In Solution Explorer, right-click MyModule, point to Add, and then click Class. The Add New Item dialog box appears.
  2. Enter AttachmentMapper.cs as the name of the file.
  3. Click OK. Visual Studio creates the file and displays the AttachmentMapper class.
  4. Replace the contents of the AttachmentMapper.cs file with the following code.
    C# 
    using System;
                    using System.Windows.Forms;
                    namespace rootnamespace.MyModule
                    {
                    public static class AttachmentMapper
                    {
                    public static ListViewItem ToListViewItem(Attachment attachment)
                    {
                    return new AttachmentListViewItem(attachment);
                    }
                    public static Attachment FromListViewItem(ListViewItem listViewItem)
                    {
                    AttachmentListViewItem attachmentListViewItem =
                    listViewItem as AttachmentListViewItem;
                    return attachmentListViewItem.Attachment;
                    }
                    private class AttachmentListViewItem : ListViewItem
                    {
                    private Attachment _attachment;
                    public AttachmentListViewItem(Attachment attachment)
                    {
                    Name = attachment.DisplayName;
                    Text = attachment.DisplayName;
                    SubItems.Add(AttachmentStatusToDisplayText(attachment));
                    _attachment = attachment;
                    }
                    private static string AttachmentStatusToDisplayText(Attachment attachment)
                    {
                    switch (attachment.Status)
                    {
                    case Attachment.AttachmentStatus.AvailableModified:
                    return "Modified";
                    case Attachment.AttachmentStatus.AvailableNotModified:
                    return "Available";
                    case Attachment.AttachmentStatus.Downloading:
                    return "Downloading...";
                    case Attachment.AttachmentStatus.NotAvailable:
                    return "To be downloaded";
                    case Attachment.AttachmentStatus.ToBeUploaded:
                    return "To be uploaded";
                    case Attachment.AttachmentStatus.Uploaded:
                    return "Uploaded";
                    case Attachment.AttachmentStatus.Uploading:
                    return "Uploading";
                    }
                    return attachment.Status.ToString();
                    }
                    public Attachment Attachment
                    {
                    get { return _attachment; }
                    }
                    }
                    }
                    }
                    
  5. Save the file.

 

The final task is to use the AttachmentMapper class to create a ListViewItem from the Attachment business entity.

To create a ListViewItem from the Attachment business entity

  1. Open the MyView view source code. To do this, right-click MyView.cs in Solution Explorer, and then click View Code.
  2. Add a method to create a ListViewItem from an Attachment business entity. Add the ListViewItem to the ListView, as shown in the following code.
    C# 
    private void AddAttachmentToList(Attachment attachment)
                    {
                    ListViewItem item = AttachmentMapper.ToListViewItem(attachment);
                    _attachmentsListView.Items.Add(item);
                    }
                    
  3. Save the file.

 

Collapse imageOutcome

At the conclusion of this How-to topic, you will have the following elements:

MyView
class. This is a view that contains a ListView control.

  • Attachment class. This is a business entity class.
  • AttachmentMapper class. This is the class that maps the Attachment business entity to a ListViewItem.