删除服务层中的实体:我应该 foreach 删除(T item)还是调用删除(Listlist)?

发布于 2024-10-10 16:56:20 字数 527 浏览 5 评论 0原文

我的用户可以删除 DataGrid 中的多个实体。 我现在应该在我的 customerService 类中创建一个 DeleteCustomer 方法

Presenter/ViewModel:

public void DeleteCustomers()
{
    foreach(...SelectedCustomers)
    {
        customerService.DeleteCustomer(item);
    }
}

OR

public void DeleteCustomers()
{
  customerService.DeleteCustomers(SelectedCustomers);
 // The DataProvider is enumerating the list...
}

你认为是什么更好的?

My user can delete multiple entities in a DataGrid.
Should I create now a DeleteCustomer method in my customerService class

like

Presenter/ViewModel:

public void DeleteCustomers()
{
    foreach(...SelectedCustomers)
    {
        customerService.DeleteCustomer(item);
    }
}

OR

public void DeleteCustomers()
{
  customerService.DeleteCustomers(SelectedCustomers);
 // The DataProvider is enumerating the list...
}

What do you think is better?

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

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

发布评论

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

评论(5

极度宠爱 2024-10-17 16:56:20

你自己已经回答了这个问题。在删除单个客户时,您可以调用:

customerService.DeleteCustomer(item);

这表明您正在将要删除的项目传递回服务(这里实际上是对如何处理客户的特定方式的抽象)。 您明确表明该服务最了解如何执行此操作。

为此,删除多个客户应该像后一个选项一样:

customerService.DeleteCustomers(SelectedCustomers);

可以获得一些好处:

  • 您在这里 再次承认该服务最了解如何执行此操作。也许服务(或它的抽象内容)可以以服务使用者不可用或立即显而易见的方式优化批处理操作。
  • 如果这是服务/WS/SOA 调用,那么最佳实践表明,与频繁的轻量调用相比,您应该更喜欢较少的“粗大”调用。

You've already answered the question yourself. In deleting a singular customer, you call:

customerService.DeleteCustomer(item);

This indicates that you are passing the items to be deleted back to the service (which here is really an abstraction over a specific way of how to handle customers). You are making a clear indication that the service has the best knowledge as to how to perform this operation.

To that end, deleting multiple customers should be like the latter option:

customerService.DeleteCustomers(SelectedCustomers);

You gain a few benefits here:

  • Your are again acknolwedging that the service knows best how to perform this operation. Perhaps the service (or whatever it's abstracting) can optimize the batch operation in ways that are not available or immediately obvious to consumers of the service.
  • If this is a service/WS/SOA call, then best practices indicate that you should prefer fewer "chunky" calls over frequent light calls.
躲猫猫 2024-10-17 16:56:20

DataProvider 是否有任何理由不应该枚举 IEnumerable?如果没有,就使用第二种方式,让 DataProvider 来处理。

Is there any reason why the DataProvider shouldn't enumerate the IEnumerable<T>? If not, just use the second way and let the DataProvider take care of it.

秋心╮凉 2024-10-17 16:56:20

我会选择第二个,因为它清楚地暴露了你想要做什么。

I'd go with the second one as that clearly exposes what you want to do.

撩动你心 2024-10-17 16:56:20

我会使用这两种方法...如果您只需要在某个时候删除单个客户,则必须将单个实例包装在 IEnumerable 中,这很烦人

I would have both methods... if you only need to delete a single customer at some point its annoying having to wrap a single instance in a IEnumerable

落花随流水 2024-10-17 16:56:20

通常在客户端-服务器场景中,您希望最大限度地减少往返次数。

显然这意味着在枚举集合和单独删除与立即删除集合之间,后者是更好的选择。

然而,作为替代方案,为了进一步减少通信,您可以将突变收集到一个集合(添加、删除、替换等...),并且仅当用户按下“保存”时,调用服务并发送过来一组变化。

Typically in a client-server scenario, you want to minimize the amount of round-trips.

Obviously this means that between enumerating the collection and deleting individually, and deleting the collection at once, the latter is the better choice.

However as an alternative, to reduce the communication even further, you could collect mutations to a collection (Add, Delete, Replace and so on...) and only when the user presses 'Save' for instance, call the service and send over a set of changes.

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