简体   繁体   English

如何使用单个实体或单个存储库设计两个不同的项目?

[英]How can I design two different projects with a single entity or a single repository?

We are developing two projects with two different teams.我们正在与两个不同的团队开发两个项目。 However, there are some common points in the two projects, for example, there are common entities and repository usage cases.但是,两个项目有一些共同点,比如有共同的实体和存储库使用案例。 How can I manage with a single entity/repository in two projects without code multiplexing?如何在没有代码多路复用的情况下管理两个项目中的单个实体/存储库?

Note: First project is spring-batch/spring cloud task Second project is microservices project注:第一个项目是spring-batch/spring cloud task 第二个项目是微服务项目

Thanks...谢谢...

Logically sharing DB is not recommended in most cases, due to the two teams will depend on eachother to evolve the schema, they will not be autonomous, so in practice you may end up with a distributed monolith.在大多数情况下不建议逻辑共享数据库,因为两个团队将相互依赖来发展模式,他们不会是自治的,所以在实践中你可能最终会得到一个分布式单体。 You may find reading about event-driven architectures useful if you go microservices.如果您使用 go 微服务,您可能会发现阅读有关事件驱动架构的文章很有用。

If you really need to use the same database without "code duplication" from 2 services, and you use the same tech stack, you can create a shared lib with the repository/entities and other common parts of the system.如果您确实需要使用相同的数据库而不需要来自 2 个服务的“代码重复”,并且您使用相同的技术堆栈,则可以使用存储库/实体和系统的其他公共部分创建一个共享库。 Just treat this lib as any other 3rd party lib, this mindset helped us architecting our microservices.只需将此库视为任何其他 3rd 方库,这种心态有助于我们构建微服务。

But: "code duplication" is not bad if you have multiple autonomous teams .但是:如果你有多个自治团队,“代码重复”也不错 If you consider the DB schema as some kind of contract, and only one team owns it, then other teams can develop their own APIs (repositories, entities) accessing this schema, with their preferred tools/tech stack, etc... I would not even consider this code duplication.如果您将 DB 模式视为某种合同,并且只有一个团队拥有它,那么其他团队可以开发自己的 API(存储库、实体)访问此模式,使用他们喜欢的工具/技术堆栈等......我会甚至不考虑这种代码重复。

Another idea is to create an API layer above the DB (a 3rd microservice maybe?), and use this API from the other services to query/update data.另一个想法是在 DB 之上创建一个 API 层(可能是第三个微服务?),并使用来自其他服务的 API 来查询/更新数据。 This way the DB can change, and API should remain more stable, but you may lose transactionality this way.这样数据库可以更改,并且 API 应该保持更稳定,但您可能会以这种方式失去事务性。

Choose whats the most economic, developer-friendly or best for short/long-term for your project.为您的项目选择最经济、对开发人员友好或最适合短期/长期的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM