Domain Layer as a specification
Thesedays, I'm writing application using Hexagonal Architecture with python and feel that "domain layer seems to be behaving as a specification". Let me explain about that here.
Example: TagService
For explanation, let's use a simple example, "TagService".
I'll define ITagService interface on domain layer, implement it on infrastructure layer and use it on application layer. The application is writen by python, so interfaces are defined with zope.interface package (zope.interface is useful for Hexagonal Architecture in Python).
Domain Layer
from zope.interface import Interface class ITagService(Interface): # specification u""" Tag management domain service """ def add_tag(post, tag): u""" add tag to a post """ def remove_tag(post, tag): u""" remove tag from a post """
Infrastructure Layer
from zope.interface import implementer from .domain.model import ITagService @implementer(ITagService) # implement TagService as described in ITagService class TagService(object): u""" TagService using xxx """ def add_tag(self, post, tag): u""" add tag to a post """ # do some actual staff def remove_tag(self, post, tag): u""" remove tag from a post """ # do some actual staff
Application Layer
from .infrastructure import TagService def add_tag(post, tag): srv = TagService() srv.add_tag(post, tag) # use TagService as described in ITagService
The point is that ITagService defined in Domain Layer behaves as a bridge between application layer and infrastructure layer. In other words, ITagService behaves as a specification for both application and infrastructure layer.
browser implementation analogy
Let's think about these relationships using an analogy of browser implementation. (Or if you want, you can use C++ compiler implementation analogy or else instead.)
EcmaScript Specification behaves like a domain layer
Google Chrome implements EcmaScript Specification, like an infrastructure layer
Application Developer uses EcmaScript Specification, like an application layer
By defining EcmaScript specification, Chrome developer can implement features without knowing actual usage of application developers, and application developer can use JavaScript features without knowing Chrome's detailed implementation.
After I have adopted Hexagonal Architecture rather than Layered Architecture, responsibilities of each layer have been getting clearer.











