silverlight   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了wcf – 这篇MSDN文章是否违反了MVVM?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

这可能是旧闻,但早在2009年3月,本文“ Model-View-ViewModel In Silverlight 2 Apps”就有一个包含DataserviceEntityBase的代码示例: // COPIED FROM SILVERLightcONTRIB Project for simplicity /// <sumMary> /// Base class for Dataservic
@H_674_6@
@H_674_6@
这可能是旧闻,但早在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 property@R_874_8313@
{
  if (_propertyChangedHandler != null)
  {
    _propertyChangedHandler(this,new PropertyChangedEventArgs(property@R_874_8313@);
  }
}

#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做什么就是这种事情(与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完全独立于Model,您需要重现类型,这些类型在很多情况下viewmodel中的Model类型相同.

模型包含具有FirstName和LastName属性的Person类型.视觉设计需要“人员列表”,因此有一个包含ListBox的View,该ListBox具有绑定到FirstName和LastName的属性路径的数据模板. Itemssource绑定到viewmodel的一个属性,该属性公开具有FirstName和LastName属性的类型的集合实例.

所以这里有一个问题,如果有@L_816_1@model Person类型的“viewmodel版本”,或者viewmodel是否应该只重用模型中现有的Person类型?

在任何一种情况下,你很可能希望这些属性是可观察的.

@H_778_19@mVVM背后的目标是什么?我们常常喜欢提供一个很好的长列表,列出模式存在的原因,但在这种情况下,实际上只有2个.

>从代码中分离视觉设计(注意:不设计).
>最大化整个应用程序的可测试表面.

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

在我看来,我没有看到实现INotifyPropertyChanged意味着绑定到视觉效果.某些服务可能希望观察模型对象属性的变化可能还有其他原因.

模型与视图分离的关键原则是隐藏有关视图如何从模型本身呈现模型的任何细节.将ForenameBACkColor属性添加到模型可能会很糟糕.这就是viewmodel的用武之地.

底线

要求模型公开可观察属性并不违反MVVM,它是一个简单而通用的要求,不要求模型具有任何View的任何特定知识,或者确实存在任何“视觉”.

大佬总结

以上是大佬教程为你收集整理的wcf – 这篇MSDN文章是否违反了MVVM?全部内容,希望文章能够帮你解决wcf – 这篇MSDN文章是否违反了MVVM?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
猜你在找的silverlight相关文章