I am writing a desktop application in C# using the MVVM pattern with an Entity Framework model. I tend to use DependencyProperties in my VM's and have come to (generally) prefer this system over implementing INotifyPropertyChanged. I would like to keep things consistent. My VM accesses the Entities in the Model and I have managed to keep things pretty separate - the View has no knowlege of the VM except for binding and command names and the Model has know knowlege of the VM.
我正在使用带有Entity Framework模型的MVVM模式在C#中编写桌面应用程序。我倾向于在我的VM中使用DependencyProperties并且(通常)更喜欢这个系统而不是实现INotifyPropertyChanged。我想保持一致。我的虚拟机访问了模型中的实体,我设法将事情分开 - 视图除了绑定和命令名称之外没有知道虚拟机,并且模型知道虚拟机的知识。
Using INotifyPropertyChanged in the VM, it seems pretty easy to update the Entities in the Model:
在VM中使用INotifyPropertyChanged,更新模型中的实体似乎非常容易:
public string Forename
{
get { return CurrentPerson.Forename; }
set
{
if (Forename != value)
{
CurrentPerson.Forename = value;
NotifyPropertyChanged("Forename");
}
}
}
...where CurrentPerson is a Person object auto-created by the Entity Data Model. There is therefore no private field created specifically to store the Forename.
...其中CurrentPerson是由实体数据模型自动创建的Person对象。因此,没有专门用于存储Forename的私有字段。
With DependencyProperties, it appears that I would have to create a DP, add the default Property, using GetValue and Setvalue and then use the PropertyChangedCallback in order to update the CurrentPerson Entity. Calling a callback in this situation appears to be adding overhead for the sake of being consistent with my other VM's.
使用DependencyProperties,我似乎必须使用GetValue和Setvalue创建DP,添加默认属性,然后使用PropertyChangedCallback来更新CurrentPerson实体。在这种情况下调用回调似乎增加了开销,以便与我的其他VM保持一致。
The question is therefore whether one or other of these methods is the way I should do things? In this instance, should I use a DependencyProperty or INotifyPropertyChanged? One thing that should be pointed out is that this will potentially be a very large scale project (with plugins and a lot of database accesses from different machines) and that everything really should be as reusable, and the modules as "disconnected", as possible.
因此,问题是这些方法中的一种或另一种是我应该做的事情吗?在这个例子中,我应该使用DependencyProperty还是INotifyPropertyChanged?应该指出的一点是,这可能是一个非常大规模的项目(插件和来自不同机器的大量数据库访问),并且所有内容都应该是可重用的,并且模块尽可能“断开连接” 。
1 个解决方案
#1
2
I would recommend using INotifyPropertyChanged instead of DependencyProperty. The main reason I stay away from DependencyProperty in ViewModels is because the DependencyProperty is located in WindowsBase.dll. This ties you to the Windows UI a bit too much (at least IMHO).
我建议使用INotifyPropertyChanged而不是DependencyProperty。我在ViewModels中远离DependencyProperty的主要原因是因为DependencyProperty位于WindowsBase.dll中。这有点太多关联Windows UI(至少恕我直言)。
Using the INotifyPropertyChanged is a lot easier to maintain since it allows the various plugins to implement it the way they want. If you force Dependency Properties, all viewmodels need to inherit from DependencyObject.
使用INotifyPropertyChanged更容易维护,因为它允许各种插件以他们想要的方式实现它。如果强制依赖属性,则所有视图模型都需要从DependencyObject继承。
See this article for more details about the use of INotifyPropertyChanged and DependencyProperty: http://kentb.blogspot.com/2009/03/view-models-pocos-versus.html
有关使用INotifyPropertyChanged和DependencyProperty的更多详细信息,请参阅此文章:http://kentb.blogspot.com/2009/03/view-models-pocos-versus.html
Another supporting answer: https://*.com/a/783154/27669
另一个支持答案:https://*.com/a/783154/27669
#1
2
I would recommend using INotifyPropertyChanged instead of DependencyProperty. The main reason I stay away from DependencyProperty in ViewModels is because the DependencyProperty is located in WindowsBase.dll. This ties you to the Windows UI a bit too much (at least IMHO).
我建议使用INotifyPropertyChanged而不是DependencyProperty。我在ViewModels中远离DependencyProperty的主要原因是因为DependencyProperty位于WindowsBase.dll中。这有点太多关联Windows UI(至少恕我直言)。
Using the INotifyPropertyChanged is a lot easier to maintain since it allows the various plugins to implement it the way they want. If you force Dependency Properties, all viewmodels need to inherit from DependencyObject.
使用INotifyPropertyChanged更容易维护,因为它允许各种插件以他们想要的方式实现它。如果强制依赖属性,则所有视图模型都需要从DependencyObject继承。
See this article for more details about the use of INotifyPropertyChanged and DependencyProperty: http://kentb.blogspot.com/2009/03/view-models-pocos-versus.html
有关使用INotifyPropertyChanged和DependencyProperty的更多详细信息,请参阅此文章:http://kentb.blogspot.com/2009/03/view-models-pocos-versus.html
Another supporting answer: https://*.com/a/783154/27669
另一个支持答案:https://*.com/a/783154/27669