【问题标题】:Using WCF DTO classes as Models in the UI在 UI 中使用 WCF DTO 类作为模型
【发布时间】:2012-02-12 11:49:50
【问题描述】:

我有以下 DTO 定义:-

[DataContract]
public class AddProductDTO
{
   [DataMember]
   public string Code { get; set; }
   [DataMember]
   public List<string> Categories { get; set; }
}

以及当前具有如下构造函数的视图模型:-

public AddProdctViewModel()
{
   Model = new AddProductDTO();
}

AddProductDTO 已作为服务引用直接添加到 VisualStudio 2010 中

在我看来,我有一个 (Xceed) WPF 数据网格,我希望能够在其中直接输入类别。目前它的 itemssource 设置为 Model.Categories。但是,这不起作用,因为 Model.Categories 值为 null。

这提出了几个问题,我希望能得到一些帮助

  1. 在 MVVM 中是否可以直接引用 WCF DTO 类作为模型,还是应该将其包装在其他东西中?
  2. 为什么在创建 AddProductDTO 时 Model.Categories 显示为 null?*
  3. 使用 WPF/WCF/MVVM 填充作为 DTO 一部分的列表的推荐方法是什么?

【问题讨论】:

    标签: wpf wcf mvvm datagrid prism


    【解决方案1】:

    我个人更喜欢将Model 类用于我的数据对象,并使用AutoMapper 之类的东西将DTO 映射到模型。这使我可以在 Model 对象上保留验证 (IDataErrorInfo) 和属性更改通知 (INotifyPropertyChanged) 等内容,而无需在往返 WCF 时包含该数据

    至于Model.Categoriesnull,通常我会在第一次调用属性的get方法时将列表对象设置为空白列表以避免此类问题。

    public class ProductModel : INotifyPropertyChanged
    {
        private List<string> _categories;
        public List<string> Categories 
        {
            get
            {
                if (_categories == null)
                    _categories = new List<string>();
    
                return _categories;
            }
        }
    }
    

    【讨论】:

    • 感谢您的建议,我已更改代码以反映这一点。但是,我仍然有想直接在数据网格中输入的问题。但是,似乎没有默认构造函数允许我这样做。我想我将不得不将 List 包装到另一个模型中,即 List 但是当它要做的只是包装一个字符串时,这似乎需要做很多工作.....
    • @lostinwpf 您应该能够直接在 DataGrid 中键入以编辑列表中的字符串。如果它不适合你,我建议发布另一个关于你的问题的问题,并确保包含你正在使用的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多