Component interfaces give many benefits:
-
Consistency. Components can only be accessed through their interface, making them easy to find, use, and reason about.
-
Encapsulation. You can change a component’s implementing namespaces without breaking the interface contract.
-
Composability. All components have access to all other components via their interfaces, and you can replace them as long as they use the same interface.
When you created the user component as part of our tutorial, poly also created the user interface.
So what is an interface, and what is it good for?
An interface in the Polylith world is a namespace named interface that usually lives in one, but sometimes several, sub-namespaces within a component.
Looking at our example user component, if you exclude the top namespace, in Polylith we think of its interface namespace as user.interface.
The interface defines def, defn, or defmacro statements which form the contract that it exposes to other components and bases.