在微服务之间共享通用但版本化的基础结构代码的正确方法是什么?
What is the correct way of sharing common yet versioned infrustructural code between microservices?
我们想将一些与基础结构相关的代码(主要是扩展方法或小助手 类)提取到单独的 NuGet 包中。
这些包绝不包含与任何一项服务相关的业务逻辑。
但是,一旦我们最终拥有包之间的依赖关系层次结构,我们可能会遇到版本控制方面的问题:
Service1 直接引用包 Av1.1 并通过引用包 B 间接引用 Av2.0。
ServiceN 直接引用包 Av1.5 并通过引用包 C 间接引用 Av1.3。
NuGet 不支持并行版本,每个项目只使用一个版本。这个版本控制问题也与整体应用程序相关,尽管可能的集成问题似乎要少得多。
有一些想法,比如不提取通用代码并让它在服务之间复制,或者只是不更改包的版本强制每个服务使用相同的版本。
关于版本控制的服务之间的代码重用,是否有任何好的建议?
使用 semantic versioning can help in this case by reducing or eliminating the amount of breaking changes your team experiences when moving between different versions of internal libraries. The core concept of semantic versioning is that major version increments signify the addition of backwards incompatible changes (such as removing or changing the signature of a method in the public API). By contrast, such backwards-incompatible changes must not occur between different minor or patch versions within the same major version. By following such conventions, your team should be able to avoid issues with your internal libraries not being compatible with each other (within the same major version). The Obsolete
属性也是你的朋友,也可以标记将在下一个主要版本中删除的代码。
一旦您的团队对您的库使用了语义版本控制,项目就可以(通常)安全地引用同一程序集的不同版本,就像您的第二个示例中那样。这些不同的版本可以 unified through binding redirects to use the same version of that assembly (usually the higher version of the two). If you are using Visual Studio 2013 or later, these can be automatically generated and updated if this tag 存在于使用您的库的项目文件中。对于您的第一个示例,如果 Av2.0 和 Av1.1 具有不兼容的 API,使用 Nuget 将有助于您更新到 Av2.0,测试该版本,并在某些功能无法正常工作时回滚。
根据个人工作经验,我强烈建议使用 Nuget 和语义版本控制,而不是复制粘贴代码或将所有内部库保留在同一程序集版本中。复制代码通常被认为是 bad practice (see this answer)。从不更改程序集的版本号使得在不反编译的情况下很难判断该程序集文件中实际包含什么代码。
我们想将一些与基础结构相关的代码(主要是扩展方法或小助手 类)提取到单独的 NuGet 包中。
这些包绝不包含与任何一项服务相关的业务逻辑。
但是,一旦我们最终拥有包之间的依赖关系层次结构,我们可能会遇到版本控制方面的问题:
Service1 直接引用包 Av1.1 并通过引用包 B 间接引用 Av2.0。
ServiceN 直接引用包 Av1.5 并通过引用包 C 间接引用 Av1.3。
NuGet 不支持并行版本,每个项目只使用一个版本。这个版本控制问题也与整体应用程序相关,尽管可能的集成问题似乎要少得多。
有一些想法,比如不提取通用代码并让它在服务之间复制,或者只是不更改包的版本强制每个服务使用相同的版本。
关于版本控制的服务之间的代码重用,是否有任何好的建议?
使用 semantic versioning can help in this case by reducing or eliminating the amount of breaking changes your team experiences when moving between different versions of internal libraries. The core concept of semantic versioning is that major version increments signify the addition of backwards incompatible changes (such as removing or changing the signature of a method in the public API). By contrast, such backwards-incompatible changes must not occur between different minor or patch versions within the same major version. By following such conventions, your team should be able to avoid issues with your internal libraries not being compatible with each other (within the same major version). The Obsolete
属性也是你的朋友,也可以标记将在下一个主要版本中删除的代码。
一旦您的团队对您的库使用了语义版本控制,项目就可以(通常)安全地引用同一程序集的不同版本,就像您的第二个示例中那样。这些不同的版本可以 unified through binding redirects to use the same version of that assembly (usually the higher version of the two). If you are using Visual Studio 2013 or later, these can be automatically generated and updated if this tag 存在于使用您的库的项目文件中。对于您的第一个示例,如果 Av2.0 和 Av1.1 具有不兼容的 API,使用 Nuget 将有助于您更新到 Av2.0,测试该版本,并在某些功能无法正常工作时回滚。
根据个人工作经验,我强烈建议使用 Nuget 和语义版本控制,而不是复制粘贴代码或将所有内部库保留在同一程序集版本中。复制代码通常被认为是 bad practice (see this answer)。从不更改程序集的版本号使得在不反编译的情况下很难判断该程序集文件中实际包含什么代码。