如何使用最有效的方法为WPF提供这些功能,而无需实现INotifyPropertyChanged和其他WPF东西来解决数百个POCO模型的问题?
现在,我将EntityFramework与简单的POCO类和手工编写的ViewModels一起使用。
我的架构如下所示:
我的想法是:
我很困惑,因为我不喜欢自己的解决方案,它现在还不稳定,但是Automapper在映射中使用了反射。
该怎么办?您是否知道一些神奇的工具,真的是很棒的工具,可以做这些神奇的事情,并为我提供了添加和扩展ViewModel的灵活性?
我相信您会假设:
我相信这两个假设都是错误的。看下面的代码示例:
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();
}
}
客户是实体框架返回的列表。ViewModel属性“客户”是一个简单的传递属性,它指向Model属性。
在此示例中,我不在Customer内部使用INotifyPropertyChanged。我知道只有在用户调用ReadCustomers()时才能修改客户列表,因此在其中调用了PropertyChanged。
如果需要为Customer类触发PropertyChanged通知,则可以直接在Customer类上实现INotifyPropertyChanged。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句