简体   繁体   English

每个资产在Fabric中都有自己的链码吗?

[英]Should each asset have its own chaincode in Fabric?

The Chaincode for Developers tutorial uses only one chaincode. Chaincode for Developers教程仅使用一个链码。

I know multiple chaincode is supported as well, and the aforementioned tutorial uses a chaincode called SimpleAsset . 我知道也支持多个链代码 ,上述教程使用名为SimpleAsset的链SimpleAsset

Does it imply that there is an expectation to have 1 chaincode for 1 asset? 这是否意味着期望1个资产拥有1个链码? If my application involves several asset types (eg. Widgets and Gadgets), is the best practice to have a separate chaincode managing the lifecycle of each asset type? 如果我的应用程序涉及多种资产类型(例如小部件和小工具),那么最佳做法是使用单独的链代码来管理每种资产类型的生命周期吗? Or a single chaincode? 还是单个链码?

It is possible to have one single chaincode to manage multiple assets. 可以使用一个链代码来管理多个资产。 There are no fixed rule on how to map assets to chaincode, but some guidelines I found useful are: 关于如何将资产映射到链码没有固定的规则,但我发现有用的一些指导原则是:

  • If the assets are tightly coupled I prefer to have a single chaincode to manage them, specially if the life cycle of one asset depends on the lyfe cycle of another asset. 如果资产紧密耦合,我更愿意使用单个链代码来管理它们,特别是如果一个资产的生命周期取决于另一个资产的生命周期。

  • If you expect for a particular asset changes in its logic and life cycle then it is best to isolate it in its own chaincode. 如果您希望特定资产的逻辑和生命周期发生变化,那么最好将其隔离在自己的链代码中。 The idea is to keep assets that rarely will have its behavior changed separated from assets that would require changes in its chaincode from time to time. 我们的想法是将很少将其行为更改的资产与需要不时更改其链代码的资产分开。

  • Try to keep your chaincodes small. 尽量保持你的链码小。 This one is mostly a personal preference but it is also related to the previous point. 这个主要是个人偏好,但也与前一点有关。

  • If you have a small number of assets with not too much logic it might make sense to have a single chaincode. 如果你有少量没有太多逻辑的资产,那么拥有一个链码可能是有意义的。

Hope you find this useful. 希望您觉得这个有帮助。

There is one thing which need to keep in mind. 有一件事需要牢记。 Once you are going to maintain all you assets within single chaincode namespace, you need to be able to distinguish between different type of assets in some way, such that you will be able to search for specific type of asset given its ID. 一旦您要在单个chaincode命名空间内维护所有资产,您需要能够以某种方式区分不同类型的资产,这样您就可以在给定ID的情况下搜索特定类型的资产。 This basically will eventually force you to extend your document model to include type information or to incorporate that knowledge into the key, making it easier to differentiate between types of assets/documents. 这基本上最终将迫使您扩展文档模型以包含类型信息或将该知识合并到密钥中,从而更容易区分资产/文档类型。

Now, thinking on this, this is in a sense has same effect as splitting assets management into separate chaincodes. 现在,考虑到这一点,这在某种意义上与将资产管理分解为单独的链代码具有相同的效果。 Moreover splitting management or maintenance of different type of assets with different chaicodes will facilitate better separation of concerns which in general better design practice. 此外,拆分管理或维护具有不同类型的不同类型的资产将有助于更好地分离关注点,这通常是更好的设计实践。

Doing small PoC or playing around with platform it make a lot of sense to put everything into one chaincode, while speaking long term and keeping in mind that you need eventually to support your own chaincode code base, I'd suggest to split up chaincodes whenever it make sense and I think managing several assets is a good candidate. 做一些小型PoC或在平台上玩游戏将一切都放在一个链码中是很有意义的,同时长期说话并记住你最终需要支持你自己的链码代码库,我建议每当分割链码时它是有道理的,我认为管理几个资产是一个很好的候选人。

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

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