我应该将列表传递给此方法还是一个接一个地传递?
我目前正在通过一个小应用程序进行 TDD,并遇到了一些我不知道如何解决的问题。该应用程序可以让人们选择他们认为会在某个位置完成比赛的车手。该页面将在 ASP.NET 中构建。
我的测试:
[Test]
public void SubmitPrediction()
{
Prediction prediction = new Prediction();
Dictionary<int, string> selectedDrivers = new Dictionary<int, string>()
{
{1, "Michael Schumacher"},
{2, "Jensen Button"}
};
prediction.SubmitPrediction(selectedDrivers);
}
现在,我传递给 SubmitPrediction()
selectedDrivers,但是,我也可以传递位置(整数),并且driverId(也是 int,可能会被一个名为 Driver 的对象替换,该对象将包含与驱动程序关联的所有内容):
prediction.SubmitPrediction(1, "Michael Schumacher");
可能会是:
prediction.SubmitPrediction(1, driver);
我想知道哪种方法更好。将驱动程序和位置封装在一个类中可能会更好,但我无法想出一个像样的名称。预测已经意味着它有一堆选定的车手和他们的位置。然后我可以把字典做成一个列表。
prediction.SubmitPrediction(SomeEncapsulatedClassHere);
I'm currently TDD'ing my way through a little application, and came across something I don't know how to solve. The application lets people select drivers they think will finish at a certain position. The page will be built in ASP.NET.
My test:
[Test]
public void SubmitPrediction()
{
Prediction prediction = new Prediction();
Dictionary<int, string> selectedDrivers = new Dictionary<int, string>()
{
{1, "Michael Schumacher"},
{2, "Jensen Button"}
};
prediction.SubmitPrediction(selectedDrivers);
}
For now, I'm passing to SubmitPrediction()
selectedDrivers, but, I can also pass the position (integer), and the driverId (also int, will probably be replaced with an object called Driver which will have everything associated with a driver):
prediction.SubmitPrediction(1, "Michael Schumacher");
Will probably be:
prediction.SubmitPrediction(1, driver);
What I am wondering is which is the better approach. It might even be better to encapsulate the driver and the position in one class, but I can't come up with a decent name. Prediction is already taken in the meaning that it has a bunch of selected drivers and their positions. Then I could make the Dictionary a List.
prediction.SubmitPrediction(SomeEncapsulatedClassHere);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据标题中的问题,如果您已经在考虑这样的问题,那么我建议您通过一个列表。列表可能只包含一个项目,因此它可以很好地代替一对一的方法,反之则需要更改您的接口(或类签名)。
我也参与封装驱动程序并在一类中担任职位。名字? DriverPlacement 怎么样?
PS:对一切都持保留态度,因为我不知道你的背景
As per the question in the title, if you already contemplating such a question, then I would suggest passing a list. A list may contain only one item so it will work well in place of one-by-one method, the other way around will require changing your interfaces (or class signatures).
I'm also on board with encapsulating driver and the position in one class. The name? How about DriverPlacement?
PS: take everything with a grain of salt as I don't know your context
这取决于您的要求。我认为,没有适用于所有情况的直接答案,尤其是对于像参数类型决策这样的问题。您应该了解您正在尝试解决的真正问题,存在于代码问题背后。不要把漂亮的设计作为你的目标。我非常擅长紧急设计。形式服从功能。您的目标是在没有错误的情况下实现您的场景。好的设计会有更少的错误,但如果你觉得自己没有足够的经验,那就尝试一下,测试不同的解决方案。有时,一旦您清楚地看到当前解决方案的缺点,最好先编写完成这项工作的代码,然后再进行重构。这就是 TDD 的好处,实际上是 TDD 的基础:红色、绿色、重构。
你想最小化客户端和服务器之间的数据流量,而不是使用需要最少内存的对象,即驱动程序 ID。你是如何分配这些 id 的?如果列表更新,新的 ID 是否意味着旧列表中的驱动程序?
It depends on your requirements. I think, there is no straight answer that works for everything, especially for a problem like a parameter type decision. You should understand the real problem you are trying to solve, existing behind the code problem. Don't make beautiful design your goal. I am strong adept of emergent design. Form follows function. Implementing your scenario with no bugs is your goal. Good design will have less bugs, but if you feel you don't have enough experience, just try things out, test different solutions. Sometimes it is better to write code that does the job and refactor later, once you've clearly seen a drawback of your current solution. That's the benefit of TDD, actually the base for TDD: red, green, refactor.
Do you want to minimize the data traffic between the client and server, than use the object that requires the least memory, the driver id. How are you assigning those ids? In case the list gets updated could a new id mean a driver from the old list?