使用按类型获取或按 ID 获取
我有一个名为 GetObjectsRelatedToAType 的方法,例如 GetCarsRelatedToDealership。从 Web 服务或一般方法签名的角度来看,哪种方法更好:
List<Cars> GetCarsRelatedToDealership( Dealership dealership );
或者
List<Cars> GetCarsRelatedToDealership( id dealership );
我倾向于喜欢对象方法,因为它在确保方法的源输入有效方面更有力。想法/建议?
I have a method called GetObjectsRelatedToAType, e.g. GetCarsRelatedToDealership. Which approach is better from a web services or general method signature standpoint:
List<Cars> GetCarsRelatedToDealership( Dealership dealership );
or
List<Cars> GetCarsRelatedToDealership( id dealership );
I tend to like the object approach because it is a little more forceful about making sure the source input to the method is valid. Thoughts/advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
ID 是该方法运行所需的唯一信息吗?如果是这样,我就这样吧。
Is the ID the only piece of information the method requires to function? If so I'd leave it at that.
对象方法有问题。
在这些情况下,对象不会给你带来任何比 id 更好的优势。 id 可能是错误的,但您可以验证它。该对象使事情变得更加复杂。
在处理服务时,我会创建一对请求/响应类。这使您可以确保您的签名永远不需要更改。您的 Request 或 Response 对象可能会更改,但方法签名不会更改。
现在,我可以传递经销商 ID 以及请求信息的用户,并确保允许请求的用户查看该经销商的库存。只是通过经销商或身份证的东西是不允许的。
The object approach has problems.
In these cases the object isn't giving you any advantage over just the id. The id might be wrong, but you can validate that. The object makes things a bit more complicated.
When dealing with services, I would create a Request / Response pair of classes. This allows you to ensure your signature never needs to change. Your Request or Response objects might change, but the method signature does not.
Now I can pass the dealership id, as well as who is requesting the information and ensure that the user asking is allowed to see the inventory of that dealership. Something just passing the dealership or id would not allow.
对于网络服务,我尝试尽量减少通过线路发送的数据量。如果您还需要一个方法,我可能会为“Dealership”类/接口创建成员方法“GetRelatedCars”。当 Web 服务调用到来时,您可以通过根据 id 获取该对象并调用该对象上的“GetRelatedCars”方法来验证它是否是真正的“经销商”(并且调用者对其具有权限)。
For the web services I try to minimize how much data is being sent over the wire. If you also need a method I would probably make member method "GetRelatedCars" for the "Dealership" class/interface. When the web service call comes you could validate it is a real "Dealership" (and the caller has rights to it) by getting that object based on the id and for the call the "GetRelatedCars" method on the object.
您可以让该方法采用接口吗?这将使其更加灵活,并且可能更容易测试。
Can you have the method take an interface? This would make it much more flexible and perhaps easier to test.
我会采用 Id 方法,并验证 Id 是否有效。您可能会获得无效的 Dealership 对象,在这种情况下,使用
Dealership
不会获得任何优势I'd go with the Id approach, and validate that the Id is valid. You could potentially get an invalid Dealership object, in which case there would be no advantage gained to using the
Dealership
通常仅使用 ID 就足够了,而且要容易得多,因为您只需将 ID 从一个地方传递到另一个地方即可。这对于 Web 服务尤其重要,因为您希望最大限度地减少传输的数据量。
但是,如果您经常有大型方法签名,其中输入没有通过方法名称很好地指定:
...很容易将错误的 ID 传递到错误的位置。从那时起我就开始尝试寻找不同的策略。将输入强类型化为域对象是一种解决方案,但它通常会比简单地使用特殊选项 POCO 带来更多麻烦:
事实是,您无法在运行时捕获所有可能的错误,因此它是将自动化测试与“快速失败”技术结合起来是一个好主意,可以在编译之后和部署之前捕获尽可能多的错误。
Using just an ID is usually sufficient, and is far easier because you can simply pass an ID around from one place to another. This is especially important with web services, where you want to minimize the amount of data being transferred.
However, if you often have large method signatures where the input is not specified very well by the name of the method:
... it can become very easy to pass the wrong ID in to the wrong place. That's the point where I would begin trying to find a different strategy. Strongly typing your inputs as domain objects is one solution, but it will generally cause you a lot more hassle than simply using a special options POCO:
The fact is, you won't be able to catch every possible error at run time, so it's a good idea to combine automated tests with "fail-fast" techniques to try to catch as many bugs as possible after compilation and before deployment.