简体   繁体   English

设计-按名称或对象删除?

[英]DESIGN — Remove by name or object?

Does it make more sense to remove an object using its name/id or passing the actual object? 使用对象的名称/ ID或传递实际对象删除对象更有意义吗? For example: 例如:

void MyList::remove(MyObject &myObject) { /* blah */ }
// or
void MyList::remove(std::string id) { /* blah */ }

I've used both but I can't really see what the advantages vs disadvantages. 我都用过,但是我真的看不到优点和缺点。 Is there a preferred standard? 有没有首选的标准?

EDIT: this would probably be a better example providing what I'm trying to do: 编辑:这可能是一个更好的例子,提供我正在尝试做的事情:

Let's say I have an Account class with a collection of Transactions. 假设我有一个带有交易集合的Account类。 Am I better to pass Transaction object or the id of the Tranaction? 我最好传递事务对象或交易ID吗?

class Account
{
  private List<Transaction> transaction = new List<Transaction>();

  public void Remove(Transaction transaction) { }
  // OR
  public void Remove(string name) { }
  // OR
  public void Remove(Guid id) { }
}

NOTE: this question has both C++ and C# code... 注意:此问题同时具有C ++和C#代码...

You may not have the item reference always. 您可能不总是有该项目的引用。 So it is better to have remove methods by name or id. 因此最好使用名称或ID的删除方法。 You can decide which one is preferable (name or id) according to your business requirement. 您可以根据您的业务需求确定哪个更可取(名称或ID)。 name or id has to be unique otherwise it will remove the wrong item. 名称或ID必须是唯一的,否则它将删除错误的项目。 So the business requirement has to decide it. 因此,业务需求必须决定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM