gpt4 book ai didi

wcf - 这篇 MSDN 文章是否违反了 MVVM?

转载 作者:行者123 更新时间:2023-12-03 07:26:05 24 4
gpt4 key购买 nike

这可能是旧新闻,但早在 2009 年 3 月,这篇文章“Model-View-ViewModel In Silverlight 2 Apps ,”有一个代码示例,其中包括 DataServiceEntityBase:

// COPIED FROM SILVERLIGHTCONTRIB Project for simplicity

/// <summary>
/// Base class for DataService Data Contract classes to implement
/// base functionality that is needed like INotifyPropertyChanged.
/// Add the base class in the partial class to add the implementation.
/// </summary>
public abstract class DataServiceEntityBase : INotifyPropertyChanged
{
/// <summary>
/// The handler for the registrants of the interface's event
/// </summary>
PropertyChangedEventHandler _propertyChangedHandler;

/// <summary>
/// Allow inheritors to fire the event more simply.
/// </summary>
/// <param name="propertyName"></param>
protected void FirePropertyChanged(string propertyName)
{
if (_propertyChangedHandler != null)
{
_propertyChangedHandler(this, new PropertyChangedEventArgs(propertyName));
}
}

#region INotifyPropertyChanged Members
/// <summary>
/// The interface used to notify changes on the entity.
/// </summary>
event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
{
add
{
_propertyChangedHandler += value;
}
remove
{
_propertyChangedHandler -= value;
}
}
#endregion

此类意味着开发人员打算将视觉效果直接绑定(bind)到数据(是的,使用了 ViewModel,但它定义了一个ObservableCollection 数据对象)。这种设计是否与 MVVM 的指导思想相差太远?现在我明白了为什么我们要这样做的一些原因:我们可以使用 DataServiceEntityBase 做这样的事情(这与 Entity Framework 密切相关):

// Partial Method to support the INotifyPropertyChanged interface
public partial class Game : DataServiceEntityBase
{
#region Partial Method INotifyPropertyChanged Implementation
// Override the Changed partial methods to implement the
// INotifyPropertyChanged interface

// This helps with the Model implementation to be a mostly
// DataBound implementation

partial void OnDeveloperChanged() { base.FirePropertyChanged("Developer"); }
partial void OnGenreChanged() { base.FirePropertyChanged("Genre"); }
partial void OnListPriceChanged() { base.FirePropertyChanged("ListPrice"); }
partial void OnListPriceCurrencyChanged() { base.FirePropertyChanged("ListPriceCurrency"); }
partial void OnPlayerInfoChanged() { base.FirePropertyChanged("PlayerInfo"); }
partial void OnProductDescriptionChanged() { base.FirePropertyChanged("ProductDescription"); }
partial void OnProductIDChanged() { base.FirePropertyChanged("ProductID"); }
partial void OnProductImageUrlChanged() { base.FirePropertyChanged("ProductImageUrl"); }
partial void OnProductNameChanged() { base.FirePropertyChanged("ProductName"); }
partial void OnProductTypeIDChanged() { base.FirePropertyChanged("ProductTypeID"); }
partial void OnPublisherChanged() { base.FirePropertyChanged("Publisher"); }
partial void OnRatingChanged() { base.FirePropertyChanged("Rating"); }
partial void OnRatingUrlChanged() { base.FirePropertyChanged("RatingUrl"); }
partial void OnReleaseDateChanged() { base.FirePropertyChanged("ReleaseDate"); }
partial void OnSystemNameChanged() { base.FirePropertyChanged("SystemName"); }
#endregion
}

当然,MSDN 代码可以被视为用于教育目的的“玩具代码”,但是有人在 Silverlight 开发的真实世界中做这样的事情吗?

最佳答案

为了使 View 完全独立于模型,您需要重现在许多情况下与 ViewModel 中的模型类型相同的类型。

示例

模型包含一个 Person 类型,该类型具有 FirstNameLastName 属性。视觉设计需要“人员列表”,因此有一个包含 ListBox 的 View ,该 ListBox 具有绑定(bind)到 FirstNameLastName 属性路径的数据模板。 ItemsSource 绑定(bind)到 ViewModel 的一个属性,该属性公开具有 FirstNameLastName 属性的类型的集合实例。

所以问题是,是否应该有 Model Person 类型的“ViewModel 版本”,还是 ViewModel 应该简单地重用现有的 Person从模型中输入?

无论哪种情况,您都很可能希望这些属性是可观察的。

考虑

MVVM 背后的目标是什么?我们经常喜欢列出一个长长的列表来说明模式存在的原因,但在本例中实际上只有 2 个。

  • 将视觉设计(注意:不是设计)与代码分开。
  • 最大化整个应用程序的可测试范围。

在 ViewModel 上公开模型类型不会对上述任一目标构成障碍。事实上,它有助于可测试性,因为需要测试的类型和成员的数量减少了。

在我看来,我不认为实现 INotifyPropertyChanged 意味着绑定(bind)到视觉效果。某些服务可能想要观察模型对象属性的变化可能还有其他原因。

将模型与 View 分离的关键原则是隐藏有关 View 如何从模型本身呈现模型的任何细节。向模型添加 ForenameBackColor 属性可能会很糟糕。这就是 ViewModel 的用武之地。

底线

要求模型公开可观察的属性并不违反 MVVM,它是一个简单且通用的要求,不要求模型具有任何 View 的任何特定知识,或者实际上根本不涉及任何“视觉效果”。

关于wcf - 这篇 MSDN 文章是否违反了 MVVM?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2488571/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com