实体框架CTP5代码首先,WPF - MVVM建模 [英] Entity Framework CTP5 Code First, WPF - MVVM modeling
问题描述
public class Task
{
public int ID {get;组; }
public int Index {get;组; }
public string Content {get;组; }
public int Indentation {get;组; }
public DateTime Start {get;组; }
public decimal Effort {get;组; }
public decimal CompletedEffort {get;组; }
public decimal Cost {get;组; }
}
建立我的视图模型的建议方法是什么?我的视图模型将实现INotifyPropertyChanged,我不希望模型类具有任何UI特定的代码 - 以便它们可以轻松地在其他应用程序中重用。
我应该使所有的模型属性虚拟,然后在视图模型中覆盖它们? (看起来很多不必要的编码...)EF代码首先使用这种格式吗?
编辑
这是一个类似的问题在MVVM中,ViewModel或Model实现INotifyPropertyChanged?然而,唯一的解决方案似乎是将我认为是UI逻辑添加到模型中。也许我可以在模型中添加一些代理,并从viewmodel中添加一些代理,这将依次使用INotifyPropertyChanged ...这样的东西
public class Task
{
public delegate void HandleChange(string propertyName);
public HandleChange ChangeHandler;
public int ID
{
get
{
return ID;
}
set
{
if(ID!= value)
{
ID = value;
ChangeHandler(ID);
}
}
}
...
我正在做的是将我的模型类的一个实例放在ViewModel中的一个属性中,然后直接实现 INotifyPropertyChanged
模型属性和仅在Model实例上的ViewModel的模型,如下所示:
public class Task:INotifyPropertyChanged
{
//实现INotifyPropertyChanged
//在所有属性的Setter中提高PropertyChanged事件
}
public class TaskViewModel:INotifyPropertyChanged
{
private Task _task;
public Task Task
{
get
{
return _task;
}
set
{
if(_task!= value)
{
_task = value;
RaisePropertyChanged(Task);
}
}
}
// INotifyPropertyChanged实现
}
然后在XAML中,我直接绑定到模型属性,例如:
TextBox Text ={Binding Task.Content}/>
(TaskViewModel将在此为View的DataContext。)
我主要是为了避免你提到的这么多不必要的编码,我找不到一个缺点。 (我使我的模型持续使用EF Code-First。)
I have my model all setup for my WPF application and working with entity framework ctp5 code first, here's a sample model class:
public class Task
{
public int ID { get; set; }
public int Index { get; set; }
public string Content { get; set; }
public int Indentation { get; set; }
public DateTime Start { get; set; }
public decimal Effort { get; set; }
public decimal CompletedEffort { get; set; }
public decimal Cost { get; set; }
}
What would be the recommended way to build my view model? My view models will implement INotifyPropertyChanged, I do not want the model classes to have any UI specific code - so that they can be easily reused in other apps. Should I make all of the model properties virtual then override them in the view model? (seems like a lot of unnecessary coding...) Would EF code first play nice with this type of format?
Edit This is a somewhat similar question In MVVM should the ViewModel or Model implement INotifyPropertyChanged? however, the only solutions appear to be adding in what I consider to be UI logic into the model. Perhaps I can add some sort of delegate to the model and hook into that from the viewmodel, which will in turn use INotifyPropertyChanged... something like this?
public class Task
{
public delegate void HandleChange(string propertyName);
public HandleChange ChangeHandler;
public int ID
{
get
{
return ID;
}
set
{
if(ID != value)
{
ID = value;
ChangeHandler("ID");
}
}
}
...
What I am doing is to make an instance of my model class to a property in the ViewModel and then implement INotifyPropertyChanged
directly on the Model for the Model properties and on the ViewModel only for the Model instance, like so:
public class Task : INotifyPropertyChanged
{
// Implementation of INotifyPropertyChanged
// Raising the PropertyChanged event in the Setters of all properties
}
public class TaskViewModel : INotifyPropertyChanged
{
private Task _task;
public Task Task
{
get
{
return _task;
}
set
{
if (_task != value)
{
_task = value;
RaisePropertyChanged("Task");
}
}
}
// INotifyPropertyChanged implementation
}
Then in XAML I bind directly to Model properties, for instance:
<TextBox Text="{Binding Task.Content}" />
(TaskViewModel would be here the DataContext for the View.)
I do this mainly to avoid this "lot of unnecessary coding" that you mention, and I could not find a drawback. (I make my model persistent with EF Code-First too.)
这篇关于实体框架CTP5代码首先,WPF - MVVM建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!