作者:HikariFocus_695 | 来源:互联网 | 2023-05-17 17:36
IamtryingtounderstandhowWPFbindingworksinjunctionwithMVVMandEntityFramework.SofarI
I am trying to understand how WPF binding works in junction with MVVM and Entity Framework. So far I understand databinding as a concept related to properties. However, when it gets to EF I lose understanding of which objects to use to define the Model for database. For instance, I have a Model class for a Category:
我试图了解WPF绑定如何与MVVM和Entity Framework结合使用。到目前为止,我将数据绑定理解为与属性相关的概念。但是,当它到达EF时,我不了解用于定义数据库模型的对象。例如,我有一个类别的Model类:
public class Category : INotifyPropertyChanged
{
string _CategoryId;
public string CategoryId
{
get
{
return _CategoryId;
}
set
{
if (_CategoryId != value)
{
_CategoryId = value;
RaisePropertyChanged("CategoryId");
}
}
}
string _CategoryName;
public string CategoryName
{
get
{
return _CategoryName;
}
set
{
if (_CategoryName != value)
{
_CategoryName = value;
RaisePropertyChanged("CategoryName");
}
}
}
///
///
///
///
void RaisePropertyChanged(string prop)
{
if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
}
public event PropertyChangedEventHandler PropertyChanged;
}
and POCO version:
和POCO版本:
public class CategoryPoco
{
public CategoryPoco() { }
public int CategoryId { get; set; }
public string CategoryName { get; set; }
}
Properties of the non Poco class in my understanding can be then used in databinding. However, I also need to build the database context model:
然后,我理解的非Poco类的属性可用于数据绑定。但是,我还需要构建数据库上下文模型:
public DbSet Categories { get; set; }
Now here is where I lose my understanding, do I use Poco or non-Poco classes when building the context model?
现在我失去了理解,在构建上下文模型时是使用Poco还是非Poco类?
How do I match the two classes when I start exchanging data with the Database?
当我开始与数据库交换数据时,如何匹配这两个类?
1 个解决方案
You use the "POCO version" to build the context model for your database. If you will, POCO is just defined as
您可以使用“POCO版本”为数据库构建上下文模型。如果你愿意,POCO只是定义为
Plain Old CLR Object. Just a normal class, no attributes describing infrastructure concerns or other responsibilities that your domain objects shouldn't have.
普通的旧CLR对象。只是一个普通的类,没有描述基础结构问题的属性或域对象不应具有的其他责任。
so technically, your Category
is also considered as POCO. POCO doesn't have a different meaning when used with MVVM or EF. EF just uses those objects to map it back to the database.
从技术上讲,您的类别也被视为POCO。与MVVM或EF一起使用时,POCO没有不同的含义。 EF只是使用这些对象将其映射回数据库。
In your Category
class, I normally don't create another Model class just to have that INotifyPropertyChanged
. It's more flexible and clear that your Category
class should be called CategoryViewModel
.
在您的Category类中,我通常不会创建另一个Model类来获得该INotifyPropertyChanged。它更灵活,更清晰,你的Category类应该被称为CategoryViewModel。
If I read your code and I see an INotifyPropertyChanged
interface where WPF uses it also for DataBinding, then I would be against it as you are now using Model -> View pattern without the ViewModel as your middleman. (Assuming you use category as your binding source)
如果我读了你的代码并且我看到一个INTifyPropertyChanged接口,其中WPF也将它用于DataBinding,那么我会反对它,因为你现在使用Model - > View模式而没有ViewModel作为你的中间人。 (假设您使用类别作为绑定源)
If you decided that you need to extend Category
class, then I would suggest to use T4 template to generate your POCO Classes as a partial class, and create another partial class that implements INotifyPropertyChanged
or add more properties that are not in the columns of a given table e.g., CategoryStatus, CategoryDescription and mark those properties with [NotMapped] attribute.
如果您决定需要扩展Category类,那么我建议使用T4模板将POCO类生成为部分类,并创建另一个实现INotifyPropertyChanged的部分类或添加不在给定列中的更多属性表格,例如,CategoryStatus,CategoryDescription,并使用[NotMapped]属性标记这些属性。
That way you don't have to do matching between the two classes and mostly your Model
is already setup in the ViewModel
to communicate it back with EF. You also have the flexibility to add more functionality to the object, which complies with the Open-Closed
design principle.
这样您就不必在两个类之间进行匹配,并且大部分模型已经在ViewModel中设置,以便与EF进行通信。您还可以灵活地向对象添加更多功能,这符合开放式封闭设计原则。