且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我要一个Web API服务和客户端之间共享类型?什么其他选择?

更新时间:2022-05-08 22:35:39

我认为,如果你不小心,第二个选项可能最终会被比第一少REST风格。 REST是少去耦,更多的是管理和重点客户端和服务器之间的耦合。

I would argue that if you are not careful, the second option could end up being less RESTful than the first. REST is less about de-coupling and more about managing and focusing the coupling between client and server.

在你知道客户机和服务器之间的耦合RESTful系统在于媒体类型定义和链接关系的定义。

In a restful system you know the coupling between client and server lies in the media type definitions and the link relation definitions.

在这两个选项中,你被有效地共享客户机和服务器之间的类型。在第一个选项这种共享是经由可管理作为一个的NuGet包和​​版本独立地客户机和服务器的一个具体实施明确的

In both options, you are effectively sharing types between the client and the server. In the first option this sharing is explicit via a concrete implementation that could be managed as a nuget package and versioned independently of client and server.

在第二个选项,你拥有共享类型的两种实现方法。不过,我猜你是不是在定义明确定义了这些类型的属性是媒体类型规划。因此,你有真理没有单一的来源,你什么都没有定义什么客户端和服务器之间的数据合同。你怎么知道你什么时候做出改变,这将打破客户端?至少在一个共享库就可以知道服务器现在使用的版本共享类型1.4.7和客户端使用1.3.9。您可以使用共享类型库语义版本,当你正在打破变化,这将迫使客户端更新就知道了。

In the second option you have two implementations of the shared types. However, I'm guessing you are not planning on defining a media type that explicitly defines what the properties of those types are. Therefore you have no single source of truth, you have nothing to define what the data contract between client and server is. How do you know when you are going to make a change that will break a client? At least with a shared library you can know that the server is now using version 1.4.7 of the shared types and the client is using 1.3.9. You can use semantic versioning on the shared type library to know when you are making a breaking change that will force the client to update.

通过第二个选项,你有一个客户端,并且将独立版本控制的secer,这将是更难跟踪是否有两个版本之间的重大更改。

With the second option, you have a client and a secer that will be independently versioned and it will be much harder to keep track of whether there are breaking changes between the two versions.

显式介质类型总是捕捉到合同和HTTP版本客户端和服务器之间的合同的***方式。但是,如果你不想去那里,然后共享的NuGet库是***的下一个步骤,因为要隔离是从客户端和服务器实现共享系统的一部分。这是REST的主要目标之一。你实际上共享该共享合同的实现库,这一事实不仅影响消费者住上不能占用图书馆其他平台。

Explict Media types are always the best way to capture the contracts and version the contracts between HTTP clients and servers. However, if you don't want to go there, then the shared nuget library is the best next step because you are isolating the part of the system that is shared from the client and server implementations. This is one of the key objectives of REST. The fact that you are actually sharing an implementation library of that shared contract only affects consumers live on other platforms that can't consume that library.

我在几年前创造了这个词 Web Pack中来形容这种想法使用一个共享的NuGet包包含共享耦合。我写了几篇文章这里和here关于这个问题的。

I coined the term Web Pack a few years ago to describe this idea of using a shared nuget package to contain the shared coupling. I wrote a few articles here and here on the subject.