在.NET API的字符串或URI?字符串、NET、API、URI

2023-09-04 02:35:14 作者:一寸相思一寸灰

我在写一个.NET封装API为Netflix的API。

I am writing an .NET wrapper API for the Netflix API.

在这一点上我可以选择重新present网址为字符串或URI的对象。在我看来,有一个很好的案例两者。

At this point I can choose to represent URLs as either strings or URI objects. Seems to me there is a good case for both.

所以,如果你使用的API,这将您preFER?

So if you were using an API, which would you prefer?

推荐答案

下面的报价是:框架设计Guildelines 我非常推荐这本书给任何人开发的.NET框架的内置

The below quote is from: Framework Design Guildelines I highly recommend this book to anyone developing frameworks on .Net

不要使用的System.Uri重新present URI / URL数据。(有关参数,   属性和返回值)

Do use System.Uri to represent URI / URL data. (For Parameters, properties, and return values)

的System.Uri是一个更安全和更丰富   再presenting的URI的方式。广泛   使用的URI相关的数据操作   普通字符串已被证明引起   许多安全和正确性   的问题。

System.Uri is a much safer and richer way of representing URIs. Extensive manipulation of URI-related data using plain strings has been shown to cause many security and correctness problems.

请考虑提供基于字符串的重载最常用的   成员的System.Uri参数。

Consider providing string-based overloads for most commonly used members with System.Uri parameters.

在的情况下的使用模式   取一个字符串从用户将   相当普及了,你应该考虑   增加一个方便的重载   接受字符串。基于字符串的   过载应被实现   条款基于URI的过载。

In cases where the usage pattern of taking a string from a user will be common enough, you should consider adding a convenience overload accepting a string. The string-based overload should be implemented in terms of the Uri-based overload.

不要自动重载用版本的所有基于URI的成员   接受一个字符串。

Do Not automatically overload all Uri-based members with a version that accepts a string.

通常,基于URI的API是   preferred。基于字符串的重载   意思是助手为最   常见的情况。因此,   不应自动提供   基于字符串的重载所有   的基于URI的部件的变体。是   选择性的提供这样的帮手   只是为最常用的   变种。

Generally, Uri-based APIs are preferred. String-based overloads are meant to be helpers for the most common scenarios. Therefore, you should not automatically provide string-based overloads for all variants of the Uri-based members. Be selective and provide such helpers just for the most commonly used variants.

(每注释)编辑:书中明确规定:,广泛的操控使用简单的字符串URI相关数据已被证明会导致许多安全性和正确性的问题我不知道你想使用的System.Uri / UriBuilder什么额外的理由。此外,为什么不是你想拿架构的优势,读取/操纵URI?

EDIT (per comments): The book specifically states: "Extensive manipulation of URI-related data using plain strings has been shown to cause many security and correctness problems." I am not sure what additional justification you want for using System.Uri / UriBuilder. Additionally, why wouldn't you want to take advantage of the framework to read/manipulate a URI?

在设计将被他人使用,使他们平易近人,以及可靠的是很重要的一个API。为此,本书也提到,你应该提供好重载常用功能。但是,为了确保正确性,应始终贯彻的基本code用的URI。

When designing an API that will be used by others it is important to make them approachable, as well as reliable. For this reason the book does mention, you should provide "nice" overloads for common functionality. However, to ensure correctness, you should always implement the underlying code with URIs.

能否请你说明你的欲望,或理由只使用字符串?

Can you please clarify your wants, or reasons to use only strings?