Magento 2: Declaring Interfaces on Api/Data Before Creating Model and Resource

apimagento2resourcesservice-contractweb services

I'm creating a custom module, and I want to know if it's better to create a module with this structure :

Api/Data/CustomInterface.php
Model/Custom.php
Model/ResourceModel/Custom.php
Model/ResourceModel/Custom/Collection.php

My model implements the interface, and I create every getters and setters inside.

The other solution is this structure :

Model/Custom.php
Model/ResourceModel/Custom.php
Model/ResourceModel/Custom/Collection.php

No interfaces, no getters, no setters, few methods like Magento 1.

The first is slower, but cleaner.
The second is faster, but messier.

Which is the best for you ?

Thank you

Best Answer

With service contracts usage (API interfaces), it will be possible to expose custom module functionality as web API just by adding config etc/webapi.xml. Improved capabilities for integration with 3rd party systems based on service contracts will be added in the future, as well as queue support.

Another benefit, if there is another module dependent on your module, it can just rely on service interfaces. This reduces coupling between modules and minimizes risk of accidental breaking of dependent module.