Smart Client Software Factory 映射业务实体到界面元素

来源:互联网 发布:ubuntu 显示文件列表 编辑:程序博客网 时间:2024/05/12 10:53
下面做了一个例子,如下图,这个例子的主要目的是,把业务模型实体类映射到具体的UI控件上

上面在Module(自己创建的业务工程)右键添加一个实体类模型 Attachment

[csharp] view plaincopyprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace SmartClient.Module  
  7. {  
  8.     public class Attachment  
  9.     {  
  10.         public event EventHandler StatusChanged;  
  11.   
  12.         public enum AttachmentStatus  
  13.         {  
  14.             NotAvailable,  
  15.             Downloading,  
  16.             AvailableNotModified,  
  17.             AvailableModified,  
  18.             ToBeUploaded,  
  19.             Uploading,  
  20.             Uploaded  
  21.         }  
  22.   
  23.         private string _displayName;  
  24.   
  25.         public string DisplayName  
  26.         {  
  27.             get { return _displayName; }  
  28.             set { _displayName = value; }  
  29.         }  
  30.   
  31.         private string _fileName;  
  32.   
  33.         public string FileName  
  34.         {  
  35.             get { return _fileName; }  
  36.             set { _fileName = value; }  
  37.         }  
  38.   
  39.         private string _documentUrl;  
  40.   
  41.         public string DocumentUrl  
  42.         {  
  43.             get { return _documentUrl; }  
  44.             set { _documentUrl = value; }  
  45.         }  
  46.   
  47.         private AttachmentStatus _status;  
  48.   
  49.         public AttachmentStatus Status  
  50.         {  
  51.             get { return _status; }  
  52.             set  
  53.             {  
  54.                 _status = value;  
  55.             }  
  56.         }  
  57.   
  58.         private DateTime _localCreationTime;  
  59.   
  60.         public DateTime LocalCreationTime  
  61.         {  
  62.             get { return _localCreationTime; }  
  63.             set { _localCreationTime = value; }  
  64.         }  
  65.     }  
  66. }  
再添加一个映射器类,AttachmentMapper,它的作用是把这个业务实体绑定到ListView中

[csharp] view plaincopyprint?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Windows.Forms;  
  6.   
  7. namespace SmartClient.Module  
  8. {  
  9.     public static class AttachmentMapper  
  10.     {  
  11.         public static ListViewItem ToListViewItem(Attachment attachment)  
  12.         {  
  13.             return new AttachmentListViewItem(attachment);  
  14.         }  
  15.   
  16.         public static Attachment FromListViewItem(ListViewItem listViewItem)  
  17.         {  
  18.             AttachmentListViewItem attachmentListViewItem =  
  19.                             listViewItem as AttachmentListViewItem;  
  20.   
  21.             return attachmentListViewItem.Attachment;  
  22.         }  
  23.   
  24.         private class AttachmentListViewItem : ListViewItem  
  25.         {  
  26.             private Attachment _attachment;  
  27.   
  28.             public AttachmentListViewItem(Attachment attachment)  
  29.             {  
  30.                 Name = attachment.DisplayName;  
  31.                 Text = attachment.DisplayName;  
  32.                 SubItems.Add(AttachmentStatusToDisplayText(attachment));  
  33.                 _attachment = attachment;  
  34.             }  
  35.   
  36.             private static string AttachmentStatusToDisplayText(Attachment attachment)  
  37.             {  
  38.                 switch (attachment.Status)  
  39.                 {  
  40.                     case Attachment.AttachmentStatus.AvailableModified:  
  41.                         return "Modified";  
  42.   
  43.                     case Attachment.AttachmentStatus.AvailableNotModified:  
  44.                         return "Available";  
  45.   
  46.                     case Attachment.AttachmentStatus.Downloading:  
  47.                         return "Downloading...";  
  48.   
  49.                     case Attachment.AttachmentStatus.NotAvailable:  
  50.                         return "To be downloaded";  
  51.   
  52.                     case Attachment.AttachmentStatus.ToBeUploaded:  
  53.                         return "To be uploaded";  
  54.   
  55.                     case Attachment.AttachmentStatus.Uploaded:  
  56.                         return "Uploaded";  
  57.   
  58.                     case Attachment.AttachmentStatus.Uploading:  
  59.                         return "Uploading";  
  60.                 }  
  61.   
  62.                 return attachment.Status.ToString();  
  63.             }  
  64.   
  65.             public Attachment Attachment  
  66.             {  
  67.                 get { return _attachment; }  
  68.             }  
  69.         }  
  70.     }  
  71. }  
然后我们在View中,拖放一个ListView上去,并命名为_attachmentsListView,并修改其.cs代码

[csharp] view plaincopyprint?
  1. using System;  
  2. using System.Windows.Forms;  
  3. using Microsoft.Practices.CompositeUI.SmartParts;  
  4. using Microsoft.Practices.ObjectBuilder;  
  5. using SmartClient.Infrastructure.Interface;  
  6.   
  7. namespace SmartClient.Module  
  8. {  
  9.     public partial class View : UserControl, IView  
  10.     {  
  11.         public View()  
  12.         {  
  13.             InitializeComponent();  
  14.         }  
  15.   
  16.         protected override void OnLoad(EventArgs e)  
  17.         {  
  18.             _presenter.OnViewReady();  
  19.             base.OnLoad(e);  
  20.             //加入如下代码,在窗体加载时执行对ListView的绑定  
  21.             var attachment = new Attachment();  
  22.             attachment.DisplayName = "这是一个测试";  
  23.             AddAttachmentToList(attachment);  
  24.         }  
  25.   
  26.         private void AddAttachmentToList(Attachment attachment)  
  27.         {  
  28.             ListViewItem item = AttachmentMapper.ToListViewItem(attachment);  
  29.             _attachmentsListView.Items.Add(item);  
  30.         }  
  31.     }  
  32. }  
最后我们得到如下的界面,ListView已经绑定上我们测试的数据


0 0