0

As far as I understand, the domain model should not know about the persistence layer. But then... How can I deal with removing element that are dependant of a father object?

For example, in my domain, if I have a ShoppingBasket with Products, and I want to remove a Product. I can remove the Product in my Domain Model. But how will I get my persistence layer to be aware that a Product must be deleted (i.e. how do I get the corresponding product relashionship removed from the database)?

  • Are you using an ORM like Entity Framework or NHibernate (or Hibernate for Java)? If so, then @RobertHarvey's answer is appropriate. If you are creating your own data access code, then you've got some additional work. – Greg Burghardt Mar 24 '17 at 16:47
  • @Greg Burghardt yes, i had an ORM in mind. It happens to be Doctrine, but the same applies. – mika Mar 24 '17 at 18:01
2

Usually, in situations like this, there is an object that maps the associated objects to each other. In this case, it would be a BasketItem object. It would look something like this:

public class BasketItem
{
    public int BasketID;
    public int ProductID;
    public int Quantity;
}

Removal of the item from the basket is a simple matter of removing the appropriate BasketItem object.

This is basic database design, which is why it's not obvious. Database design is not always taught in "class first" paradigms, which is unfortunate.

  • It appears the OP's target language is PHP, given that he had the Doctrine ORM in mind. Same advice applies, though. – Greg Burghardt Mar 24 '17 at 18:09
  • @GregBurghardt: How did you figure that out from the question text? – Robert Harvey Mar 24 '17 at 19:56
  • It was in a comment to his question. It really should be included in the question text. – Greg Burghardt Mar 24 '17 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.