I've been writing a Hexagonal Architecture-based backend in-house recently and it's working pretty well (I'm going to), so I'll try to write the basic essence super-easily.
from zope.interface import Interface
from .base import Entity, ValueObject
class Post(Entity):
u""" Post Model """
...
class Tag(ValueObject):
u""" Tag Model """
...
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 """
Please refer to the following articles for ValueObject and zope.interface.
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
@implementer(ITagService)
class DummyTagService(object): # i.e. memory based implementation
u""" TagService for tests """
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
from .infrastructure import TagService
def add_tag(post, tag):
srv = TagService()
srv.add_tag(post, tag) # use TagService as described in ITagService
--Ensure that DummyTagService and TagService behave as expected at the domain tier. --Use DummyTagService when testing objects that use TagService.
I was asked internally, "Do you need ITagService?", But I think the role of the domain layer is to present "specifications" to each of the application layer and the infrastructure layer. .. With the domain layer, the infrastructure layer can be implemented to provide that interface, and the app layer can use the model according to that interface.
If you compare it with the ECMAScript specification,
--The ECMAScript specification is domain layer, --Google Chrome developers implement the browser according to the ECMAScript specification and --App developers develop JavaScript apps according to the ECMAScript specifications
With the domain layer in between, both feature providers and users can benefit from each other.
Recommended Posts