Entity Framework WPF 中的 POCO 类 和 ViewModel
Entity Framework POCO classes and ViewModels in WPF
如何解决数百个 POCO 模型没有实现 INotifyPropertyChanged 和其他 WPF 东西的问题,使用最有效的方式提供这些WPF的功能?
现在我使用 EntityFramework 和简单的 POCO classes 和 ViewModels 手写。
我的架构是这样的:
- 查看
- 视图模型
- 存储库模式
- WCF 存储库或 DB 存储库
- 业务逻辑
- Entity Framework
- POCO 模型 classes
我对此的看法是:
- 使用 Automapper 将 POCO classes 映射到 ViewModels classes 及之前手动创建这些 ViewModels。
- 使用 T4 生成基础 ViewModels 作为在 POCO [=77 之前生成的包装=]es,编写我自己的(或使用现有解决方案)实例解析器 class,以在 中提供相同的功能(一个实例 = 数据库中的一条记录) ]EF.
我很困惑,因为我不喜欢我自己的解决方案,它现在不稳定,但是Automapper在Reflection中使用映射。
怎么办?你知道一些很棒的、非常棒的工具来完成这些神奇的事情并让我可以灵活地添加和扩展 ViewModel 吗?
我相信你假设:
- 通常您会在 ViewModel 中创建模型对象的副本
- 通常您在 ViewModel 的每个对象中实现 INotifyPropertyChanged
我认为这两种假设都是错误的。
看下面的代码示例:
class Customer
{
public int ID {get; set;}
public string Name {get; set;}
}
class MyViewModel: INotifyPropertyChanged
{
// Hook you repository (model) anyway you like (Singletons, Dependency Injection, etc)
// For this sample I'm just crating a new one
MyRepository repo = new MyRepository();
public List<Customer> Customers
{
get { return repo.Customers;}
}
public void ReadCustomers()
{
repo.ReadCustomers();
InternalPropertyChanged("Customers");
}
public event PropertyChangedEventHandler PropertyChanged;
protected void InternalPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
class MyRepository
{
private List<Customer> customers;
public List<Customer> Customers
{
get { return customers; }
}
public void ReadCustomers()
{
// db is the Entity Framework Context
// In the real word I would use a separate DAL object
customers = db.Customers.ToList();
}
}
Customers 是 Entity Framework 返回的列表。
ViewModel 属性 Customers 是一个简单的直通 属性 指向模型 属性.
在此示例中,我没有在 Customer 中使用 INotifyPropertyChanged。
我知道只有当用户调用 ReadCustomers() 时才能修改 Customers 列表,因此我在其中调用了 PropertyChanged。
如果我需要为客户 class 触发 PropertyChanged 通知,我将直接在客户 class 上实施 INotifyPropertyChanged。
如何解决数百个 POCO 模型没有实现 INotifyPropertyChanged 和其他 WPF 东西的问题,使用最有效的方式提供这些WPF的功能?
现在我使用 EntityFramework 和简单的 POCO classes 和 ViewModels 手写。
我的架构是这样的:
- 查看
- 视图模型
- 存储库模式
- WCF 存储库或 DB 存储库
- 业务逻辑
- Entity Framework
- POCO 模型 classes
我对此的看法是:
- 使用 Automapper 将 POCO classes 映射到 ViewModels classes 及之前手动创建这些 ViewModels。
- 使用 T4 生成基础 ViewModels 作为在 POCO [=77 之前生成的包装=]es,编写我自己的(或使用现有解决方案)实例解析器 class,以在 中提供相同的功能(一个实例 = 数据库中的一条记录) ]EF.
我很困惑,因为我不喜欢我自己的解决方案,它现在不稳定,但是Automapper在Reflection中使用映射。
怎么办?你知道一些很棒的、非常棒的工具来完成这些神奇的事情并让我可以灵活地添加和扩展 ViewModel 吗?
我相信你假设:
- 通常您会在 ViewModel 中创建模型对象的副本
- 通常您在 ViewModel 的每个对象中实现 INotifyPropertyChanged
我认为这两种假设都是错误的。 看下面的代码示例:
class Customer
{
public int ID {get; set;}
public string Name {get; set;}
}
class MyViewModel: INotifyPropertyChanged
{
// Hook you repository (model) anyway you like (Singletons, Dependency Injection, etc)
// For this sample I'm just crating a new one
MyRepository repo = new MyRepository();
public List<Customer> Customers
{
get { return repo.Customers;}
}
public void ReadCustomers()
{
repo.ReadCustomers();
InternalPropertyChanged("Customers");
}
public event PropertyChangedEventHandler PropertyChanged;
protected void InternalPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
class MyRepository
{
private List<Customer> customers;
public List<Customer> Customers
{
get { return customers; }
}
public void ReadCustomers()
{
// db is the Entity Framework Context
// In the real word I would use a separate DAL object
customers = db.Customers.ToList();
}
}
Customers 是 Entity Framework 返回的列表。 ViewModel 属性 Customers 是一个简单的直通 属性 指向模型 属性.
在此示例中,我没有在 Customer 中使用 INotifyPropertyChanged。 我知道只有当用户调用 ReadCustomers() 时才能修改 Customers 列表,因此我在其中调用了 PropertyChanged。
如果我需要为客户 class 触发 PropertyChanged 通知,我将直接在客户 class 上实施 INotifyPropertyChanged。