代码之家  ›  专栏  ›  技术社区  ›  Budda

RIA:如何获得功能,而不是数据

  •  1
  • Budda  · 技术社区  · 15 年前

    在服务器端,我有以下类:

    public class Customer
    {
        [Key]
        public int Id { get; set; }
    
        public string FirstName { get; set; }
    
        public string SecondName { get; set; }
    
        public string FullName { get { return string.Concat(FirstName, " ", SecondName); } }
    }
    

    问题是每个字段都经过计算并传输到客户端(到Silvelight应用程序),例如“FullName”属性:

        [DataMember()]
        [Editable(false)]
        [ReadOnly(true)]
        public string FullName
        {
            get
            {
                return this._fullName;
            }
            set
            {
                if ((this._fullName != value))
                {
                    this.ValidateProperty("FullName", value);
                    this.OnFullNameChanging(value);
                    this._fullName = value;
                    this.RaisePropertyChanged("FullName");
                    this.OnFullNameChanged();
                }
            }
        }
    

    在没有手动复制属性实现的情况下,这是可能的吗?

    非常感谢。

    1 回复  |  直到 15 年前
        1
  •  0
  •   Chris Holmes    14 年前

    将计算出的属性作为部分类移动到不同的文件中,并利用“共享”命名转换(MyFileName.Shared.cs). 例子:

    //Employee.cs
    public partial class Employee
    {
       [Key]
        public string EmployeeId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    
    //Employee.Shared.cs
    public partial class Employee
    {
        public string LastNameFirst
        {
          get { return string.Format("{0}, {1}", LastName, FirstName); }
        }
    }
    

    共享文件中的代码将显示在客户端。

    推荐文章