Posted on 2007-10-11 18:49
江南白衣 阅读(128)
评论(0) 编辑 收藏
This topic describes how to convert a business entity into a form for display in your view.
Prerequisites
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
- Install the Smart Client Development Package. For information about how to install this guidance package, see Guidance Automation.
- 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.
- 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.
- 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.
Steps
You will create the initial smart client solution in the following stages:
- Create the business entity class in the module.
- Add a ListView control to the view.
- Create a class that maps the business entity to a ListViewItem.
- 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
- In Solution Explorer, right-click MyModule, point to Add, and then click Class. The Add New Item dialog box appears.
- Enter Attachment.cs as the name of the file.
- Click OK. Visual Studio creates the file and displays the Attachment class.
- 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; }
}
}
|
- Save the file.
Next, add a ListView control to the view MyView.
To add the ListView control
- Open the view MyView in the Visual Studio Designer. To do this, right-click MyView.cs in Solution Explorer, and then click View Designer.
- Open the Visual Studio Toolbox.
- 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
- In Solution Explorer, right-click MyModule, point to Add, and then click Class. The Add New Item dialog box appears.
- Enter AttachmentMapper.cs as the name of the file.
- Click OK. Visual Studio creates the file and displays the AttachmentMapper class.
- 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; }
}
}
}
}
|
- 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
- Open the MyView view source code. To do this, right-click MyView.cs in Solution Explorer, and then click View Code.
- 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);
}
|
- Save the file.
Outcome
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.