title: DDD with Python: did I get it right?
body: <p>I'm trying to use <strong>Domain Driven Design (DDD)</strong> in a Python project, but it looks like a lot of boilerplate code. I think I'm on the wrong path.</p>
  
  <p>I've three files, all defining the item for each purpose. It feels too much. Also I'm converting to and from dictionaries too often, but I'm trying to keep the purposes separated.</p>
  
  <p>This topic should not be opinion-based, because I'm trying to follow the DDD approach and there should be a pattern to follow.</p>
  
  <p>Relevant part of the code below. Please have a closer look on the <code>ItemRepository</code>.</p>
  
  <p><em>/domain/item.py</em></p>
  
  <pre><code>"""
  Vanilla Python class, business level
  """
  class ItemDomain:
      def __init__(self, name):
          self.name = name
  
      @classmethod
      def from_dictionary(cls, dictionary):
          return cls(name=dictionary['name'])
  
      def to_dictionary(self):
          return {'name': self.name } 
  
  </code></pre>
  
  <p><em>/model/item.py</em></p>
  
  <pre><code>"""
  Persistent model for SQLAlchemy
  """
  class ItemModel(DefaultModel):
      __tablename__ = 'items'
      name = Column(Text)
  </code></pre>
  
  <p><em>/schema/item.py</em></p>
  
  <pre><code>"""
  Schema for Marshmallow
  """
  class ItemSchema(Schema):
      name = fields.Str(required=True)
  </code></pre>
  
  <p><em>/repository/item.py</em></p>
  
  <pre><code>class ItemRepository:
  
      def get_one(item_id):
          # ...
          model = session.query(ItemModel).filter_by(item_id=item_id).first()
          return ItemDomain.from_dictionary(dict(model))
  
      def add_one(item: ItemDomain):
          # ...
          item = item.to_dictionary()
          ItemSchema().load(item)  # validation: will raise an exception if invalid
          model = ItemModel()
          model.from_dictionary(item)
          session.add(model)
          # ...
  </code></pre>
  
  <p>What can I do to have a clean architecture without overhead?</p>