Let`s say I need to store and manipulate collection of complex data in the smart contract. There maybe complex rules defining who and how can change an item. There maybe be several millions of items in the collection.
There are 2 ways how that logic can be implemented
- Using child contract:
contract DataItem { bytes32 key; string value; function DataItem(bytes32 k, string v) { key = k; value = v; } } contract DAppInterface { mapping(bytes32 => address) public dataItems; function addDataItem(bytes32 k, string v) external { dataItems[k] = new DataItem(k, v); } }
- Using struct:
contract DAppInterface { struct DataItem { bytes32 key; string value; } mapping(bytes32 => DataItem) public dataItems; function addDataItem(bytes32 k, string v) external { dataItems[k].key = k; dataItems[k].value = v; } }
What is advantage of using one vs another approach? What are the guidelines for picking one vs another?