简体   繁体   English

MVVM模型应该是什么样的?

[英]What should MVVM Model be like?

Hello i have 3 questions about MVVM Model. 您好,我有3个关于MVVM模型的问题。

  1. Isn't there any way to bypass that redundant PropertyChanged("PropName"); 没有任何方法可以绕过该冗余PropertyChanged("PropName");
  2. What is the best way to wrap POCO objects to WPF INotifyPropertyChanged, IDataErrorInfo 将POCO对象包装到WPF的最好方法是INotifyPropertyChanged, IDataErrorInfo
  3. How should i interact with (WPfWrapers - POCO) inside ViewModel - via casting, or property... 我应该如何通过铸造或属性与ViewModel内部的(WPfWrapers-POCO)交互...

Thanks. 谢谢。

Here are 3 answers: 这里有3个答案:

  1. You find alternatives of raising the PropertyChanged event without passing the “PropName” as string parameter in the .NET community . 您可以找到引发PropertyChanged事件而不在.NET社区中传递“ PropName”作为字符串参数的替代方法。 However, all of them have other drawbacks (eg performance). 但是,它们都有其他缺点(例如性能)。

  2. The best way is to implement INotifyPropertyChanged and IDataErrorInfo directly in the Model. 最好的方法是直接在模型中实现INotifyPropertyChanged和IDataErrorInfo。 That's not always possible. 并非总是可能的。 If you need to wrap you Model classes then you might have a look at the DataModel concept. 如果需要包装Model类,则可以看看DataModel概念。

  3. I'm not sure if I understand the last question right but here is an answer. 我不确定我是否理解最后一个问题,但这是一个答案。 The ViewModel or DataModel should interact with the Model directly. ViewModel或DataModel应该直接与Model交互。 But these classes shouldn't interact with the View in a direct way. 但是这些类不应直接与View交互。 Use interfaces (eg IView) for such scenarios. 对于此类情况,请使用接口(例如IView)。

More information can be found here: WPF Application Framework (WAF) 在此处可以找到更多信息: WPF应用程序框架(WAF)

  1. Yes, you can do this with a Lamdba expression. 是的,您可以使用Lamdba表达式执行此操作。 But this will cost some processor time (made some quick measurements: this approach is around 200 times slower than using the the string constant. Keep this in mind when using the expression on highly frequented POCOs): 但这将花费一些处理器时间(进行一些快速测量:此方法比使用字符串常量慢大约200倍。在频繁使用的POCO上使用表达式时,请记住这一点):

     private string ExtractPropertyName<T>( Expression<Func<T>> propertyExpresssion ) { if ( propertyExpresssion == null ) { throw new ArgumentNullException( "propertyExpresssion" ); } var memberExpression = propertyExpresssion.Body as MemberExpression; if ( memberExpression == null ) { throw new ArgumentException( "The expression is not a member access expression.", "propertyExpresssion" ); } var property = memberExpression.Member as PropertyInfo; if ( property == null ) { throw new ArgumentException( "The member access expression does not access a property.", "propertyExpresssion" ); } if ( !property.DeclaringType.IsAssignableFrom( this.GetType( ) ) ) { throw new ArgumentException( "The referenced property belongs to a different type.", "propertyExpresssion" ); } var getMethod = property.GetGetMethod( true ); if ( getMethod == null ) { // this shouldn't happen - the expression would reject the property before reaching this far throw new ArgumentException( "The referenced property does not have a get method.", "propertyExpresssion" ); } if ( getMethod.IsStatic ) { throw new ArgumentException( "The referenced property is a static property.", "propertyExpresssion" ); } return memberExpression.Member.Name; } private string myProperty; public string MyProperty { get { return myProperty; } set { myProperty = value; this.RaisePropertyChanged( ( ) => MyProperty ); } } protected void RaisePropertyChanged<T>( Expression<Func<T>> propertyExpression ) { var propertyName = ExtractPropertyName( propertyExpression ); this.RaisePropertyChanged( propertyName ); } 
  2. I think you don't have to wrap them (besides creating a coresponding view model). 我认为您不必包装它们(除了创建核心响应视图模型外)。 The POCOs are used as model and the interfaces are implemented by the viewmodel. POCO用作模型,接口由viewmodel实现。

  3. This question is obsolete when you don't wrap the POCOs. 当您不包装POCO时,这个问题就过时了。

您可以使用.NET 4.5名为“ CallerMemberName”的新功能来避免对属性名称进行硬编码。

On your first question: Have a look at this post 关于第一个问题: 看看这篇文章

And do a google search 做一个谷歌搜索

There are many ways (and discussions) 有很多方法(和讨论)

My 2 cents: 我的2美分:

First And second 第一第二

There is also the option to use Dependency properties in your viewmodel. 还可以选择在视图模型中使用Dependency属性。 Alot of people doesnt seem to like doing this because they are a part of wpf and have thread affinity (you can only call the dependency property method from the thread that created that perticular object 很多人似乎不喜欢这样做,因为它们是wpf的一部分并且具有线程亲和力(您只能从创建该垂直对象的线程中调用依赖属性方法

I personaly have never found this to be a problem since you're view is both dependent on wpf and has thread affinity anyway, so you even if INotifyPropertyChanged is used you still have to fire the PropertyChanged event from the correct thread. 我个人从来没有发现这是一个问题,因为您认为视图既依赖于wpf而且具有线程亲和力,因此即使使用INotifyPropertyChanged,您仍然必须从正确的线程中触发PropertyChanged事件。

Dependecy properties have built in notification support and does not require wpf to do any reflection, so they are faster for data binding (but slower to set/get albeit on a small time scale) Dependecy属性具有内置的通知支持,不需要wpf进行任何反射,因此它们用于数据绑定的速度较快(但设置/获取的时间较短,尽管时间较短)

your scenario might be diffrent than mine, but its someting to look at i think :) 您的情况可能与我的情况有所不同,但我认为这有点奇怪:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM