I am confused about Domain Driven Design Approaches. From the sources on net I understood it is way of segregating your Domain Objects
and Database Objects
but I don't understand the difference between two.
For an example lets take the code of Polls example in django tutorial, there are two models Polls
and Choice
.
Are these domain level objects
or database level objects
?
Is there a need for DDD with an ORM?
If yes, can you provide a good situation where you need to use DDD approach with an ORM
For example, this is the model
class Polls(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
DDD approach code I have seen people writing
class PollService(object):
def __init__(self, poll_repository):
self.poll_respository = poll_respository
def update(self, poll_id):
poll = self.poll_respository.fetch_by_id(poll_id)
poll.question += '?'
self.poll_respository.update(poll)
#assume that the following code works?
class PollRepository():
def __init__(self, db):
self.db = db
def update(self, poll):
try:
self.db.session().add(poll)
self.db.session.commit()
except Exception:
self.db.session.rollback()
Is this a correct approach? I see a lot of redundant code here but people say that Polls
is a domain level object and it should not directly talk to database?
Does DDD always comes with a DDD-reposiotry ? Why we need a DDD repository if we have an ORM
Another approach
views.py
def update_poll(poll_id):
poll = models.Polls.objects.get(poll_id)
poll.question += '?'
poll.save()
What is wrong with this approach?