删除服务层中的实体:我应该 foreach 删除(T item)还是调用删除(Listlist)?
我的用户可以删除 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你自己已经回答了这个问题。在删除单个客户时,您可以调用:
这表明您正在将要删除的项目传递回服务(这里实际上是对如何处理客户的特定方式的抽象)。 您明确表明该服务最了解如何执行此操作。
为此,删除多个客户应该像后一个选项一样:
可以获得一些好处:
You've already answered the question yourself. In deleting a singular customer, you call:
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:
You gain a few benefits here:
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.我会选择第二个,因为它清楚地暴露了你想要做什么。
I'd go with the second one as that clearly exposes what you want to do.
我会使用这两种方法...如果您只需要在某个时候删除单个客户,则必须将单个实例包装在 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
通常在客户端-服务器场景中,您希望最大限度地减少往返次数。
显然这意味着在枚举集合和单独删除与立即删除集合之间,后者是更好的选择。
然而,作为替代方案,为了进一步减少通信,您可以将突变收集到一个集合(添加、删除、替换等...),并且仅当用户按下“保存”时,调用服务并发送过来一组变化。
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.