WPF/MVVM =>将域集合聚合到 ObservableCollections 中

发布于 2024-10-14 15:55:00 字数 924 浏览 0 评论 0原文

假设我急切地使用 NHibernate、EF 或任何您想要的 ORM 工具加载一些业务集合...

现在我有一个 IList,其中每个客户都有许多订单,每个订单有许多产品。

您的 CustomerRepository 中有一个 GetAllCustomer() 方法。

您在哪里以及如何将所有数据聚合到三个类型为 Customer、Order 和 Product 的 ObservableCollections 中

因为我需要添加/删除通知事件,所以

!?您是否真的在执行 customerRepo.GetAllCustomer() 的 BillingViewModel 中执行类似的操作:

BillingViewModel.cs


private ObservableCollection<Customer> _customersOC = new ObservableCollection<Customer>();

public BillingViewModel()
{

var customers = customerRepo.GetAllCustomer();
ConvertDomainToUICollections(customers);

}


private ConvertDomainToUICollections(IList<Customer> customers)
{

    foreach(Customer c in customers)
    {
        _customersOC.Add(c);
        foreach(Order o in c.Orders)
        {

            // Here I do not know how to proceed and put each in another OC<Order> etc...
        }

    }
}

Lets assume I load some business collections eagerly with NHibernate, EF or any ORM tool you wish...

Now I have an IList where each Customer has many Orders and each Order has many Products.

You have a GetAllCustomer() method in your CustomerRepository.

WHERE do you and HOW do you aggregate all your data into THREE ObservableCollections of type

Customer, Order and Product because I need add/delete notification events !?

Do you really do something like that in the BillingViewModel where you execute the customerRepo.GetAllCustomer():

BillingViewModel.cs


private ObservableCollection<Customer> _customersOC = new ObservableCollection<Customer>();

public BillingViewModel()
{

var customers = customerRepo.GetAllCustomer();
ConvertDomainToUICollections(customers);

}


private ConvertDomainToUICollections(IList<Customer> customers)
{

    foreach(Customer c in customers)
    {
        _customersOC.Add(c);
        foreach(Order o in c.Orders)
        {

            // Here I do not know how to proceed and put each in another OC<Order> etc...
        }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

埋葬我深情 2024-10-21 15:55:00

就我个人而言,我尝试仅在需要时并尽可能在“最低级别”聚合此类数据。

例如,在 BillingViewModel 中,我将为客户执行这样的聚合。但是,我不会在这里执行订单 - 而是在 CustomerViewModel 中处理此问题,因为我会尝试对其进行设置,以便编辑特定客户的订单。

如果您需要在非常高的级别上执行此操作,那么您将有效地重写整个模型。这是可以做到的,但它确实降低了数据的有用性和可维护性,因为您在非常高的级别上有效地维护了模型的两个完整版本。

话虽这么说,许多 ORM 可以更好地为您处理这个问题。例如,Lightspeed(我的最爱之一)只是让他们的集合自动实现INotifyCollectionChanged直接来自 ORM。这使得无需转换为 ObservableCollection - 您可以只使用从 ORM 返回的数据。

Personally, I try to aggregate data like this only when required, and at the "lowest level" possible.

For example, in the BillingViewModel, I'd do an aggregation like this for Customers. However, I wouldn't do Orders here - but rather, handle this within the CustomerViewModel, since I'd try to set it up so that I edit Orders on a specific Customer.

If you need to do this at the very high level, you'll effectively be rewriting your entire Model. This can be done, but it really reduces the usefulness and maintainability of your data, since you're effectively maintaining two full versions of the Model at a very high level.

That being said, many ORMs handle this much more nicely for you. For example, Lightspeed (one of my favorites) just makes their collections automatically implement INotifyCollectionChanged straight from the ORM. This makes it unnecessary to convert into ObservableCollection<T> - you can just use the data as returned from the ORM.

坦然微笑 2024-10-21 15:55:00

一种选择是创建代表域模型中所有三种类型的类(本质上是视图模型)。因此,您将创建一个 customerVM 类,它将具有 orderVM 的成员可观察集合,并且 orderVM 将具有 ProductVM 的成员可观察集合。然后,将域模型映射到 viewModel,这是数据的 UI 表示。

有些人发现为域和视图模型使用单独的类并且必须在它们之间来回映射是很痛苦的。不过我认为这正是使用数据绑定和 MVVM 的现实。一种选择可能是为域对象生成代理或包装器,另一种可能是在域对象本身上拥有通知接口。

链接到一个相关问题:
WPF/MVVM:委派域模型集合到 ViewModel

编辑:我刚刚意识到链接的问题也是您的问题。 :)

One option is to create classes (essentially viewmodels) that represent all three types in your domain model. So you would create a customerVM class, and it would have a member observable collection of orderVMs, and the orderVM would have a a member ovservable collection of productVMs. Then you map your domain model to your viewModels, which is the UI representation of the data.

Some people find it painful to have separate classes for the domain and the viewmodel and have to map back and forth between them. However I think it is just to reality of using databinding and MVVM. One option might be to generate proxies or wrappers for your domain objects, and another might be to have the Notifying interfaces on the domain objects themselves.

Link to a related question:
WPF/MVVM: Delegating a domain Model collection to a ViewModel

Edit: I just realized the linked questions was your question as well. :)

过度放纵 2024-10-21 15:55:00

You might be interested in the BookLibrary sample application of the WPF Application Framework (WAF). It's a WPF MVVM application which uses the Entity Framework. It solves exactly your issue by introducing a EntityObservableCollection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文