【发布时间】:2010-03-21 20:21:43
【问题描述】:
这可能是旧闻,但早在 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
这个类意味着开发者打算将视觉效果直接绑定到数据(是的,使用了 ViewModel,但它定义了 ObservableCollection 的数据对象) .这种设计是否与 MVVM 的指导相差太远?现在我可以看到我们为什么要这样做的一些原因:我们可以用DataServiceEntityBase 做的是这种事情(与实体框架密切相关):
// 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 开发的真实世界中是否有人在做类似的事情?
【问题讨论】:
-
相当煽动性的标题 - 你有什么理由在同一句话中喷出某人的名字和“违反”这个词?
-
宅男:问题正文中明确说明了我的推理。
-
不,这不是针对某个人的原因。您是在专门呼唤一个人,因此可能会因为您自己的误解而抹黑他们。其次,Shawn 必须有一位编辑来批准所发布的内容,但我没有看到您因任何违规行为或特定编辑的姓名而指责 MSDN。最后,也是最重要的一点,如果您阅读了这篇文章,Shawn 特别指出 此示例不一定代表实际代码的使用方式。它只是为了解释模式而设计的。。我已将此帖子标记为具有攻击性。
-
伟大的工作。你在保护无辜者。
-
@rasx:尽管 AnthonyWJones 编辑了你的标题,但你似乎并不后悔。我,同样,正在标记攻击性。请淡化花言巧语并提出“好”的问题。我们不是来打架的。
标签: wcf silverlight entity-framework mvvm