使用按类型获取或按 ID 获取

发布于 2024-10-31 06:08:39 字数 342 浏览 3 评论 0原文

我有一个名为 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 技术交流群。

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

发布评论

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

评论(6

反差帅 2024-11-07 06:08:39

ID 是该方法运行所需的唯一信息吗?如果是这样,我就这样吧。

Is the ID the only piece of information the method requires to function? If so I'd leave it at that.

蓝梦月影 2024-11-07 06:08:39

对象方法有问题。

Dealership dealership;
GetCarsRelatedToDealership(dealership); // dealership is null

var dealership = new Dealership();
GetCarsRelatedToDealership(dealership); // dealership has no id

在这些情况下,对象不会给你带来任何比 id 更好的优势。 id 可能是错误的,但您可以验证它。该对象使事情变得更加复杂。

在处理服务时,我会创建一对请求/响应类。这使您可以确保您的签名永远不需要更改。您的 Request 或 Response 对象可能会更改,但方法签名不会更改。

现在,我可以传递经销商 ID 以及请求信息的用户,并确保允许请求的用户查看该经销商的库存。只是通过经销商或身份证的东西是不允许的。

public class CarsRequest
{
  public int DealershipId { get; set; }
  public int RequesterId { get; set; }
}

public class CarsResponse
{
  public Car[] Cars { get; set; }
}

CarsResponse GetCarsRelatedToDealership(CarsRequest request);

The object approach has problems.

Dealership dealership;
GetCarsRelatedToDealership(dealership); // dealership is null

var dealership = new Dealership();
GetCarsRelatedToDealership(dealership); // dealership has no id

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.

public class CarsRequest
{
  public int DealershipId { get; set; }
  public int RequesterId { get; set; }
}

public class CarsResponse
{
  public Car[] Cars { get; set; }
}

CarsResponse GetCarsRelatedToDealership(CarsRequest request);
以酷 2024-11-07 06:08:39

对于网络服务,我尝试尽量减少通过线路发送的数据量。如果您还需要一个方法,我可能会为“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.

箜明 2024-11-07 06:08:39

您可以让该方法采用接口吗?这将使其更加灵活,并且可能更容易测试。

List<Cars> GetCarsRelatedToDealership( IDealership dealership );

Can you have the method take an interface? This would make it much more flexible and perhaps easier to test.

List<Cars> GetCarsRelatedToDealership( IDealership dealership );
少年亿悲伤 2024-11-07 06:08:39

我会采用 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

南巷近海 2024-11-07 06:08:39

通常仅使用 ID 就足够了,而且要容易得多,因为您只需将 ID 从一个地方传递到另一个地方即可。这对于 Web 服务尤其重要,因为您希望最大限度地减少传输的数据量。

但是,如果您经常有大型方法签名,其中输入没有通过方法名称很好地指定:

List<Cars> GetCars(int makeId, int modelId, int year, int dealershipId);

...很容易将错误的 ID 传递到错误的位置。从那时起我就开始尝试寻找不同的策略。将输入强类型化为域对象是一种解决方案,但它通常会比简单地使用特殊选项 POCO 带来更多麻烦:

public class GetCarsOptions
{
    public int MakeId {get;set;}
    public int ModelId {get;set;}
    public int Year {get;set;}
    public int DealershipId {get;set;}
}

List<Cars> GetCars(GetCarsOptions options)
{
    ValidateOptions(options); // make sure the values all make sense.
}

事实是,您无法在运行时捕获所有可能的错误,因此它是将自动化测试与“快速失败”技术结合起来是一个好主意,可以在编译之后和部署之前捕获尽可能多的错误。

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:

List<Cars> GetCars(int makeId, int modelId, int year, int dealershipId);

... 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:

public class GetCarsOptions
{
    public int MakeId {get;set;}
    public int ModelId {get;set;}
    public int Year {get;set;}
    public int DealershipId {get;set;}
}

List<Cars> GetCars(GetCarsOptions options)
{
    ValidateOptions(options); // make sure the values all make sense.
}

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.

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